aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/mock/histogram.go
blob: 734d7b5f88d5f65c0489bdd0073fb20732738662 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package mock

import (
	"sort"
	"sync"
	"time"

	"github.com/ydb-platform/ydb/library/go/core/metrics"
	"go.uber.org/atomic"
)

var (
	_ metrics.Histogram = (*Histogram)(nil)
	_ metrics.Timer     = (*Histogram)(nil)
)

type Histogram struct {
	Name         string
	Tags         map[string]string
	BucketBounds []float64
	BucketValues []int64
	InfValue     *atomic.Int64
	mutex        sync.Mutex
}

func (h *Histogram) RecordValue(value float64) {
	boundIndex := sort.SearchFloat64s(h.BucketBounds, value)

	if boundIndex < len(h.BucketValues) {
		h.mutex.Lock()
		h.BucketValues[boundIndex] += 1
		h.mutex.Unlock()
	} else {
		h.InfValue.Inc()
	}
}

func (h *Histogram) RecordDuration(value time.Duration) {
	h.RecordValue(value.Seconds())
}