-
Notifications
You must be signed in to change notification settings - Fork 7
/
mito_bench_test.go
391 lines (381 loc) · 10.3 KB
/
mito_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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package mito
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/elastic/mito/lib"
"github.com/google/cel-go/cel"
)
var (
fastMarshal = flag.Bool("fast_marshal", false, "specify fast/non-pretty marshaling of the result")
sampleBench = flag.Bool("sample_bench", false, "log one benchmark result for each benchmark")
)
var benchmarks = []struct {
name string
setup func(*testing.B, bool) (prg cel.Program, ast *cel.Ast, state any, err error)
}{
// Self-contained.
{
name: "hello_world_static",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(
`"hello world"`,
root,
fold,
false,
false,
)
return prg, ast, nil, err
},
},
{
name: "hello_world_object_static",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(
`{"greeting":"hello world"}`,
root,
fold,
false,
false,
)
return prg, ast, nil, err
},
},
{
name: "nested_static",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(
`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`,
root,
fold,
false,
false,
)
return prg, ast, nil, err
},
},
{
name: "encode_json_static",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(
`{"a":{"b":{"c":{"d":{"e":"f"}}}}}.encode_json()`,
root,
fold,
false,
false,
lib.JSON(nil),
)
return prg, ast, nil, err
},
},
{
name: "nested_collate_static",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(
`{"a":{"b":{"c":{"d":{"e":"f"}}}}}.collate("a.b.c.d.e")`,
root,
fold,
false,
false,
lib.Collections(),
)
return prg, ast, nil, err
},
},
// From state.
{
name: "hello_world_state",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(root, root, fold, false, false)
state := map[string]any{root: "hello world"}
return prg, ast, state, err
},
},
{
name: "hello_world_object_state",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(
`{"greeting":state.greeting}`,
root,
fold,
false,
false,
)
state := map[string]any{root: mustParseJSON(`{"greeting": "hello world}"}`)}
return prg, ast, state, err
},
},
{
name: "nested_state",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(root, root, fold, false, false)
state := map[string]any{root: mustParseJSON(`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`)}
return prg, ast, state, err
},
},
{
name: "encode_json_state",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(`state.encode_json()`,
root,
fold,
false,
false,
lib.JSON(nil),
)
state := map[string]any{root: mustParseJSON(`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`)}
return prg, ast, state, err
},
},
// These two have additional complexity due to a wart that requires
// that we elevate the value to get the environment to know that state
// is a map.
//
// There should be an easier way to do this. It has come up in DX discussions.
//
// Similar in the net versions below.
{
name: "nested_collate_list_state",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(`[state].collate("a.b.c.d.e")`,
root,
fold,
false,
false,
lib.Collections(),
)
state := map[string]any{root: mustParseJSON(`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`)}
return prg, ast, state, err
},
},
{
name: "nested_collate_map_state",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
prg, ast, _, err := compile(`{"state": state}.collate("state.a.b.c.d.e")`,
root,
fold,
false,
false,
lib.Collections(),
)
state := map[string]any{root: mustParseJSON(`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`)}
return prg, ast, state, err
},
},
// From net.
{
// null_net just does a GET and uses the result in the cheapest way.
// This is to get an idea of how much of the bench work is coming from
// the test server.
name: "null_net",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
}))
b.Cleanup(func() { srv.Close() })
prg, ast, _, err := compile(
fmt.Sprintf(`get(%q).size()`, srv.URL),
root,
fold,
false,
false,
lib.HTTP(srv.Client(), nil, nil),
)
return prg, ast, nil, err
},
},
{
name: "hello_world_net",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("hello world"))
}))
b.Cleanup(func() { srv.Close() })
prg, ast, _, err := compile(
fmt.Sprintf(`string(get(%q).Body)`, srv.URL),
root,
fold,
false,
false,
lib.HTTP(srv.Client(), nil, nil),
)
return prg, ast, nil, err
},
},
{
name: "hello_world_object_net",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{"greeting":"hello world"}`))
}))
b.Cleanup(func() { srv.Close() })
prg, ast, _, err := compile(
fmt.Sprintf(`{"greeting":bytes(get(%q).Body).decode_json().greeting}`, srv.URL),
root,
fold,
false,
false,
lib.HTTP(srv.Client(), nil, nil),
lib.JSON(nil),
)
return prg, ast, nil, err
},
},
{
name: "nested_net",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`))
}))
b.Cleanup(func() { srv.Close() })
prg, ast, _, err := compile(
fmt.Sprintf(`bytes(get(%q).Body).decode_json()`, srv.URL),
root,
fold,
false,
false,
lib.HTTP(srv.Client(), nil, nil),
lib.JSON(nil),
)
return prg, ast, nil, err
},
},
{
name: "encode_json_null_net",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`))
}))
b.Cleanup(func() { srv.Close() })
prg, ast, _, err := compile(
fmt.Sprintf(`get(%q).Body`, srv.URL),
root,
fold,
false,
false,
lib.HTTP(srv.Client(), nil, nil),
lib.JSON(nil),
)
return prg, ast, nil, err
},
},
{
// encode_json_net should bead assessed with reference to encode_json_null_net
// which performs the same request but does not round-trip the object.
name: "encode_json_net",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`))
}))
b.Cleanup(func() { srv.Close() })
prg, ast, _, err := compile(
fmt.Sprintf(`bytes(get(%q).Body).decode_json().encode_json()`, srv.URL),
root,
fold,
false,
false,
lib.HTTP(srv.Client(), nil, nil),
lib.JSON(nil),
)
return prg, ast, nil, err
},
},
{
name: "nested_collate_list_net",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`))
}))
b.Cleanup(func() { srv.Close() })
prg, ast, _, err := compile(
fmt.Sprintf(`[bytes(get(%q).Body).decode_json()].collate("a.b.c.d.e")`, srv.URL),
root,
fold,
false,
false,
lib.HTTP(srv.Client(), nil, nil),
lib.JSON(nil),
lib.Collections(),
)
return prg, ast, nil, err
},
},
{
name: "nested_collate_map_net",
setup: func(b *testing.B, fold bool) (cel.Program, *cel.Ast, any, error) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`{"a":{"b":{"c":{"d":{"e":"f"}}}}}`))
}))
b.Cleanup(func() { srv.Close() })
prg, ast, _, err := compile(
fmt.Sprintf(`{"body": bytes(get(%q).Body).decode_json()}.collate("body.a.b.c.d.e")`, srv.URL),
root,
fold,
false,
false,
lib.HTTP(srv.Client(), nil, nil),
lib.JSON(nil),
lib.Collections(),
)
return prg, ast, nil, err
},
},
}
func BenchmarkMito(b *testing.B) {
for _, bench := range benchmarks {
b.Run(bench.name, func(b *testing.B) {
for _, fold := range []bool{false, true} {
name := "unfolded"
if fold {
name = "folded"
}
b.Run(name, func(b *testing.B) {
sampled := false
b.StopTimer()
prg, ast, state, err := bench.setup(b, fold)
if err != nil {
b.Fatalf("failed setup: %v", err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
v, _, _, err := run(prg, ast, *fastMarshal, state)
if err != nil {
b.Fatalf("failed operation: %v", err)
}
if *sampleBench && !sampled {
sampled = true
b.Logf("\n%s", v)
}
}
})
}
})
}
}
func mustParseJSON(s string) any {
var v any
err := json.Unmarshal([]byte(s), &v)
if err != nil {
panic(err)
}
return v
}