~mna/zerojson

824f0acae408bc1b364306b00083a389ebfaa460 — Martin Angers 3 years ago 06e7c31
add fastjson benchmark for comparison
4 files changed, 83 insertions(+), 10 deletions(-)

A fastjson_bench_test.go
M go.mod
M go.sum
M zerojson_bench_test.go
A fastjson_bench_test.go => fastjson_bench_test.go +68 -0
@@ 0,0 1,68 @@
package zerojson

import (
	"io/ioutil"
	"path/filepath"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
	"github.com/valyala/fastjson"
)

func BenchmarkFastjsonTrue(b *testing.B) {
	benchmarkFastjsonInput(b, " true ")
}

func BenchmarkFastjsonFalse(b *testing.B) {
	benchmarkFastjsonInput(b, " false ")
}

func BenchmarkFastjsonNull(b *testing.B) {
	benchmarkFastjsonInput(b, " null ")
}

func BenchmarkFastjsonString(b *testing.B) {
	benchmarkFastjsonInput(b, `"a\bcde\fghijklm\nopq\rs\t\uaB12vwxyz\"\\\/"`)
}

func BenchmarkFastjsonNumber(b *testing.B) {
	benchmarkFastjsonInput(b, `123.456e+7890`)
}

func BenchmarkFastjsonSmallObject(b *testing.B) {
	benchmarkFastjsonInput(b, `{"active": true, "name": "Foo Bar", "age": 42}`)
}

func BenchmarkFastjsonSmallArray(b *testing.B) {
	benchmarkFastjsonInput(b, `["a string", -10.234, false, null]`)
}

func BenchmarkFastjsonDeeplyNested(b *testing.B) {
	benchmarkFastjsonInput(b, strings.Repeat("[", 1_000_000)+strings.Repeat("]", 1_000_000))
}

func BenchmarkFastjsonTestdata(b *testing.B) {
	files, err := ioutil.ReadDir("testdata")
	require.NoError(b, err)
	for _, file := range files {
		if ext := filepath.Ext(file.Name()); ext != ".json" {
			continue
		}

		b.Run(file.Name(), func(b *testing.B) {
			input, err := ioutil.ReadFile(filepath.Join("testdata", file.Name()))
			require.NoError(b, err)
			benchmarkFastjsonInput(b, string(input))
		})
	}
}

func benchmarkFastjsonInput(b *testing.B, input string) {
	var p fastjson.Parser
	for i := 0; i < b.N; i++ {
		if _, err := p.Parse(input); err != nil {
			b.Fatal(err)
		}
	}
}

M go.mod => go.mod +4 -1
@@ 2,4 2,7 @@ module git.sr.ht/~mna/zerojson

go 1.13

require github.com/stretchr/testify v1.4.0
require (
	github.com/stretchr/testify v1.4.0
	github.com/valyala/fastjson v1.4.1
)

M go.sum => go.sum +2 -0
@@ 5,6 5,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/valyala/fastjson v1.4.1 h1:hrltpHpIpkaxll8QltMU8c3QZ5+qIiCL8yKqPFJI/yE=
github.com/valyala/fastjson v1.4.1/go.mod h1:nV6MsjxL2IMJQUoHDIrjEI7oLyeqK6aBD7EFWPsvP8o=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

M zerojson_bench_test.go => zerojson_bench_test.go +9 -9
@@ 10,39 10,39 @@ import (
	"github.com/stretchr/testify/require"
)

func BenchmarkTrue(b *testing.B) {
func BenchmarkZerojsonTrue(b *testing.B) {
	benchmarkInput(b, " true ")
}

func BenchmarkFalse(b *testing.B) {
func BenchmarkZerojsonFalse(b *testing.B) {
	benchmarkInput(b, " false ")
}

func BenchmarkNull(b *testing.B) {
func BenchmarkZerojsonNull(b *testing.B) {
	benchmarkInput(b, " null ")
}

func BenchmarkString(b *testing.B) {
func BenchmarkZerojsonString(b *testing.B) {
	benchmarkInput(b, `"a\bcde\fghijklm\nopq\rs\t\uaB12vwxyz\"\\\/"`)
}

func BenchmarkNumber(b *testing.B) {
func BenchmarkZerojsonNumber(b *testing.B) {
	benchmarkInput(b, `123.456e+7890`)
}

func BenchmarkSmallObject(b *testing.B) {
func BenchmarkZerojsonSmallObject(b *testing.B) {
	benchmarkInput(b, `{"active": true, "name": "Foo Bar", "age": 42}`)
}

func BenchmarkSmallArray(b *testing.B) {
func BenchmarkZerojsonSmallArray(b *testing.B) {
	benchmarkInput(b, `["a string", -10.234, false, null]`)
}

func BenchmarkDeeplyNested(b *testing.B) {
func BenchmarkZerojsonDeeplyNested(b *testing.B) {
	benchmarkInput(b, strings.Repeat("[", 1_000_000)+strings.Repeat("]", 1_000_000))
}

func BenchmarkTestdata(b *testing.B) {
func BenchmarkZerojsonTestdata(b *testing.B) {
	files, err := ioutil.ReadDir("testdata")
	require.NoError(b, err)
	for _, file := range files {