From 06af1af34e48d559a2a6a39a08ec02d40218e336 Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Thu, 11 Aug 2016 18:15:15 -0700 Subject: [PATCH] fixed benchmark bug with jsonparser Big thanks to dlsspy for pointing this out. --- README.md | 2 +- gjson_test.go | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2898ecc..3a79d04 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/gjson_test.go b/gjson_test.go index 1aec401..0563d7b 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -495,17 +495,27 @@ 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 { - t.Fatal("did not find the value") + 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") + } } } }