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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
package fastjson
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"testing"
)
func BenchmarkParseRawString(b *testing.B) {
for _, s := range []string{`""`, `"a"`, `"abcd"`, `"abcdefghijk"`, `"qwertyuiopasdfghjklzxcvb"`} {
b.Run(s, func(b *testing.B) {
benchmarkParseRawString(b, s)
})
}
}
func benchmarkParseRawString(b *testing.B, s string) {
b.ReportAllocs()
b.SetBytes(int64(len(s)))
s = s[1:] // skip the opening '"'
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rs, tail, err := parseRawString(s)
if err != nil {
panic(fmt.Errorf("cannot parse %q: %s", s, err))
}
if rs != s[:len(s)-1] {
panic(fmt.Errorf("invalid string obtained; got %q; want %q", rs, s[:len(s)-1]))
}
if len(tail) > 0 {
panic(fmt.Errorf("non-empty tail got: %q", tail))
}
}
})
}
func BenchmarkParseRawNumber(b *testing.B) {
for _, s := range []string{"1", "1234", "123456", "-1234", "1234567890.1234567", "-1.32434e+12"} {
b.Run(s, func(b *testing.B) {
benchmarkParseRawNumber(b, s)
})
}
}
func benchmarkParseRawNumber(b *testing.B, s string) {
b.ReportAllocs()
b.SetBytes(int64(len(s)))
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rn, tail, err := parseRawNumber(s)
if err != nil {
panic(fmt.Errorf("cannot parse %q: %s", s, err))
}
if rn != s {
panic(fmt.Errorf("invalid number obtained; got %q; want %q", rn, s))
}
if len(tail) > 0 {
panic(fmt.Errorf("non-empty tail got: %q", tail))
}
}
})
}
func BenchmarkObjectGet(b *testing.B) {
for _, itemsCount := range []int{10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("items_%d", itemsCount), func(b *testing.B) {
for _, lookupsCount := range []int{0, 1, 2, 4, 8, 16, 32, 64} {
b.Run(fmt.Sprintf("lookups_%d", lookupsCount), func(b *testing.B) {
benchmarkObjectGet(b, itemsCount, lookupsCount)
})
}
})
}
}
func benchmarkObjectGet(b *testing.B, itemsCount, lookupsCount int) {
b.StopTimer()
var ss []string
for i := 0; i < itemsCount; i++ {
s := fmt.Sprintf(`"key_%d": "value_%d"`, i, i)
ss = append(ss, s)
}
s := "{" + strings.Join(ss, ",") + "}"
key := fmt.Sprintf("key_%d", len(ss)/2)
expectedValue := fmt.Sprintf("value_%d", len(ss)/2)
b.StartTimer()
b.ReportAllocs()
b.SetBytes(int64(len(s)))
b.RunParallel(func(pb *testing.PB) {
p := benchPool.Get()
for pb.Next() {
v, err := p.Parse(s)
if err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
o := v.GetObject()
for i := 0; i < lookupsCount; i++ {
sb := o.Get(key).GetStringBytes()
if string(sb) != expectedValue {
panic(fmt.Errorf("unexpected value; got %q; want %q", sb, expectedValue))
}
}
}
benchPool.Put(p)
})
}
func BenchmarkMarshalTo(b *testing.B) {
b.Run("small", func(b *testing.B) {
benchmarkMarshalTo(b, smallFixture)
})
b.Run("medium", func(b *testing.B) {
benchmarkMarshalTo(b, mediumFixture)
})
b.Run("large", func(b *testing.B) {
benchmarkMarshalTo(b, largeFixture)
})
b.Run("canada", func(b *testing.B) {
benchmarkMarshalTo(b, canadaFixture)
})
b.Run("citm", func(b *testing.B) {
benchmarkMarshalTo(b, citmFixture)
})
b.Run("twitter", func(b *testing.B) {
benchmarkMarshalTo(b, twitterFixture)
})
}
func benchmarkMarshalTo(b *testing.B, s string) {
p := benchPool.Get()
v, err := p.Parse(s)
if err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
b.ReportAllocs()
b.SetBytes(int64(len(s)))
b.RunParallel(func(pb *testing.PB) {
var b []byte
for pb.Next() {
// It is ok calling v.MarshalTo from concurrent
// goroutines, since MarshalTo doesn't modify v.
b = v.MarshalTo(b[:0])
}
})
benchPool.Put(p)
}
func BenchmarkParse(b *testing.B) {
b.Run("small", func(b *testing.B) {
benchmarkParse(b, smallFixture)
})
b.Run("medium", func(b *testing.B) {
benchmarkParse(b, mediumFixture)
})
b.Run("large", func(b *testing.B) {
benchmarkParse(b, largeFixture)
})
b.Run("canada", func(b *testing.B) {
benchmarkParse(b, canadaFixture)
})
b.Run("citm", func(b *testing.B) {
benchmarkParse(b, citmFixture)
})
b.Run("twitter", func(b *testing.B) {
benchmarkParse(b, twitterFixture)
})
}
var (
// small, medium and large fixtures are from https://github.com/buger/jsonparser/blob/f04e003e4115787c6272636780bc206e5ffad6c4/benchmark/benchmark.go
smallFixture = getFromFile("testdata/small.json")
mediumFixture = getFromFile("testdata/medium.json")
largeFixture = getFromFile("testdata/large.json")
// canada, citm and twitter fixtures are from https://github.com/serde-rs/json-benchmark/tree/0db02e043b3ae87dc5065e7acb8654c1f7670c43/data
canadaFixture = getFromFile("testdata/canada.json")
citmFixture = getFromFile("testdata/citm_catalog.json")
twitterFixture = getFromFile("testdata/twitter.json")
)
func getFromFile(filename string) string {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(fmt.Errorf("cannot read %s: %s", filename, err))
}
return string(data)
}
func benchmarkParse(b *testing.B, s string) {
b.Run("stdjson-map", func(b *testing.B) {
benchmarkStdJSONParseMap(b, s)
})
b.Run("stdjson-struct", func(b *testing.B) {
benchmarkStdJSONParseStruct(b, s)
})
b.Run("stdjson-empty-struct", func(b *testing.B) {
benchmarkStdJSONParseEmptyStruct(b, s)
})
b.Run("fastjson", func(b *testing.B) {
benchmarkFastJSONParse(b, s)
})
b.Run("fastjson-get", func(b *testing.B) {
benchmarkFastJSONParseGet(b, s)
})
}
func benchmarkFastJSONParse(b *testing.B, s string) {
b.ReportAllocs()
b.SetBytes(int64(len(s)))
b.RunParallel(func(pb *testing.PB) {
p := benchPool.Get()
for pb.Next() {
v, err := p.Parse(s)
if err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
if v.Type() != TypeObject {
panic(fmt.Errorf("unexpected value type; got %s; want %s", v.Type(), TypeObject))
}
}
benchPool.Put(p)
})
}
func benchmarkFastJSONParseGet(b *testing.B, s string) {
b.ReportAllocs()
b.SetBytes(int64(len(s)))
b.RunParallel(func(pb *testing.PB) {
p := benchPool.Get()
var n int
for pb.Next() {
v, err := p.Parse(s)
if err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
n += v.GetInt("sid")
n += len(v.GetStringBytes("uuid"))
p := v.Get("person")
if p != nil {
n++
}
c := v.Get("company")
if c != nil {
n++
}
u := v.Get("users")
if u != nil {
n++
}
a := v.GetArray("features")
n += len(a)
a = v.GetArray("topicSubTopics")
n += len(a)
o := v.Get("search_metadata")
if o != nil {
n++
}
}
benchPool.Put(p)
})
}
var benchPool ParserPool
func benchmarkStdJSONParseMap(b *testing.B, s string) {
b.ReportAllocs()
b.SetBytes(int64(len(s)))
bb := s2b(s)
b.RunParallel(func(pb *testing.PB) {
var m map[string]interface{}
for pb.Next() {
if err := json.Unmarshal(bb, &m); err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
}
})
}
func benchmarkStdJSONParseStruct(b *testing.B, s string) {
b.ReportAllocs()
b.SetBytes(int64(len(s)))
bb := s2b(s)
b.RunParallel(func(pb *testing.PB) {
var m struct {
Sid int
UUID string
Person map[string]interface{}
Company map[string]interface{}
Users []interface{}
Features []map[string]interface{}
TopicSubTopics map[string]interface{}
SearchMetadata map[string]interface{}
}
for pb.Next() {
if err := json.Unmarshal(bb, &m); err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
}
})
}
func benchmarkStdJSONParseEmptyStruct(b *testing.B, s string) {
b.ReportAllocs()
b.SetBytes(int64(len(s)))
bb := s2b(s)
b.RunParallel(func(pb *testing.PB) {
var m struct{}
for pb.Next() {
if err := json.Unmarshal(bb, &m); err != nil {
panic(fmt.Errorf("unexpected error: %s", err))
}
}
})
}
|