histogram.go 742 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package mock
  2. import (
  3. "sort"
  4. "sync"
  5. "time"
  6. "github.com/ydb-platform/ydb/library/go/core/metrics"
  7. "go.uber.org/atomic"
  8. )
  9. var (
  10. _ metrics.Histogram = (*Histogram)(nil)
  11. _ metrics.Timer = (*Histogram)(nil)
  12. )
  13. type Histogram struct {
  14. Name string
  15. Tags map[string]string
  16. BucketBounds []float64
  17. BucketValues []int64
  18. InfValue *atomic.Int64
  19. mutex sync.Mutex
  20. }
  21. func (h *Histogram) RecordValue(value float64) {
  22. boundIndex := sort.SearchFloat64s(h.BucketBounds, value)
  23. if boundIndex < len(h.BucketValues) {
  24. h.mutex.Lock()
  25. h.BucketValues[boundIndex] += 1
  26. h.mutex.Unlock()
  27. } else {
  28. h.InfValue.Inc()
  29. }
  30. }
  31. func (h *Histogram) RecordDuration(value time.Duration) {
  32. h.RecordValue(value.Seconds())
  33. }