forked from karminski/streaming-json-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_timing_test.go
48 lines (43 loc) · 1.46 KB
/
lexer_timing_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
package streamingjsongo
import (
"fmt"
"testing"
)
func BenchmarkParse(b *testing.B) {
testCaseA := `{"string": "这是一个字符串", "integer": 42, "float": 3.14159, "boolean_true": true, "boolean_false": false, "null": null, "object": {"empty_object": {}, "non_empty_object": {"key": "value"}, "nested_object": {"nested_key": {"sub_nested_key": "sub_nested_value"}}}, "array":["string in array", 123, 45.67, true, false, null, {"object_in_array": "object_value"},["nested_array"]]}`
b.Run("streaming-json-go-append-json-segment", func(b *testing.B) {
benchmarkAppendString(b, testCaseA)
})
b.Run("streaming-json-go-append-and-complete-json-segment", func(b *testing.B) {
benchmarkAppendAndCompleteJSON(b, testCaseA)
})
}
func benchmarkAppendString(b *testing.B, s string) {
b.ReportAllocs()
b.SetBytes(int64(len(s)))
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lexer := NewLexer()
if err := lexer.AppendString(s); err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
}
})
}
func benchmarkAppendAndCompleteJSON(b *testing.B, s string) {
b.ReportAllocs()
b.SetBytes(int64(len(s)))
var completedJSON string
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lexer := NewLexer()
if err := lexer.AppendString(s); err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
completedJSON = lexer.CompleteJSON()
if len(completedJSON) == 0 {
panic(fmt.Errorf("invalied completed JSON length"))
}
}
})
}