Class: Twingly::Metrics::Histogram

Inherits:
Object
  • Object
show all
Defined in:
lib/twingly/metrics/histogram.rb

Constant Summary collapse

DEFAULT_SAMPLE_SIZE =
1028
DEFAULT_ALPHA =
0.015

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sample) ⇒ Histogram

Returns a new instance of Histogram.



22
23
24
25
26
27
28
29
# File 'lib/twingly/metrics/histogram.rb', line 22

def initialize(sample)
  @sample   = sample
  @count    = Atomic.new(0)
  @min      = Atomic.new(nil)
  @max      = Atomic.new(nil)
  @sum      = Atomic.new(0)
  @variance = Atomic.new([-1, 0])
end

Class Method Details

.new_exponentially_decayingObject



18
19
20
# File 'lib/twingly/metrics/histogram.rb', line 18

def self.new_exponentially_decaying
  new(Twingly::Metrics::ExponentiallyDecayingSample.new(DEFAULT_SAMPLE_SIZE, DEFAULT_ALPHA))
end

.new_uniformObject



14
15
16
# File 'lib/twingly/metrics/histogram.rb', line 14

def self.new_uniform
  new(Twingly::Metrics::UniformSample.new(DEFAULT_SAMPLE_SIZE))
end

Instance Method Details

#clearObject



31
32
33
34
35
36
37
38
# File 'lib/twingly/metrics/histogram.rb', line 31

def clear
  @sample.clear
  @count.value = 0
  @min.value = nil
  @max.value = nil
  @sum.value = 0
  @variance.value = [-1, 0]
end

#countObject



53
54
55
# File 'lib/twingly/metrics/histogram.rb', line 53

def count
  @count.value
end

#maxObject



61
62
63
# File 'lib/twingly/metrics/histogram.rb', line 61

def max
  count.positive? ? @max.value : 0.0
end

#max=(potential_max) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/twingly/metrics/histogram.rb', line 81

def max=(potential_max)
  done = false

  until done
    current_max = @max.value
    done = (!current_max.nil? && current_max >= potential_max) || @max.compare_and_swap(current_max,
                                                                                        potential_max)
  end
end

#meanObject



69
70
71
# File 'lib/twingly/metrics/histogram.rb', line 69

def mean
  count.positive? ? @sum.value / count : 0.0
end

#minObject



65
66
67
# File 'lib/twingly/metrics/histogram.rb', line 65

def min
  count.positive? ? @min.value : 0.0
end

#min=(potential_min) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/twingly/metrics/histogram.rb', line 91

def min=(potential_min)
  done = false

  until done
    current_min = @min.value
    done = (!current_min.nil? && current_min <= potential_min) || @min.compare_and_swap(current_min,
                                                                                        potential_min)
  end
end

#snapshotObject



49
50
51
# File 'lib/twingly/metrics/histogram.rb', line 49

def snapshot
  @sample.snapshot
end

#stddevObject



73
74
75
# File 'lib/twingly/metrics/histogram.rb', line 73

def stddev
  count.positive? ? variance**0.5 : 0.0
end

#sumObject



57
58
59
# File 'lib/twingly/metrics/histogram.rb', line 57

def sum
  @sum.value
end

#update(value) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/twingly/metrics/histogram.rb', line 40

def update(value)
  @count.update { |v| v + 1 }
  @sample.update(value)
  self.max = value
  self.min = value
  @sum.update { |v| v + value }
  update_variance(value)
end

#update_variance(value) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/twingly/metrics/histogram.rb', line 101

def update_variance(value) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  @variance.update do |old_values|
    new_values = Array.new(2)
    if old_values[0] == -1
      new_values[0] = value
      new_values[1] = 0
    else
      old_m = old_values[0]
      old_s = old_values[1]

      new_m = old_m + ((value - old_m) / count)
      new_s = old_s + ((value - old_m) * (value - new_m))

      new_values[0] = new_m
      new_values[1] = new_s
    end

    new_values
  end
end

#varianceObject



77
78
79
# File 'lib/twingly/metrics/histogram.rb', line 77

def variance
  count <= 1 ? 0.0 : @variance.value[1] / (count - 1)
end