OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / codahale / hdrhistogram / window.go
1 package hdrhistogram
2
3 // A WindowedHistogram combines histograms to provide windowed statistics.
4 type WindowedHistogram struct {
5         idx int
6         h   []Histogram
7         m   *Histogram
8
9         Current *Histogram
10 }
11
12 // NewWindowed creates a new WindowedHistogram with N underlying histograms with
13 // the given parameters.
14 func NewWindowed(n int, minValue, maxValue int64, sigfigs int) *WindowedHistogram {
15         w := WindowedHistogram{
16                 idx: -1,
17                 h:   make([]Histogram, n),
18                 m:   New(minValue, maxValue, sigfigs),
19         }
20
21         for i := range w.h {
22                 w.h[i] = *New(minValue, maxValue, sigfigs)
23         }
24         w.Rotate()
25
26         return &w
27 }
28
29 // Merge returns a histogram which includes the recorded values from all the
30 // sections of the window.
31 func (w *WindowedHistogram) Merge() *Histogram {
32         w.m.Reset()
33         for _, h := range w.h {
34                 w.m.Merge(&h)
35         }
36         return w.m
37 }
38
39 // Rotate resets the oldest histogram and rotates it to be used as the current
40 // histogram.
41 func (w *WindowedHistogram) Rotate() {
42         w.idx++
43         w.Current = &w.h[w.idx%len(w.h)]
44         w.Current.Reset()
45 }