1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // This file implements histogramming for RPC statistics collection.
17 "golang.org/x/net/internal/timeseries"
24 // histogram keeps counts of values in buckets that are spaced
25 // out in powers of 2: 0-1, 2-3, 4-7...
26 // histogram implements timeseries.Observable
27 type histogram struct {
28 sum int64 // running total of measurements
29 sumOfSquares float64 // square of running total
30 buckets []int64 // bucketed values for histogram
31 value int // holds a single value as an optimization
32 valueCount int64 // number of values recorded for single value
35 // AddMeasurement records a value measurement observation to the histogram.
36 func (h *histogram) addMeasurement(value int64) {
37 // TODO: assert invariant
39 h.sumOfSquares += float64(value) * float64(value)
41 bucketIndex := getBucket(value)
43 if h.valueCount == 0 || (h.valueCount > 0 && h.value == bucketIndex) {
48 h.buckets[bucketIndex]++
52 func (h *histogram) allocateBuckets() {
54 h.buckets = make([]int64, bucketCount)
55 h.buckets[h.value] = h.valueCount
61 func log2(i int64) int {
63 for ; i >= 0x100; i >>= 8 {
66 for ; i > 0; i >>= 1 {
72 func getBucket(i int64) (index int) {
77 if index >= bucketCount {
78 index = bucketCount - 1
83 // Total returns the number of recorded observations.
84 func (h *histogram) total() (total int64) {
85 if h.valueCount >= 0 {
88 for _, val := range h.buckets {
94 // Average returns the average value of recorded observations.
95 func (h *histogram) average() float64 {
100 return float64(h.sum) / float64(t)
103 // Variance returns the variance of recorded observations.
104 func (h *histogram) variance() float64 {
105 t := float64(h.total())
109 s := float64(h.sum) / t
110 return h.sumOfSquares/t - s*s
113 // StandardDeviation returns the standard deviation of recorded observations.
114 func (h *histogram) standardDeviation() float64 {
115 return math.Sqrt(h.variance())
118 // PercentileBoundary estimates the value that the given fraction of recorded
119 // observations are less than.
120 func (h *histogram) percentileBoundary(percentile float64) int64 {
123 // Corner cases (make sure result is strictly less than Total())
126 } else if total == 1 {
127 return int64(h.average())
130 percentOfTotal := round(float64(total) * percentile)
131 var runningTotal int64
133 for i := range h.buckets {
134 value := h.buckets[i]
135 runningTotal += value
136 if runningTotal == percentOfTotal {
137 // We hit an exact bucket boundary. If the next bucket has data, it is a
138 // good estimate of the value. If the bucket is empty, we interpolate the
139 // midpoint between the next bucket's boundary and the next non-zero
140 // bucket. If the remaining buckets are all empty, then we use the
141 // boundary for the next bucket as the estimate.
143 min := bucketBoundary(j)
144 if runningTotal < total {
145 for h.buckets[j] == 0 {
149 max := bucketBoundary(j)
150 return min + round(float64(max-min)/2)
151 } else if runningTotal > percentOfTotal {
152 // The value is in this bucket. Interpolate the value.
153 delta := runningTotal - percentOfTotal
154 percentBucket := float64(value-delta) / float64(value)
155 bucketMin := bucketBoundary(uint8(i))
156 nextBucketMin := bucketBoundary(uint8(i + 1))
157 bucketSize := nextBucketMin - bucketMin
158 return bucketMin + round(percentBucket*float64(bucketSize))
161 return bucketBoundary(bucketCount - 1)
164 // Median returns the estimated median of the observed values.
165 func (h *histogram) median() int64 {
166 return h.percentileBoundary(0.5)
169 // Add adds other to h.
170 func (h *histogram) Add(other timeseries.Observable) {
171 o := other.(*histogram)
172 if o.valueCount == 0 {
173 // Other histogram is empty
174 } else if h.valueCount >= 0 && o.valueCount > 0 && h.value == o.value {
175 // Both have a single bucketed value, aggregate them
176 h.valueCount += o.valueCount
178 // Two different values necessitate buckets in this histogram
180 if o.valueCount >= 0 {
181 h.buckets[o.value] += o.valueCount
183 for i := range h.buckets {
184 h.buckets[i] += o.buckets[i]
188 h.sumOfSquares += o.sumOfSquares
192 // Clear resets the histogram to an empty state, removing all observed values.
193 func (h *histogram) Clear() {
201 // CopyFrom copies from other, which must be a *histogram, into h.
202 func (h *histogram) CopyFrom(other timeseries.Observable) {
203 o := other.(*histogram)
204 if o.valueCount == -1 {
206 copy(h.buckets, o.buckets)
209 h.sumOfSquares = o.sumOfSquares
211 h.valueCount = o.valueCount
214 // Multiply scales the histogram by the specified ratio.
215 func (h *histogram) Multiply(ratio float64) {
216 if h.valueCount == -1 {
217 for i := range h.buckets {
218 h.buckets[i] = int64(float64(h.buckets[i]) * ratio)
221 h.valueCount = int64(float64(h.valueCount) * ratio)
223 h.sum = int64(float64(h.sum) * ratio)
224 h.sumOfSquares = h.sumOfSquares * ratio
227 // New creates a new histogram.
228 func (h *histogram) New() timeseries.Observable {
234 func (h *histogram) String() string {
235 return fmt.Sprintf("%d, %f, %d, %d, %v",
236 h.sum, h.sumOfSquares, h.value, h.valueCount, h.buckets)
239 // round returns the closest int64 to the argument
240 func round(in float64) int64 {
241 return int64(math.Floor(in + 0.5))
244 // bucketBoundary returns the first value in the bucket.
245 func bucketBoundary(bucket uint8) int64 {
252 // bucketData holds data about a specific bucket for use in distTmpl.
253 type bucketData struct {
256 Pct, CumulativePct float64
260 // data holds data about a Distribution for use in distTmpl.
262 Buckets []*bucketData
264 Mean, StandardDeviation float64
267 // maxHTMLBarWidth is the maximum width of the HTML bar for visualizing buckets.
268 const maxHTMLBarWidth = 350.0
270 // newData returns data representing h for use in distTmpl.
271 func (h *histogram) newData() *data {
272 // Force the allocation of buckets to simplify the rendering implementation
274 // We scale the bars on the right so that the largest bar is
275 // maxHTMLBarWidth pixels in width.
276 maxBucket := int64(0)
277 for _, n := range h.buckets {
283 barsizeMult := maxHTMLBarWidth / float64(maxBucket)
288 pctMult = 100.0 / float64(total)
291 buckets := make([]*bucketData, len(h.buckets))
292 runningTotal := int64(0)
293 for i, n := range h.buckets {
299 if i < bucketCount-1 {
300 upperBound = bucketBoundary(uint8(i + 1))
302 upperBound = math.MaxInt64
304 buckets[i] = &bucketData{
305 Lower: bucketBoundary(uint8(i)),
308 Pct: float64(n) * pctMult,
309 CumulativePct: float64(runningTotal) * pctMult,
310 GraphWidth: int(float64(n) * barsizeMult),
318 StandardDeviation: h.standardDeviation(),
322 func (h *histogram) html() template.HTML {
323 buf := new(bytes.Buffer)
324 if err := distTmpl().Execute(buf, h.newData()); err != nil {
326 log.Printf("net/trace: couldn't execute template: %v", err)
328 return template.HTML(buf.String())
331 var distTmplCache *template.Template
332 var distTmplOnce sync.Once
334 func distTmpl() *template.Template {
335 distTmplOnce.Do(func() {
337 distTmplCache = template.Must(template.New("distTmpl").Parse(`
340 <td style="padding:0.25em">Count: {{.Count}}</td>
341 <td style="padding:0.25em">Mean: {{printf "%.0f" .Mean}}</td>
342 <td style="padding:0.25em">StdDev: {{printf "%.0f" .StandardDeviation}}</td>
343 <td style="padding:0.25em">Median: {{.Median}}</td>
348 {{range $b := .Buckets}}
351 <td style="padding:0 0 0 0.25em">[</td>
352 <td style="text-align:right;padding:0 0.25em">{{.Lower}},</td>
353 <td style="text-align:right;padding:0 0.25em">{{.Upper}})</td>
354 <td style="text-align:right;padding:0 0.25em">{{.N}}</td>
355 <td style="text-align:right;padding:0 0.25em">{{printf "%#.3f" .Pct}}%</td>
356 <td style="text-align:right;padding:0 0.25em">{{printf "%#.3f" .CumulativePct}}%</td>
357 <td><div style="background-color: blue; height: 1em; width: {{.GraphWidth}};"></div></td>