-
Notifications
You must be signed in to change notification settings - Fork 6
/
benchmark_test.go
184 lines (156 loc) · 4.14 KB
/
benchmark_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
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
181
182
183
184
package jsoncolor_test
import (
"bytes"
stdj "encoding/json"
"io"
"io/ioutil"
"testing"
"time"
segmentj "github.com/segmentio/encoding/json"
"github.com/neilotoole/jsoncolor"
nwidgerj "github.com/nwidger/jsoncolor"
)
func BenchmarkEncode(b *testing.B) {
recs := makeRecords(b, 10000)
benchmarks := []struct {
name string
indent bool
color bool
fn newEncoderFunc
}{
{name: "stdlib_NoIndent", fn: newEncStdlib},
{name: "stdlib_Indent", fn: newEncStdlib, indent: true},
{name: "segmentj_NoIndent", fn: newEncSegmentj},
{name: "segmentj_Indent", fn: newEncSegmentj, indent: true},
{name: "neilotoole_NoIndent_NoColor", fn: newEncNeilotoole},
{name: "neilotoole_Indent_NoColor", fn: newEncNeilotoole, indent: true},
{name: "neilotoole_NoIndent_Color", fn: newEncNeilotoole, color: true},
{name: "neilotoole_Indent_Color", fn: newEncNeilotoole, indent: true, color: true},
{name: "nwidger_NoIndent_NoColor", fn: newEncNwidger},
{name: "nwidger_Indent_NoColor", fn: newEncNwidger, indent: true},
{name: "nwidger_indent_NoIndent_Colo", fn: newEncNwidger, color: true},
{name: "nwidger_indent_Indent_Color", fn: newEncNwidger, indent: true, color: true},
}
for _, bm := range benchmarks {
bm := bm
b.Run(bm.name, func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
w := &bytes.Buffer{}
enc := bm.fn(w, bm.indent, bm.color)
for i := range recs {
err := enc.Encode(recs[i])
if err != nil {
b.Error(err)
}
}
}
})
}
}
func makeRecords(tb testing.TB, n int) [][]interface{} {
recs := make([][]interface{}, 0, n)
// add a bunch of data from a file, just to make the recs bigger
data, err := ioutil.ReadFile("testdata/sakila_actor.json")
if err != nil {
tb.Fatal(err)
}
f := new(interface{})
if err = stdj.Unmarshal(data, f); err != nil {
tb.Fatal(err)
}
type someStruct struct {
i int64
a string
f interface{} // x holds JSON loaded from file
}
for i := 0; i < n; i++ {
rec := []interface{}{
int(1),
int64(2),
float32(2.71),
float64(3.14),
"hello world",
someStruct{i: 8, a: "goodbye world", f: f},
map[string]interface{}{"a": 9, "b": "ca va"},
true,
false,
time.Unix(1631659220, 0),
time.Millisecond * 1631659220,
}
recs = append(recs, rec)
}
return recs
}
type newEncoderFunc func(w io.Writer, indent, color bool) encoder
var (
_ newEncoderFunc = newEncStdlib
_ newEncoderFunc = newEncSegmentj
_ newEncoderFunc = newEncNeilotoole
_ newEncoderFunc = newEncNwidger
)
type encoder interface {
SetEscapeHTML(on bool)
SetIndent(prefix, indent string)
Encode(v interface{}) error
}
func newEncStdlib(w io.Writer, indent, color bool) encoder {
enc := stdj.NewEncoder(w)
if indent {
enc.SetIndent("", " ")
}
enc.SetEscapeHTML(true)
return enc
}
func newEncSegmentj(w io.Writer, indent, color bool) encoder {
enc := segmentj.NewEncoder(w)
if indent {
enc.SetIndent("", " ")
}
enc.SetEscapeHTML(true)
return enc
}
func newEncNeilotoole(w io.Writer, indent, color bool) encoder {
enc := jsoncolor.NewEncoder(w)
if indent {
enc.SetIndent("", " ")
}
enc.SetEscapeHTML(true)
if color {
clrs := jsoncolor.DefaultColors()
enc.SetColors(clrs)
}
return enc
}
func newEncNwidger(w io.Writer, indent, color bool) encoder {
if !color {
enc := nwidgerj.NewEncoder(w)
enc.SetEscapeHTML(false)
if indent {
enc.SetIndent("", " ")
}
return enc
}
// It's color
f := nwidgerj.NewFormatter()
f.SpaceColor = nwidgerj.DefaultSpaceColor
f.CommaColor = nwidgerj.DefaultCommaColor
f.ColonColor = nwidgerj.DefaultColonColor
f.ObjectColor = nwidgerj.DefaultObjectColor
f.ArrayColor = nwidgerj.DefaultArrayColor
f.FieldQuoteColor = nwidgerj.DefaultFieldQuoteColor
f.FieldColor = nwidgerj.DefaultFieldColor
f.StringQuoteColor = nwidgerj.DefaultStringQuoteColor
f.StringColor = nwidgerj.DefaultStringColor
f.TrueColor = nwidgerj.DefaultTrueColor
f.FalseColor = nwidgerj.DefaultFalseColor
f.NumberColor = nwidgerj.DefaultNumberColor
f.NullColor = nwidgerj.DefaultNullColor
enc := nwidgerj.NewEncoderWithFormatter(w, f)
enc.SetEscapeHTML(false)
if indent {
enc.SetIndent("", " ")
}
return enc
}