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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
//go:build alltests && go1.7
// +build alltests,go1.7
// Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
// Use of this source code is governed by a MIT license found in the LICENSE file.
package codec
// see notes in z_all_test.go
import (
"strconv"
"sync"
"testing"
"time"
)
var benchmarkGroupOnce sync.Once
var benchmarkGroupSave struct {
testUseIoEncDec int
testUseReset bool
testDepth int
testMapStringKeyOnly bool
}
func benchmarkGroupInitAll() {
testInitAll() // calls flag.Parse
benchmarkGroupSave.testUseIoEncDec = testUseIoEncDec
benchmarkGroupSave.testUseReset = testUseReset
benchmarkGroupSave.testDepth = testDepth
benchmarkGroupSave.testMapStringKeyOnly = testMapStringKeyOnly
}
func benchmarkGroupReset() {
testUseIoEncDec = benchmarkGroupSave.testUseIoEncDec
testUseReset = benchmarkGroupSave.testUseReset
testDepth = benchmarkGroupSave.testDepth
testMapStringKeyOnly = benchmarkGroupSave.testMapStringKeyOnly
}
func benchmarkOneFn(fns []func(*testing.B)) func(*testing.B) {
switch len(fns) {
case 0:
return nil
case 1:
return fns[0]
default:
return func(t *testing.B) {
for _, f := range fns {
f(t)
}
}
}
}
func benchmarkSuiteNoop(b *testing.B) {
testOnce.Do(testInitAll)
b.ResetTimer()
for i := 0; i < b.N; i++ {
time.Sleep(10 * time.Millisecond)
}
}
func benchmarkSuite(t *testing.B, fns ...func(t *testing.B)) {
benchmarkGroupOnce.Do(benchmarkGroupInitAll)
f := benchmarkOneFn(fns)
// find . -name "*_test.go" | xargs grep -e 'flag.' | cut -d '&' -f 2 | cut -d ',' -f 1 | grep -e '^bench'
testReinit() // so flag.Parse() is called first, and never called again
benchReinit()
benchmarkGroupReset()
testReinit()
benchReinit()
t.Run("options-false...", f)
benchmarkGroupReset()
testUseIoEncDec = 1024
testReinit()
benchReinit()
t.Run("use-bufio-!bytes", f)
benchmarkGroupReset()
testUseReset = true
testReinit()
benchReinit()
t.Run("reset-enc-dec...", f)
benchmarkGroupReset()
}
func benchmarkVeryQuickSuite(t *testing.B, name string, fns ...func(t *testing.B)) {
benchmarkDivider()
benchmarkGroupOnce.Do(benchmarkGroupInitAll)
benchmarkGroupReset()
// bd=1 2 | ti=-1, 1024 |
testUseIoEncDec = -1
// testDepth = depth
testReinit()
benchReinit()
t.Run(name+"-bd"+strconv.Itoa(testDepth)+"........", benchmarkOneFn(fns))
benchmarkGroupReset()
}
func benchmarkQuickSuite(t *testing.B, name string, fns ...func(t *testing.B)) {
benchmarkVeryQuickSuite(t, name, fns...)
// encoded size of TestStruc is between 20K and 30K for bd=1 // consider buffer=1024 * 16 * testDepth
testUseIoEncDec = 1024 // (value of defEncByteBufSize): use smaller buffer, and more flushes - it's ok.
// testDepth = depth
testReinit()
benchReinit()
t.Run(name+"-bd"+strconv.Itoa(testDepth)+"-buf"+strconv.Itoa(testUseIoEncDec), benchmarkOneFn(fns))
testUseIoEncDec = 0
// testDepth = depth
testReinit()
benchReinit()
t.Run(name+"-bd"+strconv.Itoa(testDepth)+"-io.....", benchmarkOneFn(fns))
benchmarkGroupReset()
}
/*
z='bench_test.go'
find . -name "$z" | xargs grep -e '^func Benchmark.*Encode' | \
cut -d '(' -f 1 | cut -d ' ' -f 2 | \
while read f; do echo "t.Run(\"$f\", $f)"; done &&
echo &&
find . -name "$z" | xargs grep -e '^func Benchmark.*Decode' | \
cut -d '(' -f 1 | cut -d ' ' -f 2 | \
while read f; do echo "t.Run(\"$f\", $f)"; done
*/
func benchmarkCodecGroup(t *testing.B) {
benchmarkDivider()
t.Run("Benchmark__Msgpack____Encode", Benchmark__Msgpack____Encode)
t.Run("Benchmark__Binc_______Encode", Benchmark__Binc_______Encode)
t.Run("Benchmark__Simple_____Encode", Benchmark__Simple_____Encode)
t.Run("Benchmark__Cbor_______Encode", Benchmark__Cbor_______Encode)
t.Run("Benchmark__Json_______Encode", Benchmark__Json_______Encode)
benchmarkDivider()
t.Run("Benchmark__Msgpack____Decode", Benchmark__Msgpack____Decode)
t.Run("Benchmark__Binc_______Decode", Benchmark__Binc_______Decode)
t.Run("Benchmark__Simple_____Decode", Benchmark__Simple_____Decode)
t.Run("Benchmark__Cbor_______Decode", Benchmark__Cbor_______Decode)
t.Run("Benchmark__Json_______Decode", Benchmark__Json_______Decode)
}
func BenchmarkCodecSuite(t *testing.B) { benchmarkSuite(t, benchmarkCodecGroup) }
func benchmarkJsonEncodeGroup(t *testing.B) {
t.Run("Benchmark__Json_______Encode", Benchmark__Json_______Encode)
}
func benchmarkJsonDecodeGroup(t *testing.B) {
t.Run("Benchmark__Json_______Decode", Benchmark__Json_______Decode)
}
func benchmarkCborEncodeGroup(t *testing.B) {
t.Run("Benchmark__Cbor_______Encode", Benchmark__Cbor_______Encode)
}
func benchmarkCborDecodeGroup(t *testing.B) {
t.Run("Benchmark__Cbor_______Decode", Benchmark__Cbor_______Decode)
}
func BenchmarkCodecQuickSuite(t *testing.B) {
benchmarkQuickSuite(t, "cbor", benchmarkCborEncodeGroup)
benchmarkQuickSuite(t, "cbor", benchmarkCborDecodeGroup)
benchmarkQuickSuite(t, "json", benchmarkJsonEncodeGroup)
benchmarkQuickSuite(t, "json", benchmarkJsonDecodeGroup)
// depths := [...]int{1, 4}
// for _, d := range depths {
// benchmarkQuickSuite(t, d, benchmarkJsonEncodeGroup)
// benchmarkQuickSuite(t, d, benchmarkJsonDecodeGroup)
// }
// benchmarkQuickSuite(t, 1, benchmarkJsonEncodeGroup)
// benchmarkQuickSuite(t, 4, benchmarkJsonEncodeGroup)
// benchmarkQuickSuite(t, 1, benchmarkJsonDecodeGroup)
// benchmarkQuickSuite(t, 4, benchmarkJsonDecodeGroup)
// benchmarkQuickSuite(t, 1, benchmarkJsonEncodeGroup, benchmarkJsonDecodeGroup)
// benchmarkQuickSuite(t, 4, benchmarkJsonEncodeGroup, benchmarkJsonDecodeGroup)
// benchmarkQuickSuite(t, benchmarkJsonEncodeGroup)
// benchmarkQuickSuite(t, benchmarkJsonDecodeGroup)
}
|