-
Notifications
You must be signed in to change notification settings - Fork 21
/
flatten_json_bench_test.go
137 lines (105 loc) · 2.72 KB
/
flatten_json_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
package quamina
import (
"os"
"strings"
"testing"
)
var (
topMatches []X
topFields []Field
)
const PatternContext = `{ "context": { "user_id": [9034], "friends_count": [158] } }`
const PatternMiddleNestedField = `{ "payload": { "user": { "id_str": ["903487807"] } } }`
const PatternLastField = `{ "payload": { "lang_value": ["ja"] } }`
func Benchmark_JsonFlattener_ContextFields(b *testing.B) {
RunBenchmarkWithJSONFlattener(b, "context\nuser_id", "context\nfriends_count")
}
func Benchmark_JsonFlattener_MiddleNestedField(b *testing.B) {
RunBenchmarkWithJSONFlattener(b, "payload\nuser\nid_str")
}
func Benchmark_JsonFlattener_LastField(b *testing.B) {
RunBenchmarkWithJSONFlattener(b, "payload\nlang_value")
}
func RunBenchmarkWithJSONFlattener(b *testing.B, paths ...string) {
b.Helper()
var localFields []Field
event, err := os.ReadFile("./testdata/status.json")
if err != nil {
b.Fatal(err)
}
flattener := newJSONFlattener()
t := newSegmentsIndex(paths...)
results, err := flattener.Flatten(event, t)
if err != nil {
b.Fatal(err)
}
PrintFields(b, results)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
fields, err := flattener.Flatten(event, t)
if err != nil {
b.Fatal(err)
}
localFields = fields
}
topFields = localFields
}
func Benchmark_JsonFlattner_Evaluate_ContextFields(b *testing.B) {
q, err := New()
if err != nil {
b.Fatal(err)
}
RunBenchmarkEvaluate(b, q, PatternContext)
}
func Benchmark_JsonFlattner_Evaluate_MiddleNestedField(b *testing.B) {
q, err := New()
if err != nil {
b.Fatal(err)
}
RunBenchmarkEvaluate(b, q, PatternMiddleNestedField)
}
func Benchmark_JsonFlattner_Evaluate_LastField(b *testing.B) {
q, err := New()
if err != nil {
b.Fatal(err)
}
RunBenchmarkEvaluate(b, q, PatternLastField)
}
func RunBenchmarkEvaluate(b *testing.B, q *Quamina, pattern string) {
b.Helper()
err := q.AddPattern(1, pattern)
if err != nil {
b.Fatalf("Failed adding pattern: %+v", err)
}
event, err := os.ReadFile("./testdata/status.json")
if err != nil {
b.Fatal(err)
}
matches, err := q.MatchesForEvent(event)
if err != nil {
b.Fatalf("failed matching: %s", err)
}
if len(matches) != 1 {
b.Fatalf("in-correct matching: %+v", matches)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
matches, err := q.MatchesForEvent(event)
if err != nil {
b.Fatalf("failed matching: %s", err)
}
if len(matches) != 1 {
b.Fatalf("in-correct matching: %+v", matches)
}
}
}
func PrintFields(tb testing.TB, fields []Field) {
tb.Helper()
tb.Logf("> Fields\n")
for _, field := range fields {
tb.Logf("Path [%s] Val [%s] ArrayTrail [%+v]\n", strings.ReplaceAll(string(field.Path), "\n", "->"), field.Val, field.ArrayTrail)
}
tb.Logf("\n")
}