| 1 |
// +build use_easyjson |
| 2 |
|
| 3 |
package benchmark |
| 4 |
|
| 5 |
import ( |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/mailru/easyjson" |
| 9 |
"github.com/mailru/easyjson/jwriter" |
| 10 |
) |
| 11 |
|
| 12 |
func BenchmarkEJ_Unmarshal_M(b *testing.B) { |
| 13 |
b.SetBytes(int64(len(largeStructText))) |
| 14 |
for i := 0; i < b.N; i++ { |
| 15 |
var s LargeStruct |
| 16 |
err := s.UnmarshalJSON(largeStructText) |
| 17 |
if err != nil { |
| 18 |
b.Error(err) |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func BenchmarkEJ_Unmarshal_S(b *testing.B) { |
| 24 |
b.SetBytes(int64(len(smallStructText))) |
| 25 |
|
| 26 |
for i := 0; i < b.N; i++ { |
| 27 |
var s Entities |
| 28 |
err := s.UnmarshalJSON(smallStructText) |
| 29 |
if err != nil { |
| 30 |
b.Error(err) |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func BenchmarkEJ_Marshal_M(b *testing.B) { |
| 36 |
var l int64 |
| 37 |
for i := 0; i < b.N; i++ { |
| 38 |
data, err := easyjson.Marshal(&largeStructData) |
| 39 |
if err != nil { |
| 40 |
b.Error(err) |
| 41 |
} |
| 42 |
l = int64(len(data)) |
| 43 |
} |
| 44 |
b.SetBytes(l) |
| 45 |
} |
| 46 |
|
| 47 |
func BenchmarkEJ_Marshal_L(b *testing.B) { |
| 48 |
var l int64 |
| 49 |
for i := 0; i < b.N; i++ { |
| 50 |
data, err := easyjson.Marshal(&xlStructData) |
| 51 |
if err != nil { |
| 52 |
b.Error(err) |
| 53 |
} |
| 54 |
l = int64(len(data)) |
| 55 |
} |
| 56 |
b.SetBytes(l) |
| 57 |
} |
| 58 |
|
| 59 |
func BenchmarkEJ_Marshal_L_ToWriter(b *testing.B) { |
| 60 |
var l int64 |
| 61 |
out := &DummyWriter{} |
| 62 |
for i := 0; i < b.N; i++ { |
| 63 |
w := jwriter.Writer{} |
| 64 |
xlStructData.MarshalEasyJSON(&w) |
| 65 |
if w.Error != nil { |
| 66 |
b.Error(w.Error) |
| 67 |
} |
| 68 |
|
| 69 |
l = int64(w.Size()) |
| 70 |
w.DumpTo(out) |
| 71 |
} |
| 72 |
b.SetBytes(l) |
| 73 |
|
| 74 |
} |
| 75 |
func BenchmarkEJ_Marshal_M_Parallel(b *testing.B) { |
| 76 |
b.SetBytes(int64(len(largeStructText))) |
| 77 |
|
| 78 |
b.RunParallel(func(pb *testing.PB) { |
| 79 |
for pb.Next() { |
| 80 |
_, err := largeStructData.MarshalJSON() |
| 81 |
if err != nil { |
| 82 |
b.Error(err) |
| 83 |
} |
| 84 |
} |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
func BenchmarkEJ_Marshal_M_ToWriter(b *testing.B) { |
| 89 |
var l int64 |
| 90 |
out := &DummyWriter{} |
| 91 |
for i := 0; i < b.N; i++ { |
| 92 |
w := jwriter.Writer{} |
| 93 |
largeStructData.MarshalEasyJSON(&w) |
| 94 |
if w.Error != nil { |
| 95 |
b.Error(w.Error) |
| 96 |
} |
| 97 |
|
| 98 |
l = int64(w.Size()) |
| 99 |
w.DumpTo(out) |
| 100 |
} |
| 101 |
b.SetBytes(l) |
| 102 |
|
| 103 |
} |
| 104 |
func BenchmarkEJ_Marshal_M_ToWriter_Parallel(b *testing.B) { |
| 105 |
out := &DummyWriter{} |
| 106 |
|
| 107 |
b.RunParallel(func(pb *testing.PB) { |
| 108 |
var l int64 |
| 109 |
for pb.Next() { |
| 110 |
w := jwriter.Writer{} |
| 111 |
largeStructData.MarshalEasyJSON(&w) |
| 112 |
if w.Error != nil { |
| 113 |
b.Error(w.Error) |
| 114 |
} |
| 115 |
|
| 116 |
l = int64(w.Size()) |
| 117 |
w.DumpTo(out) |
| 118 |
} |
| 119 |
if l > 0 { |
| 120 |
b.SetBytes(l) |
| 121 |
} |
| 122 |
}) |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
func BenchmarkEJ_Marshal_L_Parallel(b *testing.B) { |
| 127 |
var l int64 |
| 128 |
b.RunParallel(func(pb *testing.PB) { |
| 129 |
for pb.Next() { |
| 130 |
data, err := xlStructData.MarshalJSON() |
| 131 |
if err != nil { |
| 132 |
b.Error(err) |
| 133 |
} |
| 134 |
l = int64(len(data)) |
| 135 |
} |
| 136 |
}) |
| 137 |
b.SetBytes(l) |
| 138 |
} |
| 139 |
|
| 140 |
func BenchmarkEJ_Marshal_L_ToWriter_Parallel(b *testing.B) { |
| 141 |
out := &DummyWriter{} |
| 142 |
b.RunParallel(func(pb *testing.PB) { |
| 143 |
var l int64 |
| 144 |
for pb.Next() { |
| 145 |
w := jwriter.Writer{} |
| 146 |
|
| 147 |
xlStructData.MarshalEasyJSON(&w) |
| 148 |
if w.Error != nil { |
| 149 |
b.Error(w.Error) |
| 150 |
} |
| 151 |
l = int64(w.Size()) |
| 152 |
w.DumpTo(out) |
| 153 |
} |
| 154 |
if l > 0 { |
| 155 |
b.SetBytes(l) |
| 156 |
} |
| 157 |
}) |
| 158 |
} |
| 159 |
|
| 160 |
func BenchmarkEJ_Marshal_S(b *testing.B) { |
| 161 |
var l int64 |
| 162 |
for i := 0; i < b.N; i++ { |
| 163 |
data, err := smallStructData.MarshalJSON() |
| 164 |
if err != nil { |
| 165 |
b.Error(err) |
| 166 |
} |
| 167 |
l = int64(len(data)) |
| 168 |
} |
| 169 |
b.SetBytes(l) |
| 170 |
} |
| 171 |
|
| 172 |
func BenchmarkEJ_Marshal_S_Parallel(b *testing.B) { |
| 173 |
var l int64 |
| 174 |
b.RunParallel(func(pb *testing.PB) { |
| 175 |
for pb.Next() { |
| 176 |
data, err := smallStructData.MarshalJSON() |
| 177 |
if err != nil { |
| 178 |
b.Error(err) |
| 179 |
} |
| 180 |
l = int64(len(data)) |
| 181 |
} |
| 182 |
}) |
| 183 |
b.SetBytes(l) |
| 184 |
} |