From 550c66c276592668d8c9532be82f4506e937725e Mon Sep 17 00:00:00 2001 From: Josh Baker Date: Sat, 27 Aug 2016 06:20:43 -0700 Subject: [PATCH] added reset timer to all benchmarks --- README.md | 25 ++++++++++++++++++------- gjson_test.go | 13 ++++++++++--- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3bc93df..6713365 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,16 @@ if gjson.Get(json, "name.last").Exists(){ } ``` +## Unmarshal to a map + +To unmarshal to a `map[string]interface{}`: + +```go +m, ok := gjson.Parse(json).Value().(map[string]interface{}) +if !ok{ + // not a map +} +``` ## Performance @@ -197,13 +207,14 @@ Benchmarks of GJSON alongside [encoding/json](https://golang.org/pkg/encoding/js and [jsonparser](https://github.com/buger/jsonparser) ``` -BenchmarkGJSONGet-8 3000000 368 ns/op 0 B/op 0 allocs/op -BenchmarkJSONUnmarshalMap-8 600000 9181 ns/op 3048 B/op 69 allocs/op -BenchmarkJSONUnmarshalStruct-8 600000 9256 ns/op 1832 B/op 69 allocs/op -BenchmarkJSONDecoder-8 300000 14365 ns/op 4224 B/op 184 allocs/op -BenchmarkFFJSONLexer-8 1500000 3569 ns/op 896 B/op 8 allocs/op -BenchmarkEasyJSONLexer-8 3000000 973 ns/op 613 B/op 6 allocs/op -BenchmarkJSONParserGet-8 3000000 531 ns/op 21 B/op 0 allocs/op +BenchmarkGJSONGet-8 15000000 333 ns/op 0 B/op 0 allocs/op +BenchmarkGJSONUnmarshalMap-8 900000 4188 ns/op 1920 B/op 26 allocs/op +BenchmarkJSONUnmarshalMap-8 600000 8908 ns/op 3048 B/op 69 allocs/op +BenchmarkJSONUnmarshalStruct-8 600000 9026 ns/op 1832 B/op 69 allocs/op +BenchmarkJSONDecoder-8 300000 14339 ns/op 4224 B/op 184 allocs/op +BenchmarkFFJSONLexer-8 1500000 3156 ns/op 896 B/op 8 allocs/op +BenchmarkEasyJSONLexer-8 3000000 938 ns/op 613 B/op 6 allocs/op +BenchmarkJSONParserGet-8 3000000 442 ns/op 21 B/op 0 allocs/op ``` JSON document used: diff --git a/gjson_test.go b/gjson_test.go index 3a1d361..3c32f38 100644 --- a/gjson_test.go +++ b/gjson_test.go @@ -781,6 +781,7 @@ var benchPaths = []string{ func BenchmarkGJSONGet(t *testing.B) { t.ReportAllocs() + t.ResetTimer() for i := 0; i < t.N; i++ { for j := 0; j < len(benchPaths); j++ { if Get(exampleJSON, benchPaths[j]).Type == Null { @@ -793,10 +794,11 @@ func BenchmarkGJSONGet(t *testing.B) { func BenchmarkGJSONUnmarshalMap(t *testing.B) { t.ReportAllocs() + t.ResetTimer() for i := 0; i < t.N; i++ { for j := 0; j < len(benchPaths); j++ { parts := strings.Split(benchPaths[j], ".") - m := Parse(exampleJSON).Value().(map[string]interface{}) + m, _ := Parse(exampleJSON).Value().(map[string]interface{}) var v interface{} for len(parts) > 0 { part := parts[0] @@ -820,6 +822,7 @@ func BenchmarkGJSONUnmarshalMap(t *testing.B) { func BenchmarkJSONUnmarshalMap(t *testing.B) { t.ReportAllocs() + t.ResetTimer() for i := 0; i < t.N; i++ { for j := 0; j < len(benchPaths); j++ { parts := strings.Split(benchPaths[j], ".") @@ -850,6 +853,7 @@ func BenchmarkJSONUnmarshalMap(t *testing.B) { func BenchmarkJSONUnmarshalStruct(t *testing.B) { t.ReportAllocs() + t.ResetTimer() for i := 0; i < t.N; i++ { for j := 0; j < len(benchPaths); j++ { var s BenchStruct @@ -877,6 +881,7 @@ func BenchmarkJSONUnmarshalStruct(t *testing.B) { func BenchmarkJSONDecoder(t *testing.B) { t.ReportAllocs() + t.ResetTimer() for i := 0; i < t.N; i++ { for j := 0; j < len(benchPaths); j++ { dec := json.NewDecoder(bytes.NewBuffer([]byte(exampleJSON))) @@ -922,6 +927,7 @@ func BenchmarkJSONDecoder(t *testing.B) { func BenchmarkFFJSONLexer(t *testing.B) { t.ReportAllocs() + t.ResetTimer() for i := 0; i < t.N; i++ { for j := 0; j < len(benchPaths); j++ { l := fflib.NewFFLexer([]byte(exampleJSON)) @@ -964,7 +970,6 @@ func BenchmarkFFJSONLexer(t *testing.B) { } func BenchmarkEasyJSONLexer(t *testing.B) { - t.ReportAllocs() skipCC := func(l *jlexer.Lexer, n int) { for i := 0; i < n; i++ { l.Skip() @@ -980,6 +985,8 @@ func BenchmarkEasyJSONLexer(t *testing.B) { l.Delim('}') l.WantComma() } + t.ReportAllocs() + t.ResetTimer() for i := 0; i < t.N; i++ { for j := 0; j < len(benchPaths); j++ { l := &jlexer.Lexer{Data: []byte(exampleJSON)} @@ -1049,8 +1056,8 @@ func BenchmarkJSONParserGet(t *testing.B) { for i := 0; i < len(benchPaths); i++ { keys = append(keys, strings.Split(benchPaths[i], ".")) } - t.ResetTimer() t.ReportAllocs() + t.ResetTimer() for i := 0; i < t.N; i++ { for j, k := range keys { if j == 1 {