-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbench_test.go
More file actions
116 lines (107 loc) · 2.73 KB
/
bench_test.go
File metadata and controls
116 lines (107 loc) · 2.73 KB
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
package gnata_test
import (
"context"
"encoding/json"
"testing"
"github.com/recolabs/gnata"
)
const (
benchData = `{
"Account": {
"Name": "Firefly",
"Order": [
{"OrderID": "order103", "Product": [
{"SKU": "0406654608", "Description": "Bowler Hat", "UnitPrice": 68.45, "Quantity": 2, "Discount": 0.1},
{"SKU": "040657863", "Description": "Cloak", "UnitPrice": 107.99, "Quantity": 1, "Discount": 0.2}
]},
{"OrderID": "order104", "Product": [
{"SKU": "0406654608", "Description": "Bowler Hat", "UnitPrice": 68.45, "Quantity": 4, "Discount": 0.1},
{"SKU": "0406654603", "Description": "Trilby", "UnitPrice": 21.67, "Quantity": 1, "Discount": 0.0}
]}
]
}
}`
)
var benchExprs = []string{
"Account.Name",
"Account.Order.Product.SKU",
"Account.Order.Product[UnitPrice > 50].SKU",
"$sum(Account.Order.Product.(UnitPrice * Quantity * (1 - Discount)))",
}
func BenchmarkCompile(b *testing.B) {
for _, expr := range benchExprs {
b.Run(expr, func(b *testing.B) {
b.ReportAllocs()
for range b.N {
_, err := gnata.Compile(expr)
if err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkEval(b *testing.B) {
var data any
if err := json.Unmarshal(json.RawMessage(benchData), &data); err != nil {
b.Fatal(err)
}
for _, exprStr := range benchExprs {
expr, err := gnata.Compile(exprStr)
if err != nil {
b.Logf("skip %q: %v", exprStr, err)
continue
}
b.Run(exprStr, func(b *testing.B) {
b.ReportAllocs()
for range b.N {
if _, err := expr.Eval(context.Background(), data); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkEvalBytes(b *testing.B) {
rawData := json.RawMessage(benchData)
for _, exprStr := range benchExprs {
expr, err := gnata.Compile(exprStr)
if err != nil {
b.Logf("skip %q: %v", exprStr, err)
continue
}
b.Run(exprStr, func(b *testing.B) {
b.SetBytes(int64(len(rawData)))
b.ReportAllocs()
for range b.N {
if _, err := expr.EvalBytes(context.Background(), rawData); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkStreamEvaluator(b *testing.B) {
exprs := make([]*gnata.Expression, 0, len(benchExprs))
indices := make([]int, 0, len(benchExprs))
for _, exprStr := range benchExprs {
e, err := gnata.Compile(exprStr)
if err != nil {
b.Logf("skip %q: %v", exprStr, err)
continue
}
indices = append(indices, len(exprs))
exprs = append(exprs, e)
}
se := gnata.NewStreamEvaluator(exprs)
rawData := json.RawMessage(benchData)
b.ResetTimer()
b.SetBytes(int64(len(rawData)))
b.ReportAllocs()
for range b.N {
if _, err := se.EvalMany(context.Background(), rawData, "bench-schema", indices); err != nil {
b.Fatal(err)
}
}
b.ReportMetric(float64(se.Stats().Hits), "cache-hits")
}