forked from slaise/GoJsonBenchmark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark_medium_pl_test.go
100 lines (90 loc) · 2.11 KB
/
benchmark_medium_pl_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
package main
import (
"encoding/json"
"testing"
"github.com/buger/jsonparser"
gojson "github.com/goccy/go-json"
"github.com/json-iterator/go"
"github.com/mailru/easyjson/jlexer"
"github.com/valyala/fastjson"
)
/*
encoding/json
*/
func BenchmarkDecodeStdStructMedium(b *testing.B) {
b.ReportAllocs()
var data MediumPayload
for i := 0; i < b.N; i++ {
json.Unmarshal(mediumFixture, &data)
}
}
// jsoniter
func BenchmarkDecodeJsoniterStructMedium(b *testing.B) {
b.ReportAllocs()
var data MediumPayload
for i := 0; i < b.N; i++ {
jsoniter.Unmarshal(mediumFixture, &data)
}
}
// easyjson
func BenchmarkDecodeEasyJsonMedium(b *testing.B) {
b.ReportAllocs()
var data MediumPayload
for i := 0; i < b.N; i++ {
lexer := &jlexer.Lexer{Data: mediumFixture}
data.UnmarshalEasyJSON(lexer)
}
}
// jsonParser
func BenchmarkDecodeJsonParserMedium(b *testing.B) {
b.ReportAllocs()
paths := [][]string{
{"person", "name", "fullName"},
{"person", "github", "followers"},
}
for i := 0; i < b.N; i++ {
var data = MediumPayload{
Person: &CBPerson{
Name: &CBName{},
Github: &CBGithub{},
},
}
jsonparser.EachKey(mediumFixture, func(idx int, value []byte, vt jsonparser.ValueType, err error) {
switch idx {
case 0:
data.Person.Name.FullName, _ = jsonparser.ParseString(value)
case 1:
v, _ := jsonparser.ParseInt(value)
data.Person.Github.Followers = int(v)
}
}, paths...)
}
}
// go-json
func BenchmarkDecodeGoJsonMedium(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var data MediumPayload
gojson.Unmarshal(mediumFixture, &data)
_ = data.Person.Name.FullName
_ = data.Person.Github.Followers
}
}
// fastjson
func BenchmarkDecodeFastJsonMedium(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var p fastjson.Parser
var data = MediumPayload{
Person: &CBPerson{
Name: &CBName{},
Github: &CBGithub{},
},
}
v, _ := p.ParseBytes(mediumFixture)
data.Person.Name.FullName = string(v.GetStringBytes("person", "name", "fullName"))
data.Person.Github.Followers = v.GetInt("person", "github", "followers")
}
}