This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathbenchmark_test.go
356 lines (277 loc) · 7.73 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"testing"
elektra "github.com/ElektraInitiative/libelektra/src/bindings/go-elektra/kdb"
)
type prepareFunc func(t testing.TB, handle elektra.KDB, ks elektra.KeySet, parentKey elektra.Key, runs int)
func Benchmark(b *testing.B) {
world := "world"
benchmarks := []struct {
verb string
path string
body func(v2 bool, index int) interface{}
indexKeys bool
}{
{
verb: "GET",
path: "/version",
},
{
verb: "GET",
path: "/kdb/user:/tests/go/elektrad/benchmark/get",
},
{
verb: "GET",
path: "/kdbFind/user:/tests/go/elektrad/benchmark/get/001",
},
{
verb: "POST",
path: "/kdbMv/user:/tests/go/elektrad/benchmark/post/mv/from",
body: func(v2 bool, i int) interface{} {
if v2 {
return indexedKeyName("/post/mv/to/v2", i)
}
return indexedKeyName("/post/mv/to/v1", i)
},
indexKeys: true,
},
{
verb: "POST",
path: "/kdbMeta/user:/tests/go/elektrad/benchmark/post/meta",
body: func(_ bool, _ int) interface{} {
return keyValueBody{Key: "hello", Value: &world}
},
},
{
verb: "PUT",
path: "/kdb/user:/tests/go/elektrad/benchmark/put",
body: func(_ bool, _ int) interface{} {
return "value"
},
indexKeys: true,
},
{
verb: "DELETE",
path: "/kdb/user:/tests/go/elektrad/benchmark/delete/kdb",
indexKeys: true,
},
{
verb: "DELETE",
path: "/kdbMeta/user:/tests/go/elektrad/benchmark/delete/meta",
body: func(_ bool, _ int) interface{} {
return keyValueBody{Key: "hello"}
},
indexKeys: true,
},
}
prepareFuncs := []prepareFunc{
prepareGetKdb,
preparePostKdbMv,
preparePostKdbMeta,
prepareDeleteMeta,
prepareDeleteKdb,
}
cleanup := prepareBenchmark(b, prepareFuncs)
defer cleanup()
for _, bt := range benchmarks {
run := func(b *testing.B, url string, v2 bool) {
b.StopTimer()
b.ResetTimer()
cookie := getCookie(b, url)
if b.N == 1 {
return
}
for n := 0; n < b.N; n++ {
path := bt.path
if bt.indexKeys {
if v2 {
path = indexedKey(path+"/v2", n)
} else {
path = indexedKey(path+"/v1", n)
}
}
var requestBody interface{}
if bt.body != nil {
requestBody = bt.body(v2, n)
}
r := benchRequest(b, bt.verb, url, path, requestBody, v2)
if cookie != nil {
r.AddCookie(cookie)
}
b.StartTimer()
resp, err := http.DefaultClient.Do(r)
b.StopTimer()
Check(b, err, "request failed")
responseBody, _ := ioutil.ReadAll(resp.Body)
Assertf(b, resp.StatusCode >= 200 && resp.StatusCode < 300, "unexpected status code for path %s: %d, %s", path, resp.StatusCode, responseBody)
resp.Body.Close()
}
}
b.Run(fmt.Sprintf("%s%s", bt.verb, bt.path), func(b *testing.B) {
b.Run("v1", func(b *testing.B) {
run(b, "http://localhost:33333", false)
})
b.Run("v2", func(b *testing.B) {
run(b, "http://localhost:8080", true)
})
})
}
}
func stringReader(body string) io.Reader {
return bytes.NewBuffer([]byte(body))
}
func jsonReader(body interface{}) io.Reader {
b, err := json.Marshal(body)
if err != nil {
panic("could not marshal json")
}
return bytes.NewBuffer(b)
}
func getCookie(b testing.TB, url string) *http.Cookie {
r, err := http.NewRequest("GET", url+"/version", nil)
Check(b, err, "error creating cookie request")
resp, err := http.DefaultClient.Do(r)
Check(b, err, "error getting cookie")
cookies := resp.Cookies()
if len(cookies) > 0 {
return cookies[0]
}
return nil
}
func getTestHandle(t testing.TB) elektra.KDB {
handle := elektra.New()
err := handle.Open()
if err != nil {
t.Fatal(err)
}
return handle
}
func benchRequest(b *testing.B, verb, url, path string, body interface{}, v2 bool) *http.Request {
b.Helper()
contentType := "application/json"
var bodyReader io.Reader
if body != nil {
switch b := body.(type) {
case string:
if v2 {
bodyReader = jsonReader(body)
} else {
// the old elektra doesn't like JSON strings in the body
contentType = "text/plain"
bodyReader = stringReader(b)
}
default:
bodyReader = jsonReader(body)
}
}
r, err := http.NewRequest(verb, url+path, bodyReader)
r.Header.Add("Content-Type", contentType)
Check(b, err, "error creating request")
return r
}
func clear(b testing.TB, ks elektra.KeySet, keyName string) {
key, err := elektra.NewKey(keyName)
Check(b, err, "could not create clear key")
ks.Cut(key)
}
func get(b testing.TB, handle elektra.KDB, ks elektra.KeySet, parentKey elektra.Key) {
b.Helper()
_, err := handle.Get(ks, parentKey)
Check(b, err, "could not get test dataset")
}
func persist(b testing.TB, handle elektra.KDB, ks elektra.KeySet, parentKey elektra.Key) {
b.Helper()
_, err := handle.Set(ks, parentKey)
Check(b, err, "could not create test dataset")
}
func create(b testing.TB, ks elektra.KeySet, keyName string, count int) {
for n := 0; n < count; n++ {
k, err := elektra.NewKey(indexedKeyName(keyName, n))
Check(b, err, "could not create data key")
ks.AppendKey(k)
}
}
var (
namespace = "user:/tests/go/elektrad/benchmark"
data = "/get"
dataSize = 100000
)
func indexedKey(keyName string, i int) string {
return fmt.Sprintf(keyName+"/%06d", i)
}
func indexedKeyName(keyName string, i int) string {
return indexedKey(namespace+keyName, i)
}
func prepareBenchmark(b *testing.B, prepareFuncs []prepareFunc) func() {
b.Helper()
runs := 10 // b.N
// we have to add a fixed value because the first b.N call is always "1"
// for initialization reasons.
handle := getTestHandle(b)
ks := elektra.NewKeySet()
parentKey, err := elektra.NewKey(namespace)
Check(b, err, "could not create parent key")
get(b, handle, ks, parentKey)
clear(b, ks, data)
for _, f := range prepareFuncs {
f(b, handle, ks, parentKey, runs)
}
persist(b, handle, ks, parentKey)
return func() {
cleanup(b, handle, ks, parentKey)
}
}
func prepareGetKdb(b testing.TB, handle elektra.KDB, ks elektra.KeySet, parentKey elektra.Key, _ int) {
create(b, ks, data, dataSize)
}
func preparePostKdbMv(b testing.TB, handle elektra.KDB, ks elektra.KeySet, parentKey elektra.Key, runs int) {
for i := 0; i < runs; i++ {
create(b, ks, indexedKey("/post/mv/from/v1", i), 10)
}
for i := 0; i < runs; i++ {
create(b, ks, indexedKey("/post/mv/from/v2", i), 10)
}
}
func prepareDeleteKdb(b testing.TB, handle elektra.KDB, ks elektra.KeySet, parentKey elektra.Key, runs int) {
for i := 0; i < runs; i++ {
k1, err := elektra.NewKey(indexedKeyName("/delete/kdb/v1", i))
Check(b, err, "could not create data key")
k2, err := elektra.NewKey(indexedKeyName("/delete/kdb/v2", i))
Check(b, err, "could not create data key")
ks.AppendKey(k1)
ks.AppendKey(k2)
}
for i := 0; i < runs; i++ {
k, err := elektra.NewKey(indexedKeyName("/delete/kdb", i))
Check(b, err, "could not create data key")
ks.AppendKey(k)
}
}
func preparePostKdbMeta(b testing.TB, handle elektra.KDB, ks elektra.KeySet, parentKey elektra.Key, _ int) {
k, err := elektra.NewKey(namespace + "/post/meta")
Check(b, err, "could not create data key")
ks.AppendKey(k)
}
func prepareDeleteMeta(b testing.TB, handle elektra.KDB, ks elektra.KeySet, parentKey elektra.Key, runs int) {
for i := 0; i < runs; i++ {
key1, err := elektra.NewKey(indexedKeyName("/delete/meta/v1", i))
Check(b, err, "could not create data key")
key2, err := elektra.NewKey(indexedKeyName("/delete/meta/v2", i))
Check(b, err, "could not create data key")
ks.AppendKey(key1)
ks.AppendKey(key2)
key1.SetMeta("hello", "world")
key2.SetMeta("hello", "world")
}
}
func cleanup(b testing.TB, handle elektra.KDB, ks elektra.KeySet, parentKey elektra.Key) {
get(b, handle, ks, parentKey)
clear(b, ks, namespace)
persist(b, handle, ks, parentKey)
}