-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsoniter.go
162 lines (144 loc) · 3.38 KB
/
jsoniter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2020, Peter Ohler, All rights reserved.
package main
import (
"bytes"
"errors"
"io/ioutil"
"log"
"os"
"testing"
jsoniter "github.com/json-iterator/go"
)
var jsoniterPkg = pkg{
name: "jsoniter",
calls: map[string]*call{
"parse": {name: "Unmarshal", fun: jsoniterUnmarshal},
"validate": {name: "Valid", fun: jsoniterValid},
"decode": {name: "Decode", fun: jsoniterDecode},
"unmarshal-struct": {name: "Unmarshal", fun: jsoniterUnmarshalPatient},
"marshal": {name: "Marshal", fun: jsoniterMarshal},
"marshal-struct": {name: "Marshal", fun: jsoniterMarshalPatient},
"file1": {name: "Decode", fun: jsoniterFile1},
"small-file": {name: "Decode", fun: jsoniterFileManySmall},
"large-file": {name: "Decode", fun: jsoniterFileManyLarge},
},
}
func jsoniterUnmarshal(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
b.ResetTimer()
var result interface{}
for n := 0; n < b.N; n++ {
if benchErr = jsoniter.Unmarshal(sample, &result); benchErr != nil {
b.Fail()
}
}
}
func jsoniterValid(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
b.ResetTimer()
for n := 0; n < b.N; n++ {
if !jsoniter.Valid(sample) {
benchErr = errors.New("JSON not valid")
b.Fail()
}
}
}
func jsoniterDecode(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
b.ResetTimer()
var data interface{}
for n := 0; n < b.N; n++ {
dec := jsoniter.NewDecoder(bytes.NewReader(sample))
if err := dec.Decode(&data); err != nil {
benchErr = err
b.Fail()
}
}
}
func jsoniterUnmarshalPatient(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
b.ResetTimer()
var patient Patient
for n := 0; n < b.N; n++ {
if benchErr = jsoniter.Unmarshal(sample, &patient); benchErr != nil {
b.Fail()
}
}
}
func jsoniterMarshal(b *testing.B) {
data := loadSample()
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, benchErr = jsoniter.Marshal(data); benchErr != nil {
b.Fail()
}
}
}
func jsoniterMarshalPatient(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
var patient Patient
if err := jsoniter.Unmarshal(sample, &patient); err != nil {
log.Fatal(err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, benchErr = jsoniter.Marshal(&patient); benchErr != nil {
b.Fail()
}
}
}
func jsoniterFile1(b *testing.B) {
f, err := os.Open(filename)
if err != nil {
log.Fatalf("Failed to read %s. %s\n", filename, err)
}
defer func() { _ = f.Close() }()
for n := 0; n < b.N; n++ {
_, _ = f.Seek(0, 0)
dec := jsoniter.NewDecoder(f)
var data interface{}
if err := dec.Decode(&data); err != nil {
benchErr = err
b.Fail()
}
}
}
func jsoniterFileManySmall(b *testing.B) {
f := openSmallLogFile()
defer func() { _ = f.Close() }()
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, _ = f.Seek(0, 0)
j, _ := ioutil.ReadAll(f)
dec := jsoniter.NewDecoder(bytes.NewReader(j))
for {
var data interface{}
if !dec.More() {
break
}
if err := dec.Decode(&data); err != nil {
benchErr = err
b.Fail()
}
}
}
}
func jsoniterFileManyLarge(b *testing.B) {
f := openLargeLogFile()
defer func() { _ = f.Close() }()
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, _ = f.Seek(0, 0)
dec := jsoniter.NewDecoder(f)
for {
var data interface{}
if !dec.More() {
break
}
if err := dec.Decode(&data); err != nil {
benchErr = err
b.Fail()
}
}
}
}