Skip to content

Commit

Permalink
codec/bench: benchmark using codec's encoder functions
Browse files Browse the repository at this point in the history
To ensure same sequence of bytes to be decoded, always encode using codec encoder.
  • Loading branch information
ugorji committed Feb 3, 2023
1 parent 2a1ab51 commit 308de61
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 0 additions & 1 deletion codec/bench/bench.sh
Expand Up @@ -160,7 +160,6 @@ _suite_very_quick_json_non_suite() {
local t="${1:-x}"
shift
echo ">>>> very quick json bench"
# local tags=( "x" "x generated" )
local tags=("${t}" "${t} generated" "${t} codec.safe" "${t} generated codec.safe" "${t} codec.notfastpath")
local js=( En De )
for t in "${tags[@]}"; do
Expand Down
18 changes: 17 additions & 1 deletion codec/bench/bench_test.go
Expand Up @@ -36,6 +36,7 @@ package codec
import (
"reflect"
"runtime"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -238,9 +239,24 @@ func fnBenchmarkDecode(b *testing.B, encName string, ts interface{},
) {
defer benchRecoverPanic(b)
testOnce.Do(testInitAll)
// ignore method params: ts and newfn, and work on benchTs and TestStruc directly

// MARKER: to ensure same sequence of bytes to be decoded, always encode using codec encoder.
//
// ignore method params:
// - ts: use benchTs instead
// - newfn: use TestStruc instead
// - benchEncFn: use codec's encfn instead (based on name/format)

ts = benchTs

if strings.Index(encName, "json") != -1 {
encfn = fnJsonEncodeFn
} else if strings.Index(encName, "cbor") != -1 {
encfn = fnCborEncodeFn
} else if strings.Index(encName, "msgpack") != -1 {
encfn = fnMsgpackEncodeFn
}

buf := make([]byte, 0, approxSize)
buf, err := encfn(ts, buf)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions codec/bench/z_all_x_bench_test.go
Expand Up @@ -134,3 +134,11 @@ func BenchmarkCodecQuickAllJsonSuite(t *testing.B) {
// benchmarkQuickSuite(t, benchmarkAllJsonEncodeGroup)
// benchmarkQuickSuite(t, benchmarkAllJsonDecodeGroup)
}

func BenchmarkCodecQuickEncode(t *testing.B) {
benchmarkQuickSuite(t, "json-all", benchmarkAllJsonEncodeGroup)
}

func BenchmarkCodecQuickDecode(t *testing.B) {
benchmarkQuickSuite(t, "json-all", benchmarkAllJsonDecodeGroup)
}

0 comments on commit 308de61

Please sign in to comment.