-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
95 lines (74 loc) · 1.55 KB
/
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
package prom_test
import (
"context"
"strconv"
"testing"
"github.com/bool64/prom-stats"
"github.com/bool64/stats"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)
func BenchmarkTracker_Add(b *testing.B) {
registry := prometheus.NewRegistry()
tr, err := prom.NewStatsTracker(registry)
require.NoError(b, err)
ctx := context.Background()
ctx = stats.AddKeysAndValues(ctx, "foo", "bar")
var lv []string
for i := 0; i < 20; i++ {
lv = append(lv, "some-val"+strconv.Itoa(i))
}
b.ReportAllocs()
b.ResetTimer()
sem := make(chan struct{}, 50)
for i := 0; i < b.N; i++ {
i := i
sem <- struct{}{}
go func() {
defer func() {
<-sem
}()
tr.Add(ctx, "some.action.count", 1,
"name", lv[i%10],
"other", lv[i%20],
)
}()
}
for i := 0; i < cap(sem); i++ {
sem <- struct{}{}
}
}
func BenchmarkRawPrometheus(b *testing.B) {
registry := prometheus.NewRegistry()
counter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Help: "Some action",
Name: "namespace_some_action_total",
},
[]string{"name", "other"},
)
registry.MustRegister(counter)
sem := make(chan struct{}, 50)
var lv []string
for i := 0; i < 20; i++ {
lv = append(lv, "some-val"+strconv.Itoa(i))
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
i := i
sem <- struct{}{}
go func() {
defer func() {
<-sem
}()
counter.With(prometheus.Labels{
"name": lv[i%10],
"other": lv[i%20],
}).Add(1)
}()
}
for i := 0; i < cap(sem); i++ {
sem <- struct{}{}
}
}