buger/jsonparser

Benchmarking against fastjson and gjson

dgrr opened this issue · 7 comments

dgrr commented

Hello,

Can you benchmark your package agains fastjson and gjson?

Thanks.

go test -test.benchmem -bench "(JsonParser|fastjson|GJson)" ./benchmark/ -benchtime 5s -v

goos: windows
goarch: amd64
pkg: benchmarks
BenchmarkJsonParserLarge
BenchmarkJsonParserLarge-4 88914 70391 ns/op 0 B/op 0 allocs/op
BenchmarkJsonParserMedium
BenchmarkJsonParserMedium-4 568888 10794 ns/op 0 B/op 0 allocs/op
BenchmarkJsonParserDeleteMedium
BenchmarkJsonParserDeleteMedium-4 520677 12015 ns/op 0 B/op 0 allocs/op
BenchmarkJsonParserEachKeyManualMedium
BenchmarkJsonParserEachKeyManualMedium-4 877713 7063 ns/op 112 B/op 2 allocs/op
BenchmarkJsonParserEachKeyStructMedium
BenchmarkJsonParserEachKeyStructMedium-4 749266 8231 ns/op 560 B/op 12 allocs/op
BenchmarkJsonParserObjectEachStructMedium
BenchmarkJsonParserObjectEachStructMedium-4 548570 11942 ns/op 512 B/op 11 allocs/op
BenchmarkJsonParserSmall
BenchmarkJsonParserSmall-4 5255774 1096 ns/op 0 B/op 0 allocs/op
BenchmarkJsonParserEachKeyManualSmall
BenchmarkJsonParserEachKeyManualSmall-4 6288638 948 ns/op 80 B/op 2 allocs/op
BenchmarkJsonParserEachKeyStructSmall
BenchmarkJsonParserEachKeyStructSmall-4 4987012 1230 ns/op 192 B/op 8 allocs/op
BenchmarkJsonParserObjectEachStructSmall
BenchmarkJsonParserObjectEachStructSmall-4 5736694 1069 ns/op 176 B/op 7 allocs/op
BenchmarkJsonParserSetSmall
BenchmarkJsonParserSetSmall-4 3551444 1722 ns/op 768 B/op 4 allocs/op
BenchmarkJsonParserDelSmall
BenchmarkJsonParserDelSmall-4 2782608 2050 ns/op 0 B/op 0 allocs/op
PASS
ok benchmarks 83.341s

dgrr commented

I don't see fastjson nor gjson. I mean, can you create the benchmarks?

Fenny commented

@dgrr, I was wondering the same thing how it would perform versus gjson my benchmark could be improved but it's a start.

package main

import (
	"testing"
	"github.com/buger/jsonparser"
	"github.com/tidwall/gjson"
)

// go test -v ./... -run=^$ -bench=Benchmark_ -benchmem -count=3

var json = []byte(`{"name":{"first":"Janet","last":"Prichard"},"age":47}`)

func Benchmark_tidwall_json(b *testing.B) {
	var res gjson.Result
	for i := 0; i < b.N; i++ {
		res = gjson.GetBytes(json, "name.last")
	}
	if res.String() != "Prichard" {
		b.Fatal(res.String())
	}
}

func Benchmark_buger_jsonparser(b *testing.B) {
	var res []byte
	for i := 0; i < b.N; i++ {
		res, _, _, _ = jsonparser.Get(json, "name", "last")
	}
	if string(res) != "Prichard" {
		b.Fatal(string(res))
	}
}
Benchmark_tidwall_json
Benchmark_tidwall_json-4        10002166               118 ns/op              16 B/op          1 allocs/op
Benchmark_tidwall_json-4         9825056               116 ns/op              16 B/op          1 allocs/op
Benchmark_tidwall_json-4         9807814               115 ns/op              16 B/op          1 allocs/op
Benchmark_buger_jsonparser
Benchmark_buger_jsonparser-4    14266759                78.7 ns/op             0 B/op          0 allocs/op
Benchmark_buger_jsonparser-4    14287414                78.3 ns/op             0 B/op          0 allocs/op
Benchmark_buger_jsonparser-4    14461699                78.5 ns/op             0 B/op          0 allocs/op
dgrr commented

Hello @Fenny

I just commited #201 which adds fastjson benchmarks. I get the following running go test -v ./... -bench="(FastJSON|JsonParser)" -benchmem -benchtime 5s

BenchmarkJsonParserLarge
BenchmarkJsonParserLarge-8                    	   89894	     67214 ns/op	       0 B/op	       0 allocs/op
BenchmarkFastJSONLarge
BenchmarkFastJSONLarge-8                      	  119726	     44910 ns/op	       2 B/op	       0 allocs/op
BenchmarkJsonParserMedium
BenchmarkJsonParserMedium-8                   	  550980	     10873 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserDeleteMedium
BenchmarkJsonParserDeleteMedium-8             	  491080	     12086 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserEachKeyManualMedium
BenchmarkJsonParserEachKeyManualMedium-8      	  970214	      8430 ns/op	     112 B/op	       2 allocs/op
BenchmarkJsonParserEachKeyStructMedium
BenchmarkJsonParserEachKeyStructMedium-8      	  616657	      9284 ns/op	     560 B/op	      12 allocs/op
BenchmarkJsonParserObjectEachStructMedium
BenchmarkJsonParserObjectEachStructMedium-8   	  429591	     14654 ns/op	     512 B/op	      11 allocs/op
BenchmarkFastJSONMedium
BenchmarkFastJSONMedium-8                     	 1000000	      5972 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserSmall
BenchmarkJsonParserSmall-8                    	 3816054	      1325 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserEachKeyManualSmall
BenchmarkJsonParserEachKeyManualSmall-8       	 4433511	      1246 ns/op	      80 B/op	       2 allocs/op
BenchmarkJsonParserEachKeyStructSmall
BenchmarkJsonParserEachKeyStructSmall-8       	 3627688	      1846 ns/op	     192 B/op	       8 allocs/op
BenchmarkJsonParserObjectEachStructSmall
BenchmarkJsonParserObjectEachStructSmall-8    	 3208753	      1602 ns/op	     176 B/op	       7 allocs/op
BenchmarkJsonParserSetSmall
BenchmarkJsonParserSetSmall-8                 	 2163159	      2729 ns/op	     768 B/op	       4 allocs/op
BenchmarkJsonParserDelSmall
BenchmarkJsonParserDelSmall-8                 	 2357421	      2508 ns/op	       0 B/op	       0 allocs/op
BenchmarkFastJSONSmall
BenchmarkFastJSONSmall-8                      	11265998	       483 ns/op	       0 B/op	       0 allocs/op

In my benchmark, fastjson is much faster. This project README.md is Misleading or Mistake.

@phuslu Could you pls show your test case so we can analyse the performance bottleneck?

My benchmark very similar with #201 , but I only test small json object.
So I just copy @dgrr result below

BenchmarkFastJSONSmall-8                      	11265998	       483 ns/op	       0 B/op	       0 allocs/op
BenchmarkJsonParserSmall-8                    	 3816054	      1325 ns/op	       0 B/op	       0 allocs/op