added reset timer to all benchmarks

This commit is contained in:
Josh Baker 2016-08-27 06:20:43 -07:00
parent aed5ee957a
commit 550c66c276
2 changed files with 28 additions and 10 deletions

View File

@ -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 ## Performance
@ -197,13 +207,14 @@ Benchmarks of GJSON alongside [encoding/json](https://golang.org/pkg/encoding/js
and [jsonparser](https://github.com/buger/jsonparser) and [jsonparser](https://github.com/buger/jsonparser)
``` ```
BenchmarkGJSONGet-8 3000000 368 ns/op 0 B/op 0 allocs/op BenchmarkGJSONGet-8 15000000 333 ns/op 0 B/op 0 allocs/op
BenchmarkJSONUnmarshalMap-8 600000 9181 ns/op 3048 B/op 69 allocs/op BenchmarkGJSONUnmarshalMap-8 900000 4188 ns/op 1920 B/op 26 allocs/op
BenchmarkJSONUnmarshalStruct-8 600000 9256 ns/op 1832 B/op 69 allocs/op BenchmarkJSONUnmarshalMap-8 600000 8908 ns/op 3048 B/op 69 allocs/op
BenchmarkJSONDecoder-8 300000 14365 ns/op 4224 B/op 184 allocs/op BenchmarkJSONUnmarshalStruct-8 600000 9026 ns/op 1832 B/op 69 allocs/op
BenchmarkFFJSONLexer-8 1500000 3569 ns/op 896 B/op 8 allocs/op BenchmarkJSONDecoder-8 300000 14339 ns/op 4224 B/op 184 allocs/op
BenchmarkEasyJSONLexer-8 3000000 973 ns/op 613 B/op 6 allocs/op BenchmarkFFJSONLexer-8 1500000 3156 ns/op 896 B/op 8 allocs/op
BenchmarkJSONParserGet-8 3000000 531 ns/op 21 B/op 0 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: JSON document used:

View File

@ -781,6 +781,7 @@ var benchPaths = []string{
func BenchmarkGJSONGet(t *testing.B) { func BenchmarkGJSONGet(t *testing.B) {
t.ReportAllocs() t.ReportAllocs()
t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
for j := 0; j < len(benchPaths); j++ { for j := 0; j < len(benchPaths); j++ {
if Get(exampleJSON, benchPaths[j]).Type == Null { if Get(exampleJSON, benchPaths[j]).Type == Null {
@ -793,10 +794,11 @@ func BenchmarkGJSONGet(t *testing.B) {
func BenchmarkGJSONUnmarshalMap(t *testing.B) { func BenchmarkGJSONUnmarshalMap(t *testing.B) {
t.ReportAllocs() t.ReportAllocs()
t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
for j := 0; j < len(benchPaths); j++ { for j := 0; j < len(benchPaths); j++ {
parts := strings.Split(benchPaths[j], ".") parts := strings.Split(benchPaths[j], ".")
m := Parse(exampleJSON).Value().(map[string]interface{}) m, _ := Parse(exampleJSON).Value().(map[string]interface{})
var v interface{} var v interface{}
for len(parts) > 0 { for len(parts) > 0 {
part := parts[0] part := parts[0]
@ -820,6 +822,7 @@ func BenchmarkGJSONUnmarshalMap(t *testing.B) {
func BenchmarkJSONUnmarshalMap(t *testing.B) { func BenchmarkJSONUnmarshalMap(t *testing.B) {
t.ReportAllocs() t.ReportAllocs()
t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
for j := 0; j < len(benchPaths); j++ { for j := 0; j < len(benchPaths); j++ {
parts := strings.Split(benchPaths[j], ".") parts := strings.Split(benchPaths[j], ".")
@ -850,6 +853,7 @@ func BenchmarkJSONUnmarshalMap(t *testing.B) {
func BenchmarkJSONUnmarshalStruct(t *testing.B) { func BenchmarkJSONUnmarshalStruct(t *testing.B) {
t.ReportAllocs() t.ReportAllocs()
t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
for j := 0; j < len(benchPaths); j++ { for j := 0; j < len(benchPaths); j++ {
var s BenchStruct var s BenchStruct
@ -877,6 +881,7 @@ func BenchmarkJSONUnmarshalStruct(t *testing.B) {
func BenchmarkJSONDecoder(t *testing.B) { func BenchmarkJSONDecoder(t *testing.B) {
t.ReportAllocs() t.ReportAllocs()
t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
for j := 0; j < len(benchPaths); j++ { for j := 0; j < len(benchPaths); j++ {
dec := json.NewDecoder(bytes.NewBuffer([]byte(exampleJSON))) dec := json.NewDecoder(bytes.NewBuffer([]byte(exampleJSON)))
@ -922,6 +927,7 @@ func BenchmarkJSONDecoder(t *testing.B) {
func BenchmarkFFJSONLexer(t *testing.B) { func BenchmarkFFJSONLexer(t *testing.B) {
t.ReportAllocs() t.ReportAllocs()
t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
for j := 0; j < len(benchPaths); j++ { for j := 0; j < len(benchPaths); j++ {
l := fflib.NewFFLexer([]byte(exampleJSON)) l := fflib.NewFFLexer([]byte(exampleJSON))
@ -964,7 +970,6 @@ func BenchmarkFFJSONLexer(t *testing.B) {
} }
func BenchmarkEasyJSONLexer(t *testing.B) { func BenchmarkEasyJSONLexer(t *testing.B) {
t.ReportAllocs()
skipCC := func(l *jlexer.Lexer, n int) { skipCC := func(l *jlexer.Lexer, n int) {
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
l.Skip() l.Skip()
@ -980,6 +985,8 @@ func BenchmarkEasyJSONLexer(t *testing.B) {
l.Delim('}') l.Delim('}')
l.WantComma() l.WantComma()
} }
t.ReportAllocs()
t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
for j := 0; j < len(benchPaths); j++ { for j := 0; j < len(benchPaths); j++ {
l := &jlexer.Lexer{Data: []byte(exampleJSON)} l := &jlexer.Lexer{Data: []byte(exampleJSON)}
@ -1049,8 +1056,8 @@ func BenchmarkJSONParserGet(t *testing.B) {
for i := 0; i < len(benchPaths); i++ { for i := 0; i < len(benchPaths); i++ {
keys = append(keys, strings.Split(benchPaths[i], ".")) keys = append(keys, strings.Split(benchPaths[i], "."))
} }
t.ResetTimer()
t.ReportAllocs() t.ReportAllocs()
t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
for j, k := range keys { for j, k := range keys {
if j == 1 { if j == 1 {