-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbenchmark_test.go
168 lines (149 loc) · 3 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
package jsony_test
import (
"encoding/json"
"testing"
"github.com/orsinium-labs/jsony"
)
// A black box to ensure that the compiler does not optimize away benchmarked code.
//
//go:noinline
func box[T any](v T) T {
return v
}
func BenchmarkInt_Jsony(b *testing.B) {
v := box(3)
for i := 0; i < b.N; i++ {
box(jsony.EncodeBytes(jsony.Int(v)))
}
}
func BenchmarkInt_Stdlib(b *testing.B) {
v := box(3)
for i := 0; i < b.N; i++ {
b, _ := json.Marshal(v)
box(b)
}
}
func BenchmarkFloat64_Jsony(b *testing.B) {
v := box(3.14)
for i := 0; i < b.N; i++ {
box(jsony.EncodeBytes(jsony.Float64(v)))
}
}
func BenchmarkFloat64_Stdlib(b *testing.B) {
v := box(3.14)
for i := 0; i < b.N; i++ {
b, _ := json.Marshal(v)
box(b)
}
}
func BenchmarkString_Jsony(b *testing.B) {
v := box("johny")
for i := 0; i < b.N; i++ {
box(jsony.EncodeBytes(jsony.String(v)))
}
}
func BenchmarkString_Stdlib(b *testing.B) {
v := box("johny")
for i := 0; i < b.N; i++ {
b, _ := json.Marshal(v)
box(b)
}
}
func BenchmarkObject_Jsony(b *testing.B) {
name := box("john")
age := box(42)
for i := 0; i < b.N; i++ {
obj := jsony.Object{
jsony.Field{"name", jsony.String(name)},
jsony.Field{"age", jsony.Int(age)},
}
box(jsony.EncodeBytes(obj))
}
}
func BenchmarkObject_Stdlib(b *testing.B) {
name := box("john")
age := box(42)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
for i := 0; i < b.N; i++ {
v := User{
Name: name,
Age: age,
}
b, _ := json.Marshal(v)
box(b)
}
}
func BenchmarkMap_Jsony(b *testing.B) {
name := box("john")
age := box(42)
for i := 0; i < b.N; i++ {
obj := jsony.Map{
jsony.String("name"): jsony.String(name),
jsony.String("age"): jsony.Int(age),
}
box(jsony.EncodeBytes(obj))
}
}
func BenchmarkMap_Stdlib(b *testing.B) {
name := box("john")
age := box(42)
type User map[string]any
for i := 0; i < b.N; i++ {
v := User{
"name": name,
"age": age,
}
b, _ := json.Marshal(v)
box(b)
}
}
func BenchmarkMixedArray_Jsony(b *testing.B) {
name := box("john")
age := box(42)
for i := 0; i < b.N; i++ {
obj := jsony.MixedArray{jsony.String(name), jsony.Int(age)}
box(jsony.EncodeBytes(obj))
}
}
func BenchmarkMixedArray_Stdlib(b *testing.B) {
name := box("john")
age := box(42)
for i := 0; i < b.N; i++ {
v := []any{name, age}
b, _ := json.Marshal(v)
box(b)
}
}
func BenchmarkIntArray_Jsony(b *testing.B) {
x := box(42)
y := box(1337)
for i := 0; i < b.N; i++ {
obj := jsony.Array[jsony.Int]{jsony.Int(x), jsony.Int(y)}
box(jsony.EncodeBytes(obj))
}
}
func BenchmarkIntArray_Stdlib(b *testing.B) {
x := box(42)
y := box(1337)
for i := 0; i < b.N; i++ {
v := []int{x, y}
b, _ := json.Marshal(v)
box(b)
}
}
func BenchmarkBigArray_Jsony(b *testing.B) {
obj := box(make(jsony.Array[jsony.Int], 10_000))
for i := 0; i < b.N; i++ {
box(jsony.EncodeBytes(obj))
}
}
func BenchmarkBigArray_Stdlib(b *testing.B) {
x := box(make([]int, 10_000))
for i := 0; i < b.N; i++ {
b, _ := json.Marshal(x)
box(b)
}
}