window.go 1013 B

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