fixed benchmark bug with jsonparser

Big thanks to dlsspy for pointing this out.
This commit is contained in:
Josh Baker 2016-08-11 18:15:15 -07:00
parent d0d9241c76
commit 06af1af34e
2 changed files with 16 additions and 6 deletions

View File

@ -112,7 +112,7 @@ BenchmarkJSONUnmarshalStruct-8 600000 11635 ns/op 1960 B/op 69 allocs/op
BenchmarkJSONDecoder-8 300000 17193 ns/op 4864 B/op 184 allocs/op
BenchmarkFFJSONLexer-8 1500000 3773 ns/op 1024 B/op 8 allocs/op
BenchmarkEasyJSONLexer-8 3000000 1134 ns/op 741 B/op 6 allocs/op
BenchmarkJSONParserGet-8 3000000 777 ns/op 0 B/op 0 allocs/op
BenchmarkJSONParserGet-8 3000000 596 ns/op 21 B/op 0 allocs/op
```
JSON document used:

View File

@ -495,18 +495,28 @@ func BenchmarkEasyJSONLexer(t *testing.B) {
func BenchmarkJSONParserGet(t *testing.B) {
data := []byte(exampleJSON)
keys := make([][]string, len(benchPaths))
keys := make([][]string, 0, len(benchPaths))
for i := 0; i < len(benchPaths); i++ {
keys = append(keys, strings.Split(benchPaths[i], "."))
}
t.ResetTimer()
t.ReportAllocs()
for i := 0; i < t.N; i++ {
for j := 0; j < len(benchPaths); j++ {
_, _, _, err := jsonparser.Get(data, keys[j]...)
if err != nil {
for j, k := range keys {
if j == 1 {
// "widget.image.hOffset" is a number
v, _ := jsonparser.GetInt(data, k...)
if v == 0 {
t.Fatal("did not find the value")
}
} else {
// "widget.window.name",
// "widget.text.onMouseUp",
v, _ := jsonparser.GetString(data, k...)
if v == "" {
t.Fatal("did not find the value")
}
}
}
}
t.N *= len(benchPaths) // because we are running against 3 paths