forked from HdrHistogram/hdrhistogram-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdr_benchmark_test.go
156 lines (140 loc) · 3.77 KB
/
hdr_benchmark_test.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package hdrhistogram_test
import (
hdrhistogram "github.com/HdrHistogram/hdrhistogram-go"
"gonum.org/v1/gonum/stat/distuv"
"math"
"math/rand"
"testing"
)
// nolint
func BenchmarkHistogramRecordValue(b *testing.B) {
h := hdrhistogram.New(1, 10000000, 3)
for i := 0; i < 1000000; i++ {
if err := h.RecordValue(int64(i)); err != nil {
b.Fatal(err)
}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
h.RecordValue(100)
}
}
func BenchmarkNew(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
hdrhistogram.New(1, 120000, 3) // this could track 1ms-2min
}
}
// nolint
func BenchmarkHistogramValueAtPercentile(b *testing.B) {
rand.Seed(12345)
var highestTrackableValue int64 = 1000000
var lowestDiscernibleValue int64 = 1
var sigfigs = 3
var totalDatapoints = 1000000
h, data := populateHistogramLogNormalDist(b, lowestDiscernibleValue, highestTrackableValue, sigfigs, totalDatapoints)
quantiles := make([]float64, totalDatapoints)
for i := range quantiles {
data[i] = rand.Float64() * 100.0
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
h.ValueAtPercentile(data[i%totalDatapoints])
}
}
// nolint
func BenchmarkHistogramValueAtPercentileGivenPercentileSlice(b *testing.B) {
rand.Seed(12345)
var highestTrackableValue int64 = 1000000
var lowestDiscernibleValue int64 = 1
var sigfigs = 3
var totalDatapoints = 1000000
h, data := populateHistogramLogNormalDist(b, lowestDiscernibleValue, highestTrackableValue, sigfigs, totalDatapoints)
quantiles := make([]float64, b.N)
for i := range quantiles {
data[i] = rand.Float64() * 100.0
}
percentilesOfInterest := []float64{50.0, 95.0, 99.0, 99.9}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, percentile := range percentilesOfInterest {
h.ValueAtPercentile(percentile)
}
}
}
// nolint
func BenchmarkHistogramValueAtPercentilesGivenPercentileSlice(b *testing.B) {
rand.Seed(12345)
var highestTrackableValue int64 = 1000000
var lowestDiscernibleValue int64 = 1
var sigfigs = 3
var totalDatapoints = 1000000
h, data := populateHistogramLogNormalDist(b, lowestDiscernibleValue, highestTrackableValue, sigfigs, totalDatapoints)
quantiles := make([]float64, b.N)
for i := range quantiles {
data[i] = rand.Float64() * 100.0
}
percentilesOfInterest := []float64{50.0, 95.0, 99.0, 99.9}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
h.ValueAtPercentiles(percentilesOfInterest)
}
}
func BenchmarkWindowedHistogramRecordAndRotate(b *testing.B) {
w := hdrhistogram.NewWindowed(3, 1, 10000000, 3)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := w.Current.RecordValue(100); err != nil {
b.Fatal(err)
}
if i%100000 == 1 {
w.Rotate()
}
}
}
func BenchmarkWindowedHistogramMerge(b *testing.B) {
w := hdrhistogram.NewWindowed(3, 1, 10000000, 3)
for i := 0; i < 10000000; i++ {
if err := w.Current.RecordValue(100); err != nil {
b.Fatal(err)
}
if i%100000 == 1 {
w.Rotate()
}
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Merge()
}
}
func populateHistogramLogNormalDist(b *testing.B, lowestDiscernibleValue int64, highestTrackableValue int64, sigfigs int, totalDatapoints int) (*hdrhistogram.Histogram, []float64) {
dist := distuv.LogNormal{Mu: 0.0, Sigma: 0.5}
h := hdrhistogram.New(lowestDiscernibleValue, highestTrackableValue, sigfigs)
data := make([]float64, totalDatapoints)
// Draw some random values from the lognormal distribution
min := math.MaxFloat64
max := 0.0
for i := range data {
data[i] = dist.Rand()
if data[i] < min {
min = data[i]
}
if data[i] > max {
max = data[i]
}
}
k := float64(highestTrackableValue) / (max - min)
for i := range data {
v := k * data[i]
if err := h.RecordValue(int64(v)); err != nil {
b.Fatal(err)
}
}
return h, data
}