Debian Codegraph @

codec/bench/z_all_bench_test.go

raw symbols SCIP
1 //go:build alltests && go1.7
2 // +build alltests,go1.7
3
4 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
5 // Use of this source code is governed by a MIT license found in the LICENSE file.
6
7 package codec
8
9 // see notes in z_all_test.go
10
11 import (
12 "strconv"
13 "sync"
14 "testing"
15 "time"
16 )
17
18 var benchmarkGroupOnce sync.Once
19
20 var benchmarkGroupSave struct {
21 testUseIoEncDec int
22 testUseReset bool
23
24 testDepth int
25 testMapStringKeyOnly bool
26 }
27
28 func benchmarkGroupInitAll() {
29 testInitAll() // calls flag.Parse
30 benchmarkGroupSave.testUseIoEncDec = testUseIoEncDec
31 benchmarkGroupSave.testUseReset = testUseReset
32
33 benchmarkGroupSave.testDepth = testDepth
34 benchmarkGroupSave.testMapStringKeyOnly = testMapStringKeyOnly
35 }
36
37 func benchmarkGroupReset() {
38 testUseIoEncDec = benchmarkGroupSave.testUseIoEncDec
39 testUseReset = benchmarkGroupSave.testUseReset
40
41 testDepth = benchmarkGroupSave.testDepth
42 testMapStringKeyOnly = benchmarkGroupSave.testMapStringKeyOnly
43 }
44
45 func benchmarkOneFn(fns []func(*testing.B)) func(*testing.B) {
46 switch len(fns) {
47 case 0:
48 return nil
49 case 1:
50 return fns[0]
51 default:
52 return func(t *testing.B) {
53 for _, f := range fns {
54 f(t)
55 }
56 }
57 }
58 }
59
60 func benchmarkSuiteNoop(b *testing.B) {
61 testOnce.Do(testInitAll)
62 b.ResetTimer()
63 for i := 0; i < b.N; i++ {
64 time.Sleep(10 * time.Millisecond)
65 }
66 }
67
68 func benchmarkSuite(t *testing.B, fns ...func(t *testing.B)) {
69 benchmarkGroupOnce.Do(benchmarkGroupInitAll)
70
71 f := benchmarkOneFn(fns)
72 // find . -name "*_test.go" | xargs grep -e 'flag.' | cut -d '&' -f 2 | cut -d ',' -f 1 | grep -e '^bench'
73
74 testReinit() // so flag.Parse() is called first, and never called again
75 benchReinit()
76
77 benchmarkGroupReset()
78
79 testReinit()
80 benchReinit()
81 t.Run("options-false...", f)
82
83 benchmarkGroupReset()
84
85 testUseIoEncDec = 1024
86 testReinit()
87 benchReinit()
88 t.Run("use-bufio-!bytes", f)
89
90 benchmarkGroupReset()
91
92 testUseReset = true
93 testReinit()
94 benchReinit()
95 t.Run("reset-enc-dec...", f)
96
97 benchmarkGroupReset()
98 }
99
100 func benchmarkVeryQuickSuite(t *testing.B, name string, fns ...func(t *testing.B)) {
101 benchmarkDivider()
102 benchmarkGroupOnce.Do(benchmarkGroupInitAll)
103 benchmarkGroupReset()
104
105 // bd=1 2 | ti=-1, 1024 |
106
107 testUseIoEncDec = -1
108 // testDepth = depth
109 testReinit()
110 benchReinit()
111
112 t.Run(name+"-bd"+strconv.Itoa(testDepth)+"........", benchmarkOneFn(fns))
113 benchmarkGroupReset()
114 }
115
116 func benchmarkQuickSuite(t *testing.B, name string, fns ...func(t *testing.B)) {
117 benchmarkVeryQuickSuite(t, name, fns...)
118
119 // encoded size of TestStruc is between 20K and 30K for bd=1 // consider buffer=1024 * 16 * testDepth
120 testUseIoEncDec = 1024 // (value of defEncByteBufSize): use smaller buffer, and more flushes - it's ok.
121 // testDepth = depth
122 testReinit()
123 benchReinit()
124 t.Run(name+"-bd"+strconv.Itoa(testDepth)+"-buf"+strconv.Itoa(testUseIoEncDec), benchmarkOneFn(fns))
125
126 testUseIoEncDec = 0
127 // testDepth = depth
128 testReinit()
129 benchReinit()
130 t.Run(name+"-bd"+strconv.Itoa(testDepth)+"-io.....", benchmarkOneFn(fns))
131
132 benchmarkGroupReset()
133 }
134
135 /*
136 z='bench_test.go'
137 find . -name "$z" | xargs grep -e '^func Benchmark.*Encode' | \
138 cut -d '(' -f 1 | cut -d ' ' -f 2 | \
139 while read f; do echo "t.Run(\"$f\", $f)"; done &&
140 echo &&
141 find . -name "$z" | xargs grep -e '^func Benchmark.*Decode' | \
142 cut -d '(' -f 1 | cut -d ' ' -f 2 | \
143 while read f; do echo "t.Run(\"$f\", $f)"; done
144 */
145
146 func benchmarkCodecGroup(t *testing.B) {
147 benchmarkDivider()
148 t.Run("Benchmark__Msgpack____Encode", Benchmark__Msgpack____Encode)
149 t.Run("Benchmark__Binc_______Encode", Benchmark__Binc_______Encode)
150 t.Run("Benchmark__Simple_____Encode", Benchmark__Simple_____Encode)
151 t.Run("Benchmark__Cbor_______Encode", Benchmark__Cbor_______Encode)
152 t.Run("Benchmark__Json_______Encode", Benchmark__Json_______Encode)
153 benchmarkDivider()
154 t.Run("Benchmark__Msgpack____Decode", Benchmark__Msgpack____Decode)
155 t.Run("Benchmark__Binc_______Decode", Benchmark__Binc_______Decode)
156 t.Run("Benchmark__Simple_____Decode", Benchmark__Simple_____Decode)
157 t.Run("Benchmark__Cbor_______Decode", Benchmark__Cbor_______Decode)
158 t.Run("Benchmark__Json_______Decode", Benchmark__Json_______Decode)
159 }
160
161 func BenchmarkCodecSuite(t *testing.B) { benchmarkSuite(t, benchmarkCodecGroup) }
162
163 func benchmarkJsonEncodeGroup(t *testing.B) {
164 t.Run("Benchmark__Json_______Encode", Benchmark__Json_______Encode)
165 }
166
167 func benchmarkJsonDecodeGroup(t *testing.B) {
168 t.Run("Benchmark__Json_______Decode", Benchmark__Json_______Decode)
169 }
170
171 func benchmarkCborEncodeGroup(t *testing.B) {
172 t.Run("Benchmark__Cbor_______Encode", Benchmark__Cbor_______Encode)
173 }
174
175 func benchmarkCborDecodeGroup(t *testing.B) {
176 t.Run("Benchmark__Cbor_______Decode", Benchmark__Cbor_______Decode)
177 }
178
179 func BenchmarkCodecQuickSuite(t *testing.B) {
180 benchmarkQuickSuite(t, "cbor", benchmarkCborEncodeGroup)
181 benchmarkQuickSuite(t, "cbor", benchmarkCborDecodeGroup)
182 benchmarkQuickSuite(t, "json", benchmarkJsonEncodeGroup)
183 benchmarkQuickSuite(t, "json", benchmarkJsonDecodeGroup)
184
185 // depths := [...]int{1, 4}
186 // for _, d := range depths {
187 // benchmarkQuickSuite(t, d, benchmarkJsonEncodeGroup)
188 // benchmarkQuickSuite(t, d, benchmarkJsonDecodeGroup)
189 // }
190
191 // benchmarkQuickSuite(t, 1, benchmarkJsonEncodeGroup)
192 // benchmarkQuickSuite(t, 4, benchmarkJsonEncodeGroup)
193 // benchmarkQuickSuite(t, 1, benchmarkJsonDecodeGroup)
194 // benchmarkQuickSuite(t, 4, benchmarkJsonDecodeGroup)
195
196 // benchmarkQuickSuite(t, 1, benchmarkJsonEncodeGroup, benchmarkJsonDecodeGroup)
197 // benchmarkQuickSuite(t, 4, benchmarkJsonEncodeGroup, benchmarkJsonDecodeGroup)
198 // benchmarkQuickSuite(t, benchmarkJsonEncodeGroup)
199 // benchmarkQuickSuite(t, benchmarkJsonDecodeGroup)
200 }