~mna/zerojson

zerojson/stdlib_bench_test.go -rw-r--r-- 2.9 KiB View raw
ccdac2eaMartin Angers cleanup comments 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package zerojson

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

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

func BenchmarkStdlibUnmarshalTrue(b *testing.B) {
	benchmarkStdlibUnmarshalInput(b, " true ")
}

func BenchmarkStdlibDecoderTrue(b *testing.B) {
	benchmarkStdlibDecoderInput(b, " true ")
}

func BenchmarkStdlibUnmarshalFalse(b *testing.B) {
	benchmarkStdlibUnmarshalInput(b, " false ")
}

func BenchmarkStdlibDecoderFalse(b *testing.B) {
	benchmarkStdlibDecoderInput(b, " false ")
}

func BenchmarkStdlibUnmarshalNull(b *testing.B) {
	benchmarkStdlibUnmarshalInput(b, " null ")
}

func BenchmarkStdlibDecoderNull(b *testing.B) {
	benchmarkStdlibDecoderInput(b, " null ")
}

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

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

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

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

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

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

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

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

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

func BenchmarkStdlibUnmarshalTestdata(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)
			benchmarkStdlibUnmarshalInput(b, string(input))
		})
	}
}

func benchmarkStdlibUnmarshalInput(b *testing.B, input string) {
	data := []byte(input)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		var raw json.RawMessage
		if err := json.Unmarshal(data, &raw); err != nil {
			b.Fatal(err)
		}
	}
}

func benchmarkStdlibDecoderInput(b *testing.B, input string) {
	r := strings.NewReader(input)
	dec := json.NewDecoder(r)
	dec.UseNumber()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		r.Reset(input)
		for {
			_, err := dec.Token()
			if err != nil {
				if err == io.EOF {
					break
				}
				b.Fatal(err)
			}
			//fmt.Printf("[%T] %[1]v\n", tok)
		}
	}
}