File: x_bench_gen_test.go

package info (click to toggle)
golang-github-ugorji-go-codec 1.2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, experimental, forky, sid, trixie
  • size: 2,532 kB
  • sloc: sh: 462; python: 100; makefile: 4
file content (140 lines) | stat: -rw-r--r-- 4,161 bytes parent folder | download
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
//go:build x && generated
// +build x,generated

// 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

import (
	"bytes"
	"errors"
	"fmt"
	"testing"

	"github.com/mailru/easyjson"
	"github.com/pquerna/ffjson/ffjson"
	"github.com/tinylib/msgp/msgp"
)

/*
 To update all these, use:
 go get -u github.com/tinylib/msgp/msgp github.com/tinylib/msgp \
           github.com/pquerna/ffjson/ffjson github.com/pquerna/ffjson \
           github.com/mailru/easyjson/...

 Known Issues with external libraries:
 - msgp io.R/W support doesn't work. It throws error
 - ffjson: generated code fails to compile as of latest commit checked on 2022-12-26

*/

// benchXGenSkipFFJSON triggers whether we throw a panic based
// on whether ffjson generated code compiles.
//
// MARKER: change if ffjson is updated and compiles successfully
const benchXGenSkipFFJSON = true

func init() {
	testPreInitFns = append(testPreInitFns, benchXGenPreInit)
}

func benchXGenPreInit() {
	benchCheckers = append(benchCheckers,
		benchChecker{"msgp", fnMsgpEncodeFn, fnMsgpDecodeFn},
		benchChecker{"easyjson", fnEasyjsonEncodeFn, fnEasyjsonDecodeFn},
		benchChecker{"ffjson", fnFfjsonEncodeFn, fnFfjsonDecodeFn},
	)
}

func fnEasyjsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
	ts2, ok := ts.(easyjson.Marshaler)
	if !ok || ts2 == nil {
		return nil, errors.New("easyjson: input is not a easyjson.Marshaler")
	}
	if testUseIoEncDec >= 0 {
		buf := bytes.NewBuffer(bsIn[:0]) // new(bytes.Buffer)
		_, err := easyjson.MarshalToWriter(ts2, buf)
		return buf.Bytes(), err
	}
	return easyjson.Marshal(ts2)
	// return ts.(json.Marshaler).MarshalJSON()
}

func fnEasyjsonDecodeFn(buf []byte, ts interface{}) error {
	ts2, ok := ts.(easyjson.Unmarshaler)
	if !ok {
		return errors.New("easyjson: input is not a easyjson.Unmarshaler")
	}
	if testUseIoEncDec >= 0 {
		return easyjson.UnmarshalFromReader(bytes.NewReader(buf), ts2)
	}
	return easyjson.Unmarshal(buf, ts2)
	// return ts.(json.Unmarshaler).UnmarshalJSON(buf)
}

func fnFfjsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
	if benchXGenSkipFFJSON {
		panic(errors.New("ffjson: generated code fails to compile; checked 2022-12-26"))
	}

	return ffjson.Marshal(ts)
	// return ts.(json.Marshaler).MarshalJSON()
}

func fnFfjsonDecodeFn(buf []byte, ts interface{}) error {
	if benchXGenSkipFFJSON {
		panic(errors.New("ffjson: generated code fails to compile; checked 2022-12-26"))
	}

	return ffjson.Unmarshal(buf, ts)
	// return ts.(json.Unmarshaler).UnmarshalJSON(buf)
}

func fnMsgpEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
	if _, ok := ts.(msgp.Encodable); !ok {
		return nil, fmt.Errorf("msgp: input of type %T is not a msgp.Encodable", ts)
	}
	if testUseIoEncDec >= 0 {
		buf := fnBenchmarkByteBuf(bsIn)
		err := ts.(msgp.Encodable).EncodeMsg(msgp.NewWriter(buf))
		return buf.Bytes(), err
	}
	return ts.(msgp.Marshaler).MarshalMsg(bsIn[:0]) // msgp appends to slice.
}

func fnMsgpDecodeFn(buf []byte, ts interface{}) (err error) {
	if _, ok := ts.(msgp.Decodable); !ok {
		return fmt.Errorf("msgp: input of type %T is not a msgp.Decodable", ts)
	}
	if testUseIoEncDec >= 0 {
		err = ts.(msgp.Decodable).DecodeMsg(msgp.NewReader(bytes.NewReader(buf)))
		return
	}
	_, err = ts.(msgp.Unmarshaler).UnmarshalMsg(buf)
	return
}

func Benchmark__Msgp_______Encode(b *testing.B) {
	fnBenchmarkEncode(b, "msgp", benchTs, fnMsgpEncodeFn)
}

func Benchmark__Msgp_______Decode(b *testing.B) {
	fnBenchmarkDecode(b, "msgp", benchTs, fnMsgpEncodeFn, fnMsgpDecodeFn, fnBenchNewTs)
}

func Benchmark__Easyjson___Encode(b *testing.B) {
	fnBenchmarkEncode(b, "easyjson", benchTs, fnEasyjsonEncodeFn)
}

func Benchmark__Easyjson___Decode(b *testing.B) {
	fnBenchmarkDecode(b, "easyjson", benchTs, fnEasyjsonEncodeFn, fnEasyjsonDecodeFn, fnBenchNewTs)
}

func Benchmark__Ffjson_____Encode(b *testing.B) {
	fnBenchmarkEncode(b, "ffjson", benchTs, fnFfjsonEncodeFn)
}

func Benchmark__Ffjson_____Decode(b *testing.B) {
	fnBenchmarkDecode(b, "ffjson", benchTs, fnFfjsonEncodeFn, fnFfjsonDecodeFn, fnBenchNewTs)
}