Class: Stud::Benchmark::Results

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/stud/benchmark.rb

Overview

def run

Constant Summary collapse

TICKS =
%w{       }

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Results

Returns a new instance of Results.



27
28
29
# File 'lib/stud/benchmark.rb', line 27

def initialize(data)
  @data = data
end

Instance Method Details

#distribution(&range_compute) ⇒ Object

def log_distribution

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/stud/benchmark.rb', line 56

def distribution(&range_compute)
  raise ArgumentError.new("Missing range computation block") if !block_given?

  max = @data.max
  dist = Hash.new { |h,k| h[k] = 0 }
  each do |value| 
    range = range_compute.call(value)
    dist[range] += 1
  end
  return dist
end

#each(&block) ⇒ Object

def environment



41
42
43
# File 'lib/stud/benchmark.rb', line 41

def each(&block)
  @data.each(&block)
end

#environmentObject

def initialize



31
32
33
34
35
36
37
38
39
# File 'lib/stud/benchmark.rb', line 31

def environment
  # Older rubies don't have the RUBY_ENGINE defiend
  engine = (RUBY_ENGINE rescue "ruby")
  # Include jruby version in the engine
  engine += (JRUBY_VERSION rescue "")
  version = RUBY_VERSION

  return "#{engine} #{version}"
end

#log_distributionObject

def each



45
46
47
48
49
50
51
52
53
54
# File 'lib/stud/benchmark.rb', line 45

def log_distribution
  return distribution do |value|
    if value == 0
      0 ... 0
    else
      tick = (Math.log2(value).floor).to_f rescue 0
      (2 ** tick) ... (2 ** (tick+1))
    end
  end
end

#meanObject

def distribution



68
69
70
71
72
73
74
# File 'lib/stud/benchmark.rb', line 68

def mean
  if @mean.nil?
    total = Float(@data.count)
    @mean = sum / total
  end
  return @mean
end

#pretty_printObject

def to_s



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/stud/benchmark.rb', line 92

def pretty_print
  min = @data.min
  max = @data.max
  zmax = Float(max - min) # "zero" at the 'min' value, offset the max.
  incr = 0.1 # 10% increments
  #dist = distribution do |value|
    #percent = (value - min) / zmax
    #if percent == 1
      #(1 - incr ... 1.0)
    #else
      #start = ((percent * 10).floor / 10.0)
      #start ... (start + incr)
    #end
  #end
  dist = log_distribution

  total = dist.inject(0) { |sum, (step, count)| sum + count }
  sorted = dist.sort { |a,b| a.first.begin <=> b.first.begin }
  puts sorted.collect { |lower_bound, count|
    #puts lower_bound
    percent = (count / Float(total))
    "%40s: %s" % [lower_bound, (TICKS.last * (50 * percent).ceil)]
  }.join("\n")

end

#stddevObject

def mean



76
77
78
79
# File 'lib/stud/benchmark.rb', line 76

def stddev
  # sum of square deviations of mean divided by total values
  return Math.sqrt(inject(0) { |s, v| s + (v - mean) ** 2 } / (@data.count - 1))
end

#sumObject

def stddev



81
82
83
84
85
86
# File 'lib/stud/benchmark.rb', line 81

def sum
  if @sum.nil?
    @sum = inject(0) { |s,v| s + v }
  end
  return @sum
end

#to_sObject

def sum



88
89
90
# File 'lib/stud/benchmark.rb', line 88

def to_s
  return "#{environment}: avg: #{mean} stddev: #{stddev}"
end