-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_jsoniterator_test.go
121 lines (108 loc) · 3.22 KB
/
bench_jsoniterator_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
package jsonbench_test
import (
"fmt"
"log"
"runtime"
"testing"
"github.com/riftbit/protocol_benches/structs"
"github.com/riftbit/protocol_benches/vars"
jsoniter "github.com/json-iterator/go"
)
var (
JSONIteratorExampleJsonBytes = []byte(vars.ExampleJsonString)
JSONIteratorExampleJsonMinBytes []byte
JSONIteratorFilledClassicStruct structs.ClassicJSON
JSONIteratorjsoniterator = jsoniter.ConfigCompatibleWithStandardLibrary
JSONIteratorjsoniteratorfast = jsoniter.ConfigFastest
)
func init() {
JSONIteratorExampleJsonBytes = []byte(vars.ExampleJsonString)
JSONIteratorExampleJsonMinBytes = []byte(vars.ExampleJsonMinString)
err := JSONIteratorjsoniterator.Unmarshal(JSONIteratorExampleJsonMinBytes, &JSONIteratorFilledClassicStruct)
if err != nil {
log.Fatal(err)
}
runtime.GC()
}
//
func Benchmark_jsoniterator_Marshal(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf, err := JSONIteratorjsoniterator.Marshal(JSONIteratorFilledClassicStruct)
if err != nil {
b.Fatal(err)
}
if len(buf) < 1000 {
b.Fatal(fmt.Errorf(`wrong size of buf - got %d`, len(buf)))
}
}
}
//
func Benchmark_jsoniterator_Marshal_fast(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf, err := JSONIteratorjsoniteratorfast.Marshal(JSONIteratorFilledClassicStruct)
if err != nil {
b.Fatal(err)
}
if len(buf) < 1000 {
b.Fatal(fmt.Errorf(`wrong size of buf - got %d`, len(buf)))
}
}
}
//
func Benchmark_jsoniterator_Unmarshal_Full(b *testing.B) {
// b.SetBytes(int64(len(JSONIteratorExampleJsonBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf structs.ClassicJSON
if err := JSONIteratorjsoniterator.Unmarshal(JSONIteratorExampleJsonBytes, &buf); err != nil {
b.Fatal(err)
}
if buf.Aliceblue != "#f0f8ff" {
b.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, buf.Aliceblue))
}
}
}
//
func Benchmark_jsoniterator_Unmarshal_Min(b *testing.B) {
// b.SetBytes(int64(len(JSONIteratorExampleJsonMinBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf structs.ClassicJSON
if err := JSONIteratorjsoniterator.Unmarshal(JSONIteratorExampleJsonMinBytes, &buf); err != nil {
b.Fatal(err)
}
if buf.Aliceblue != "#f0f8ff" {
b.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, buf.Aliceblue))
}
}
}
//
func Benchmark_jsoniterator_Unmarshal_Full_fast(b *testing.B) {
// b.SetBytes(int64(len(JSONIteratorExampleJsonBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf structs.ClassicJSON
if err := JSONIteratorjsoniteratorfast.Unmarshal(JSONIteratorExampleJsonBytes, &buf); err != nil {
b.Fatal(err)
}
if buf.Aliceblue != "#f0f8ff" {
b.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, buf.Aliceblue))
}
}
}
//
func Benchmark_jsoniterator_Unmarshal_Min_fast(b *testing.B) {
// b.SetBytes(int64(len(JSONIteratorExampleJsonMinBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf structs.ClassicJSON
if err := JSONIteratorjsoniteratorfast.Unmarshal(JSONIteratorExampleJsonMinBytes, &buf); err != nil {
b.Fatal(err)
}
if buf.Aliceblue != "#f0f8ff" {
b.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, buf.Aliceblue))
}
}
}