-
Notifications
You must be signed in to change notification settings - Fork 1
/
gojson.go
180 lines (167 loc) · 3.62 KB
/
gojson.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) 2020, Peter Ohler, All rights reserved.
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"os"
"testing"
)
var jsonPkg = pkg{
name: "json",
calls: map[string]*call{
"parse": {name: "Unmarshal", fun: goParse},
"validate": {name: "Valid", fun: goValidate},
"decode": {name: "Decode", fun: goDecode},
"unmarshal-struct": {name: "Unmarshal", fun: goUnmarshalPatient},
"marshal": {name: "Marshal", fun: goMarshal},
"marshal-struct": {name: "Marshal", fun: goMarshalPatient},
"file1": {name: "Decode", fun: goFile1},
"small-file": {name: "Decode", fun: goFileManySmallLoad},
"large-file": {name: "Decode", fun: goFileManyLarge},
},
}
func goParse(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
b.ResetTimer()
var result interface{}
for n := 0; n < b.N; n++ {
if benchErr = json.Unmarshal(sample, &result); benchErr != nil {
b.Fail()
}
}
}
func goValidate(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
b.ResetTimer()
for n := 0; n < b.N; n++ {
if !json.Valid(sample) {
benchErr = errors.New("JSON not valid")
b.Fail()
}
}
}
func goDecode(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
b.ResetTimer()
for n := 0; n < b.N; n++ {
dec := json.NewDecoder(bytes.NewReader(sample))
for {
_, err := dec.Token()
if err == io.EOF {
break
}
if err != nil {
benchErr = err
b.Fail()
}
}
}
}
func goUnmarshalPatient(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
b.ResetTimer()
var patient Patient
for n := 0; n < b.N; n++ {
if benchErr = json.Unmarshal(sample, &patient); benchErr != nil {
b.Fail()
}
}
}
func goMarshal(b *testing.B) {
data := loadSample()
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, benchErr = json.Marshal(data); benchErr != nil {
b.Fail()
}
}
}
func goMarshalPatient(b *testing.B) {
sample, _ := ioutil.ReadFile(filename)
var patient Patient
if err := json.Unmarshal(sample, &patient); err != nil {
log.Fatal(err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, benchErr = json.Marshal(&patient); benchErr != nil {
b.Fail()
}
}
}
func goFile1(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() }()
b.ResetTimer()
var data interface{}
for n := 0; n < b.N; n++ {
_, _ = f.Seek(0, 0)
dec := json.NewDecoder(f)
if err := dec.Decode(&data); err != nil && err != io.EOF {
benchErr = err
b.Fail()
}
}
}
func goFileManySmall(b *testing.B) {
f := openSmallLogFile()
defer func() { _ = f.Close() }()
b.ResetTimer()
var data interface{}
for n := 0; n < b.N; n++ {
_, _ = f.Seek(0, 0)
dec := json.NewDecoder(f)
for {
if err := dec.Decode(&data); err == io.EOF {
break
} else if err != nil {
benchErr = err
b.Fail()
}
}
}
}
func goFileManySmallLoad(b *testing.B) {
f := openSmallLogFile()
defer func() { _ = f.Close() }()
b.ResetTimer()
var data interface{}
for n := 0; n < b.N; n++ {
_, _ = f.Seek(0, 0)
j, _ := ioutil.ReadAll(f)
dec := json.NewDecoder(bytes.NewReader(j))
for {
if err := dec.Decode(&data); err == io.EOF {
break
} else if err != nil {
benchErr = err
b.Fail()
}
}
}
}
func goFileManyLarge(b *testing.B) {
f := openLargeLogFile()
defer func() { _ = f.Close() }()
b.ResetTimer()
var data interface{}
for n := 0; n < b.N; n++ {
_, _ = f.Seek(0, 0)
dec := json.NewDecoder(f)
for {
if err := dec.Decode(&data); err == io.EOF {
break
} else if err != nil {
benchErr = err
b.Fail()
}
}
}
}