class Metrics::Instruments::Histogram
Attributes
count[R]
Public Class Methods
new(type = :uniform)
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 10 def initialize(type = :uniform) @sample = case type when :uniform Metrics::Statistics::UniformSample.new when :exponential Metrics::Statistics::ExponentialSample.new else raise ArgumentError, "Unknown type #{type.inspect}" end clear end
Public Instance Methods
as_json(*_)
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 125 def as_json(*_) { :min => min, :max => max, :mean => mean, :variance => variance, :percentiles => quantiles(Timer::DEFAULT_PERCENTILES) } end
clear()
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 33 def clear @sample.clear @min = nil @max = nil @sum = 0 @count = 0 @variance_m = -1 @variance_s = 0 end
max()
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 97 def max @max || 0.0 end
mean()
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 105 def mean if @count > 0 @sum / @count else 0.0 end end
min()
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 101 def min @min || 0.0 end
quantiles(percentiles)
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 43 def quantiles(percentiles) # Calculated using the same logic as R and Excel use # as outlined by the NIST here: http://www.itl.nist.gov/div898/handbook/prc/section2/prc252.htm sorted_values = @sample.values[0...@count].sort scores = { } percentiles.each do |pct| scores[pct] = if @count == 0 0.0 else index = pct * (sorted_values.length - 1) + 1.0 if index <= 1 sorted_values.first elsif index >= sorted_values.length sorted_values.last else lower = sorted_values[index.to_i - 1] upper = sorted_values[index.to_i] lower + (index - index.floor) * (upper - lower) end end end scores end
std_dev()
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 113 def std_dev if @count > 0 Math.sqrt(variance) else 0.0 end end
to_json(*_)
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 135 def to_json(*_) as_json.to_json end
update(value)
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 24 def update(value) @count += 1 @sum += value @sample.update(value) update_max(value) update_min(value) update_variance(value) end
update_max(value)
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 75 def update_max(value) if @max.nil? || value > @max @max = value end end
update_min(value)
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 69 def update_min(value) if @min.nil? || value < @min @min = value end end
update_variance(value)
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 81 def update_variance(value) new_m = @variance_m + ((value - @variance_m) / @count) new_s = @variance_s + ((value - @variance_m) * (value - new_m)) @variance_m = new_m @variance_s = new_s end
values()
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 121 def values @sample.values end
variance()
click to toggle source
# File lib/ruby-metrics/instruments/histogram.rb, line 89 def variance if @count <= 1 0.0 else @variance_s.to_f / (@count - 1) end end