blob: 20aaa132f9867d8098a3908759a76d2c9ff3d3d2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
module Rack::Mount
module Analysis
class Histogram < Hash #:nodoc:
attr_reader :count
def initialize
@count = 0
super(0)
expire_caches!
end
def <<(value)
@count += 1
self[value] += 1 if value
expire_caches!
self
end
def sorted_by_frequency
sort_by { |_, value| value }.reverse!
end
def max
@max ||= values.max || 0
end
def min
@min ||= values.min || 0
end
def mean
@mean ||= calculate_mean
end
def standard_deviation
@standard_deviation ||= calculate_standard_deviation
end
def upper_quartile_limit
@upper_quartile_limit ||= calculate_upper_quartile_limit
end
def keys_in_upper_quartile
@keys_in_upper_quartile ||= compute_keys_in_upper_quartile
end
private
def calculate_mean
count / size
end
def calculate_variance
values.inject(0) { |sum, e| sum + (e - mean) ** 2 } / count.to_f
end
def calculate_standard_deviation
Math.sqrt(calculate_variance)
end
def calculate_upper_quartile_limit
mean + standard_deviation
end
def compute_keys_in_upper_quartile
sorted_by_frequency.select { |_, value| value >= upper_quartile_limit }.map! { |key, _| key }
end
def expire_caches!
@max = @min = @mean = @standard_deviation = nil
@keys_in_upper_quartile = nil
end
end
end
end
|