Class: Aggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/aggregate.rb

Overview

Implements aggregate statistics and maintains configurable histogram for a set of given samples. Convenient for tracking high throughput data.

Constant Summary collapse

@@LOG_BUCKETS =

The number of buckets in the binary logarithmic histogram (low => 2**0, high => 2**@@LOG_BUCKETS)

128

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(low = nil, high = nil, width = nil) ⇒ Aggregate

Create a new Aggregate that maintains a binary logarithmic histogram by default. Specifying values for low, high, and width configures the aggregate to maintain a linear histogram with (high - low)/width buckets



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
# File 'lib/aggregate.rb', line 32

def initialize (low=nil, high=nil, width=nil)
  @count = 0
  @sum = 0.0
  @sum2 = 0.0
  @outliers_low = 0
  @outliers_high = 0

  # If the user asks we maintain a linear histogram where
  # values in the range [low, high) are bucketed in multiples
  # of width
  if (nil != low && nil != high && nil != width)

    #Validate linear specification
    if high <= low
	raise ArgumentError, "High bucket must be > Low bucket"
    end

    if high - low < width
      raise ArgumentError, "Histogram width must be <= histogram range"
    end

    if 0 != (high - low).modulo(width)
	raise ArgumentError, "Histogram range (high - low) must be a multiple of width"
    end

    @low = low
    @high = high
    @width = width
  else
    @low = 1
    @high = to_bucket(@@LOG_BUCKETS - 1)
  end

  #Initialize all buckets to 0
  @buckets = Array.new(bucket_count, 0)
end

Instance Attribute Details

#countObject (readonly)

The current number of samples



9
10
11
# File 'lib/aggregate.rb', line 9

def count
  @count
end

#maxObject (readonly)

The maximum sample value



12
13
14
# File 'lib/aggregate.rb', line 12

def max
  @max
end

#meanObject (readonly)

The current average of all samples



6
7
8
# File 'lib/aggregate.rb', line 6

def mean
  @mean
end

#minObject (readonly)

The minimum samples value



15
16
17
# File 'lib/aggregate.rb', line 15

def min
  @min
end

#outliers_highObject (readonly)

The number of samples falling above the highest valued histogram bucket



24
25
26
# File 'lib/aggregate.rb', line 24

def outliers_high
  @outliers_high
end

#outliers_lowObject (readonly)

The number of samples falling below the lowest valued histogram bucket



21
22
23
# File 'lib/aggregate.rb', line 21

def outliers_low
  @outliers_low
end

#sumObject (readonly)

The sum of all samples



18
19
20
# File 'lib/aggregate.rb', line 18

def sum
  @sum
end

Instance Method Details

#<<(data) ⇒ Object

Include a sample in the aggregate



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/aggregate.rb', line 70

def << data

  # Update min/max
  if 0 == @count
    @min = data
    @max = data
  else
    @max = [data, @max].max
    @min = [data, @min].min
  end

  # Update the running info
  @count += 1
  @sum += data
  @sum2 += (data * data)

  # Update the bucket
  @buckets[to_index(data)] += 1 unless outlier?(data)
end

#eachObject

Iterate through each bucket in the histogram regardless of its contents



188
189
190
191
192
# File 'lib/aggregate.rb', line 188

def each
  @buckets.each_with_index do |count, index|
    yield(to_bucket(index), count)
  end
end

#each_nonzeroObject

Iterate through only the buckets in the histogram that contain samples



196
197
198
199
200
# File 'lib/aggregate.rb', line 196

def each_nonzero
  @buckets.each_with_index do |count, index|
    yield(to_bucket(index), count) if count != 0
  end
end

#skip_row(value_width) ⇒ Object

We denote empty buckets with a ‘~’



150
151
152
# File 'lib/aggregate.rb', line 150

def skip_row(value_width)
  sprintf("%#{value_width}s ~\n", " ")
end

#std_devObject

Calculate the standard deviation



95
96
97
# File 'lib/aggregate.rb', line 95

def std_dev
  Math.sqrt((@sum2.to_f - ((@sum.to_f * @sum.to_f)/@count.to_f)) / (@count.to_f - 1))
end

#to_s(columns = nil) ⇒ Object

Generate a pretty-printed ASCII representation of the histogram



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/aggregate.rb', line 108

def to_s(columns=nil)

  #default to an 80 column terminal, don't support < 80 for now
  if nil == columns
    columns = 80
  else
    raise ArgumentError if columns < 80
  end

  #Find the largest bucket and create an array of the rows we intend to print
  disp_buckets = Array.new
  max_count = 0
  total = 0
  @buckets.each_with_index do |count, idx|
    next if 0 == count
    max_count = [max_count, count].max
    disp_buckets << [idx, to_bucket(idx), count]
    total += count
  end

  #XXX: Better to print just header --> footer
  return "Empty histogram" if 0 == disp_buckets.length

  #Figure out how wide the value and count columns need to be based on their
  #largest respective numbers
  value_str = "value"
  count_str = "count"
  total_str = "Total"
  value_width = [disp_buckets.last[1].to_s.length, value_str.length].max
  value_width = [value_width, total_str.length].max
  count_width = [total.to_s.length, count_str.length].max
  max_bar_width  = columns - (value_width + " |".length + "| ".length + count_width)

  #Determine the value of a '@'
  weight = [max_count.to_f/max_bar_width.to_f, 1.0].max

  #format the header
  histogram = sprintf("%#{value_width}s |", value_str)
  max_bar_width.times { histogram << "-"}
  histogram << sprintf("| %#{count_width}s\n", count_str)

  # We denote empty buckets with a '~'
  def skip_row(value_width)
    sprintf("%#{value_width}s ~\n", " ")
  end

  #Loop through each bucket to be displayed and output the correct number
  prev_index = disp_buckets[0][0] - 1

  disp_buckets.each do |x|
    #Denote skipped empty buckets with a ~
    histogram << skip_row(value_width) unless prev_index == x[0] - 1
    prev_index = x[0]

    #Add the value
    row = sprintf("%#{value_width}d |", x[1])

    #Add the bar
    bar_size = (x[2]/weight).to_i
    bar_size.times { row += "@"}
    (max_bar_width - bar_size).times { row += " " }

    #Add the count
    row << sprintf("| %#{count_width}d\n", x[2])

    #Append the finished row onto the histogram
    histogram << row
  end

  #End the table
  histogram << skip_row(value_width) if disp_buckets.last[0] != bucket_count-1
  histogram << sprintf("%#{value_width}s", "Total")
  histogram << " |"
  max_bar_width.times {histogram << "-"}
  histogram << "| "
  histogram << sprintf("%#{count_width}d\n", total)
end