-
Notifications
You must be signed in to change notification settings - Fork 12
/
bench_test.go
160 lines (143 loc) · 3.81 KB
/
bench_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
157
158
159
160
package remotewrite
import (
"context"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/metrics"
)
func BenchmarkCompileTemplatesSimple(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := compileTemplate("something ${series_id} else")
require.NoError(b, err)
}
}
func BenchmarkCompileTemplatesComplex(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := compileTemplate("something ${series_id/1000} else")
require.NoError(b, err)
}
}
func BenchmarkEvaluateTemplatesSimple(b *testing.B) {
t, err := compileTemplate("something ${series_id} else")
require.NoError(b, err)
b.ResetTimer()
var buf []byte
for i := 0; i < b.N; i++ {
buf = t.AppendByte(buf[:0], i)
}
}
func BenchmarkEvaluateTemplatesComplex(b *testing.B) {
t, err := compileTemplate("something ${series_id/1000} else")
require.NoError(b, err)
b.ResetTimer()
var buf []byte
for i := 0; i < b.N; i++ {
buf = t.AppendByte(buf[:0], i)
}
}
var benchmarkLabels = map[string]string{
"__name__": "k6_generated_metric_${series_id/1000}",
"series_id": "${series_id}",
"cardinality_1e1": "${series_id/10}",
"cardinality_1e2": "${series_id/100}",
"cardinality_1e3": "${series_id/1000}",
"cardinality_1e4": "${series_id/10000}",
"cardinality_1e5": "${series_id/100000}",
"cardinality_1e6": "${series_id/1000000}",
"cardinality_1e7": "${series_id/10000000}",
"cardinality_2": "${series_id%2}",
"cardinality_50": "${series_id%50}",
}
type testServer struct {
server *httptest.Server
vu *modulestest.VU
count *int64
}
func newTestServer(tb testing.TB) *testServer {
ts := &testServer{
count: new(int64),
}
ts.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
w.WriteHeader(200)
atomic.AddInt64(ts.count, 1)
}))
registry := metrics.NewRegistry()
ch := make(chan metrics.SampleContainer)
tb.Cleanup(func() {
ts.server.Close()
close(ch) // this might need to be elsewhere
})
ts.vu = new(modulestest.VU)
ts.vu.CtxField = context.Background()
ts.vu.StateField = new(lib.State)
ts.vu.StateField.Transport = ts.server.Client().Transport
ts.vu.StateField.BufferPool = lib.NewBufferPool()
ts.vu.StateField.Samples = ch
ts.vu.StateField.BuiltinMetrics = metrics.RegisterBuiltinMetrics(registry)
ts.vu.StateField.Tags = lib.NewVUStateTags(registry.RootTagSet())
go func() {
for range ch {
}
}()
return ts
}
func BenchmarkStoreFromPrecompiledTemplates(b *testing.B) {
s := newTestServer(b)
c := &Client{
client: &http.Client{},
cfg: &Config{
Url: s.server.URL,
Timeout: "100s",
},
vu: s.vu,
}
template, err := compileLabelTemplates(benchmarkLabels)
require.NoError(b, err)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := c.StoreFromPrecompiledTemplates(i, i+10, int64(i), 0, 100000, template)
require.NoError(b, err)
}
require.True(b, 1 <= *s.count) // this might need an atomic
}
func BenchmarkStoreFromTemplates(b *testing.B) {
s := newTestServer(b)
c := &Client{
client: &http.Client{},
cfg: &Config{
Url: s.server.URL,
Timeout: "100s",
},
vu: s.vu,
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := c.StoreFromTemplates(i, i+10, int64(i), 0, 100000, benchmarkLabels)
require.NoError(b, err)
}
require.True(b, 1 <= *s.count) // this might need an atomic
}
func BenchmarkGenerateFromPrecompiledTemplates(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
r := rand.New(rand.NewSource(time.Now().Unix()))
i := 0
template, err := compileLabelTemplates(benchmarkLabels)
require.NoError(b, err)
for pb.Next() {
i++
_ = generateFromPrecompiledTemplates(r, i, i+10, int64(i), 0, 100000, template)
}
})
}