| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package codec |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/mailru/easyjson" |
| 16 |
"github.com/pquerna/ffjson/ffjson" |
| 17 |
"github.com/tinylib/msgp/msgp" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
const benchXGenSkipFFJSON = true |
| 37 |
|
| 38 |
func init() { |
| 39 |
testPreInitFns = append(testPreInitFns, benchXGenPreInit) |
| 40 |
} |
| 41 |
|
| 42 |
func benchXGenPreInit() { |
| 43 |
benchCheckers = append(benchCheckers, |
| 44 |
benchChecker{"msgp", fnMsgpEncodeFn, fnMsgpDecodeFn}, |
| 45 |
benchChecker{"easyjson", fnEasyjsonEncodeFn, fnEasyjsonDecodeFn}, |
| 46 |
benchChecker{"ffjson", fnFfjsonEncodeFn, fnFfjsonDecodeFn}, |
| 47 |
) |
| 48 |
} |
| 49 |
|
| 50 |
func fnEasyjsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) { |
| 51 |
ts2, ok := ts.(easyjson.Marshaler) |
| 52 |
if !ok || ts2 == nil { |
| 53 |
return nil, errors.New("easyjson: input is not a easyjson.Marshaler") |
| 54 |
} |
| 55 |
if testUseIoEncDec >= 0 { |
| 56 |
buf := bytes.NewBuffer(bsIn[:0]) |
| 57 |
_, err := easyjson.MarshalToWriter(ts2, buf) |
| 58 |
return buf.Bytes(), err |
| 59 |
} |
| 60 |
return easyjson.Marshal(ts2) |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
func fnEasyjsonDecodeFn(buf []byte, ts interface{}) error { |
| 65 |
ts2, ok := ts.(easyjson.Unmarshaler) |
| 66 |
if !ok { |
| 67 |
return errors.New("easyjson: input is not a easyjson.Unmarshaler") |
| 68 |
} |
| 69 |
if testUseIoEncDec >= 0 { |
| 70 |
return easyjson.UnmarshalFromReader(bytes.NewReader(buf), ts2) |
| 71 |
} |
| 72 |
return easyjson.Unmarshal(buf, ts2) |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
func fnFfjsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) { |
| 77 |
if benchXGenSkipFFJSON { |
| 78 |
panic(errors.New("ffjson: generated code fails to compile; checked 2022-12-26")) |
| 79 |
} |
| 80 |
|
| 81 |
return ffjson.Marshal(ts) |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
func fnFfjsonDecodeFn(buf []byte, ts interface{}) error { |
| 86 |
if benchXGenSkipFFJSON { |
| 87 |
panic(errors.New("ffjson: generated code fails to compile; checked 2022-12-26")) |
| 88 |
} |
| 89 |
|
| 90 |
return ffjson.Unmarshal(buf, ts) |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
func fnMsgpEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) { |
| 95 |
if _, ok := ts.(msgp.Encodable); !ok { |
| 96 |
return nil, fmt.Errorf("msgp: input of type %T is not a msgp.Encodable", ts) |
| 97 |
} |
| 98 |
if testUseIoEncDec >= 0 { |
| 99 |
buf := fnBenchmarkByteBuf(bsIn) |
| 100 |
err := ts.(msgp.Encodable).EncodeMsg(msgp.NewWriter(buf)) |
| 101 |
return buf.Bytes(), err |
| 102 |
} |
| 103 |
return ts.(msgp.Marshaler).MarshalMsg(bsIn[:0]) |
| 104 |
} |
| 105 |
|
| 106 |
func fnMsgpDecodeFn(buf []byte, ts interface{}) (err error) { |
| 107 |
if _, ok := ts.(msgp.Decodable); !ok { |
| 108 |
return fmt.Errorf("msgp: input of type %T is not a msgp.Decodable", ts) |
| 109 |
} |
| 110 |
if testUseIoEncDec >= 0 { |
| 111 |
err = ts.(msgp.Decodable).DecodeMsg(msgp.NewReader(bytes.NewReader(buf))) |
| 112 |
return |
| 113 |
} |
| 114 |
_, err = ts.(msgp.Unmarshaler).UnmarshalMsg(buf) |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
func Benchmark__Msgp_______Encode(b *testing.B) { |
| 119 |
fnBenchmarkEncode(b, "msgp", benchTs, fnMsgpEncodeFn) |
| 120 |
} |
| 121 |
|
| 122 |
func Benchmark__Msgp_______Decode(b *testing.B) { |
| 123 |
fnBenchmarkDecode(b, "msgp", benchTs, fnMsgpEncodeFn, fnMsgpDecodeFn, fnBenchNewTs) |
| 124 |
} |
| 125 |
|
| 126 |
func Benchmark__Easyjson___Encode(b *testing.B) { |
| 127 |
fnBenchmarkEncode(b, "easyjson", benchTs, fnEasyjsonEncodeFn) |
| 128 |
} |
| 129 |
|
| 130 |
func Benchmark__Easyjson___Decode(b *testing.B) { |
| 131 |
fnBenchmarkDecode(b, "easyjson", benchTs, fnEasyjsonEncodeFn, fnEasyjsonDecodeFn, fnBenchNewTs) |
| 132 |
} |
| 133 |
|
| 134 |
func Benchmark__Ffjson_____Encode(b *testing.B) { |
| 135 |
fnBenchmarkEncode(b, "ffjson", benchTs, fnFfjsonEncodeFn) |
| 136 |
} |
| 137 |
|
| 138 |
func Benchmark__Ffjson_____Decode(b *testing.B) { |
| 139 |
fnBenchmarkDecode(b, "ffjson", benchTs, fnFfjsonEncodeFn, fnFfjsonDecodeFn, fnBenchNewTs) |
| 140 |
} |