Debian Codegraph @

codec/bench/x_bench_test.go

raw symbols SCIP
1 //go:build x && !generated
2 // +build x,!generated
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 import (
10 "bytes"
11 "testing"
12
13 gcbor "bitbucket.org/bodhisnarkva/cbor/go"
14 "github.com/Sereal/Sereal/Go/sereal"
15 xdr "github.com/davecgh/go-xdr/xdr2"
16 fxcbor "github.com/fxamacker/cbor/v2"
17 mgobson "github.com/globalsign/mgo/bson"
18 jsoniter "github.com/json-iterator/go"
19 vmsgpack "github.com/vmihailenco/msgpack/v4"
20 "go.mongodb.org/mongo-driver/bson"
21 )
22
23 /*
24 To update all these, use:
25 go get -u github.com/tinylib/msgp/msgp github.com/tinylib/msgp \
26 github.com/pquerna/ffjson/ffjson github.com/pquerna/ffjson \
27 github.com/Sereal/Sereal/Go/sereal \
28 bitbucket.org/bodhisnarkva/cbor/go \
29 github.com/davecgh/go-xdr/xdr2 \
30 github.com/globalsign/mgo/bson \
31 github.com/vmihailenco/msgpack/v4 /
32 github.com/json-iterator/go \
33 github.com/fxamacker/cbor/v2 \
34 github.com/mailru/easyjson/...
35
36 Known Issues with external libraries:
37 - msgp io.R/W support doesn't work. It throws error
38
39 */
40
41 func init() {
42 testPreInitFns = append(testPreInitFns, benchXPreInit)
43 _ = bson.NewDecoder
44 }
45
46 func benchXPreInit() {
47 benchCheckers = append(benchCheckers,
48 benchChecker{"json-iter", fnJsonIterEncodeFn, fnJsonIterDecodeFn},
49 benchChecker{"v-msgpack", fnVMsgpackEncodeFn, fnVMsgpackDecodeFn},
50 benchChecker{"bson", fnBsonEncodeFn, fnBsonDecodeFn},
51 benchChecker{"mgobson", fnMgobsonEncodeFn, fnMgobsonDecodeFn},
52 benchChecker{"fxcbor", fnFxcborEncodeFn, fnFxcborDecodeFn},
53 // place codecs with issues at the end, so as not to make results too ugly
54 benchChecker{"gcbor", fnGcborEncodeFn, fnGcborDecodeFn}, // this logs fat ugly message, but we log.SetOutput(ioutil.Discard)
55 benchChecker{"xdr", fnXdrEncodeFn, fnXdrDecodeFn},
56 benchChecker{"sereal", fnSerealEncodeFn, fnSerealDecodeFn},
57 )
58 }
59
60 func fnVMsgpackEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
61 if testUseIoEncDec >= 0 {
62 buf := bytes.NewBuffer(bsIn[:0]) // new(bytes.Buffer)
63 err := vmsgpack.NewEncoder(buf).Encode(ts)
64 return buf.Bytes(), err
65 }
66 return vmsgpack.Marshal(ts)
67 }
68
69 func fnVMsgpackDecodeFn(buf []byte, ts interface{}) error {
70 if testUseIoEncDec >= 0 {
71 return vmsgpack.NewDecoder(bytes.NewReader(buf)).Decode(ts)
72 }
73 return vmsgpack.Unmarshal(buf, ts)
74 }
75
76 func fnBsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
77 return bson.Marshal(ts)
78 }
79
80 func fnBsonDecodeFn(buf []byte, ts interface{}) error {
81 return bson.Unmarshal(buf, ts)
82 }
83
84 func fnMgobsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
85 return mgobson.Marshal(ts)
86 }
87
88 func fnMgobsonDecodeFn(buf []byte, ts interface{}) error {
89 return mgobson.Unmarshal(buf, ts)
90 }
91
92 func fnJsonIterEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
93 if testUseIoEncDec >= 0 {
94 buf := bytes.NewBuffer(bsIn[:0]) // new(bytes.Buffer)
95 err := jsoniter.NewEncoder(buf).Encode(ts)
96 return buf.Bytes(), err
97 }
98 return jsoniter.Marshal(ts)
99 }
100
101 func fnJsonIterDecodeFn(buf []byte, ts interface{}) error {
102 if testUseIoEncDec >= 0 {
103 return jsoniter.NewDecoder(bytes.NewReader(buf)).Decode(ts)
104 }
105 return jsoniter.Unmarshal(buf, ts)
106 }
107
108 func fnFxcborEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
109 if testUseIoEncDec >= 0 {
110 buf := bytes.NewBuffer(bsIn[:0])
111 err := fxcbor.NewEncoder(buf).Encode(ts)
112 return buf.Bytes(), err
113 }
114 return fxcbor.Marshal(ts)
115 }
116
117 func fnFxcborDecodeFn(buf []byte, ts interface{}) error {
118 if testUseIoEncDec >= 0 {
119 return fxcbor.NewDecoder(bytes.NewReader(buf)).Decode(ts)
120 }
121 return fxcbor.Unmarshal(buf, ts)
122 }
123
124 func fnXdrEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
125 buf := fnBenchmarkByteBuf(bsIn)
126 i, err := xdr.Marshal(buf, ts)
127 return buf.Bytes()[:i], err
128 }
129
130 func fnXdrDecodeFn(buf []byte, ts interface{}) error {
131 _, err := xdr.Unmarshal(bytes.NewReader(buf), ts)
132 return err
133 }
134
135 func fnSerealEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
136 return sereal.Marshal(ts)
137 }
138
139 func fnSerealDecodeFn(buf []byte, ts interface{}) error {
140 return sereal.Unmarshal(buf, ts)
141 }
142
143 func fnGcborEncodeFn(ts interface{}, bsIn []byte) (bs []byte, err error) {
144 buf := fnBenchmarkByteBuf(bsIn)
145 err = gcbor.NewEncoder(buf).Encode(ts)
146 return buf.Bytes(), err
147 }
148
149 func fnGcborDecodeFn(buf []byte, ts interface{}) error {
150 return gcbor.NewDecoder(bytes.NewReader(buf)).Decode(ts)
151 }
152
153 func Benchmark__JsonIter___Encode(b *testing.B) {
154 fnBenchmarkEncode(b, "jsoniter", benchTs, fnJsonIterEncodeFn)
155 }
156
157 func Benchmark__JsonIter___Decode(b *testing.B) {
158 fnBenchmarkDecode(b, "jsoniter", benchTs, fnJsonIterEncodeFn, fnJsonIterDecodeFn, fnBenchNewTs)
159 }
160
161 func Benchmark__Fxcbor_____Encode(b *testing.B) {
162 fnBenchmarkEncode(b, "fxcbor", benchTs, fnFxcborEncodeFn)
163 }
164
165 func Benchmark__Fxcbor_____Decode(b *testing.B) {
166 fnBenchmarkDecode(b, "fxcbor", benchTs, fnFxcborEncodeFn, fnFxcborDecodeFn, fnBenchNewTs)
167 }
168
169 // Place codecs with issues at the bottom, so as not to make results look too ugly.
170
171 func Benchmark__Mgobson____Encode(b *testing.B) {
172 fnBenchmarkEncode(b, "mgobson", benchTs, fnMgobsonEncodeFn)
173 }
174
175 func Benchmark__Mgobson____Decode(b *testing.B) {
176 fnBenchmarkDecode(b, "mgobson", benchTs, fnMgobsonEncodeFn, fnMgobsonDecodeFn, fnBenchNewTs)
177 }
178
179 func Benchmark__Bson_______Encode(b *testing.B) {
180 fnBenchmarkEncode(b, "bson", benchTs, fnBsonEncodeFn)
181 }
182
183 func Benchmark__Bson_______Decode(b *testing.B) {
184 fnBenchmarkDecode(b, "bson", benchTs, fnBsonEncodeFn, fnBsonDecodeFn, fnBenchNewTs)
185 }
186
187 func Benchmark__VMsgpack___Encode(b *testing.B) {
188 fnBenchmarkEncode(b, "v-msgpack", benchTs, fnVMsgpackEncodeFn)
189 }
190
191 func Benchmark__VMsgpack___Decode(b *testing.B) {
192 fnBenchmarkDecode(b, "v-msgpack", benchTs, fnVMsgpackEncodeFn, fnVMsgpackDecodeFn, fnBenchNewTs)
193 }
194
195 func Benchmark__Gcbor______Encode(b *testing.B) {
196 fnBenchmarkEncode(b, "gcbor", benchTs, fnGcborEncodeFn)
197 }
198
199 func Benchmark__Gcbor______Decode(b *testing.B) {
200 fnBenchmarkDecode(b, "gcbor", benchTs, fnGcborEncodeFn, fnGcborDecodeFn, fnBenchNewTs)
201 }
202
203 func Benchmark__Xdr________Encode(b *testing.B) {
204 fnBenchmarkEncode(b, "xdr", benchTs, fnXdrEncodeFn)
205 }
206
207 func Benchmark__Xdr________Decode(b *testing.B) {
208 fnBenchmarkDecode(b, "xdr", benchTs, fnXdrEncodeFn, fnXdrDecodeFn, fnBenchNewTs)
209 }
210
211 func Benchmark__Sereal_____Encode(b *testing.B) {
212 fnBenchmarkEncode(b, "sereal", benchTs, fnSerealEncodeFn)
213 }
214
215 func Benchmark__Sereal_____Decode(b *testing.B) {
216 fnBenchmarkDecode(b, "sereal", benchTs, fnSerealEncodeFn, fnSerealDecodeFn, fnBenchNewTs)
217 }