1
2
3
4 package codec
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 import (
44 "bytes"
45 "flag"
46 "io"
47 "io/ioutil"
48 "log"
49 "sync"
50 )
51
52
53
54 type testHED struct {
55 H Handle
56 E *Encoder
57 D *Decoder
58 }
59
60 type ioReaderWrapper struct {
61 r io.Reader
62 }
63
64 func (x ioReaderWrapper) Read(p []byte) (n int, err error) {
65 return x.r.Read(p)
66 }
67
68 type ioWriterWrapper struct {
69 w io.Writer
70 }
71
72 func (x ioWriterWrapper) Write(p []byte) (n int, err error) {
73 return x.w.Write(p)
74 }
75
76 var (
77
78 testMsgpackH = &MsgpackHandle{}
79 testBincH = &BincHandle{}
80 testSimpleH = &SimpleHandle{}
81 testCborH = &CborHandle{}
82 testJsonH = &JsonHandle{}
83
84 testHandles []Handle
85 testPreInitFns []func()
86 testPostInitFns []func()
87
88 testOnce sync.Once
89
90 testHEDs []testHED
91 )
92
93
94 var (
95 testVerbose bool
96
97
98
99 testDepth int
100
101 testMaxInitLen int
102
103 testUseReset bool
104 testUseParallel bool
105
106 testSkipIntf bool
107
108 testUseIoEncDec int
109 testUseIoWrapper bool
110
111 testNumRepeatString int
112
113 testRpcBufsize int
114 testMapStringKeyOnly bool
115
116 testBenchmarkNoConfig bool
117 )
118
119
120 var (
121 testEncodeOptions EncodeOptions
122 testDecodeOptions DecodeOptions
123 testRPCOptions RPCOptions
124 )
125
126 func init() {
127 log.SetOutput(ioutil.Discard)
128 testHEDs = make([]testHED, 0, 32)
129 testHandles = append(testHandles,
130
131 testMsgpackH, testBincH, testSimpleH, testCborH, testJsonH)
132
133 testJsonH.HTMLCharsAsIs = true
134
135 testInitFlags()
136 benchInitFlags()
137 }
138
139 func testInitFlags() {
140 var bIgnore bool
141
142 flag.BoolVar(&testVerbose, "tv", false, "Text Extra Verbose Logging if -v if set")
143 flag.IntVar(&testUseIoEncDec, "ti", -1, "Use IO Reader/Writer for Marshal/Unmarshal ie >= 0")
144 flag.BoolVar(&testUseIoWrapper, "tiw", false, "Wrap the IO Reader/Writer with a base pass-through reader/writer")
145
146 flag.BoolVar(&testSkipIntf, "tf", false, "Skip Interfaces")
147 flag.BoolVar(&testUseReset, "tr", false, "Use Reset")
148 flag.BoolVar(&testUseParallel, "tp", false, "Run tests in parallel")
149 flag.IntVar(&testNumRepeatString, "trs", 8, "Create string variables by repeating a string N times")
150 flag.BoolVar(&bIgnore, "tm", true, "(Deprecated) Use Must(En|De)code")
151
152 flag.IntVar(&testMaxInitLen, "tx", 0, "Max Init Len")
153
154 flag.IntVar(&testDepth, "tsd", 0, "Test Struc Depth")
155 flag.BoolVar(&testMapStringKeyOnly, "tsk", false, "use maps with string keys only")
156 }
157
158 func benchInitFlags() {
159 flag.BoolVar(&testBenchmarkNoConfig, "bnc", false, "benchmarks: do not make configuration changes for fair benchmarking")
160
161 flag.BoolVar(&testMapStringKeyOnly, "bs", false, "benchmarks: use maps with string keys only")
162 flag.IntVar(&testDepth, "bd", 1, "Benchmarks: Test Struc Depth")
163 }
164
165 func testHEDGet(h Handle) *testHED {
166 for i := range testHEDs {
167 v := &testHEDs[i]
168 if v.H == h {
169 return v
170 }
171 }
172 testHEDs = append(testHEDs, testHED{h, NewEncoder(nil, h), NewDecoder(nil, h)})
173 return &testHEDs[len(testHEDs)-1]
174 }
175
176 func testReinit() {
177 testOnce = sync.Once{}
178 testHEDs = nil
179 }
180
181 func testInitAll() {
182
183 if !flag.Parsed() {
184 flag.Parse()
185 }
186 for _, f := range testPreInitFns {
187 f()
188 }
189 for _, f := range testPostInitFns {
190 f()
191 }
192 }
193
194 func testSharedCodecEncode(ts interface{}, bsIn []byte, fn func([]byte) *bytes.Buffer,
195 h Handle, bh *BasicHandle, useMust bool) (bs []byte, err error) {
196
197 var e *Encoder
198 var buf *bytes.Buffer
199 if testUseReset && !testUseParallel {
200 e = testHEDGet(h).E
201 } else {
202 e = NewEncoder(nil, h)
203 }
204 var oldWriteBufferSize int
205 if testUseIoEncDec >= 0 {
206 buf = fn(bsIn)
207
208 oldWriteBufferSize = bh.WriterBufferSize
209 bh.WriterBufferSize = testUseIoEncDec
210 if testUseIoWrapper {
211 e.Reset(ioWriterWrapper{buf})
212 } else {
213 e.Reset(buf)
214 }
215 } else {
216 bs = bsIn
217 e.ResetBytes(&bs)
218 }
219 if useMust {
220 e.MustEncode(ts)
221 } else {
222 err = e.Encode(ts)
223 }
224 if testUseIoEncDec >= 0 {
225 bs = buf.Bytes()
226 bh.WriterBufferSize = oldWriteBufferSize
227 }
228 return
229 }
230
231 func testSharedCodecDecoder(bs []byte, h Handle, bh *BasicHandle) (d *Decoder, oldReadBufferSize int) {
232
233 if testUseReset && !testUseParallel {
234 d = testHEDGet(h).D
235 } else {
236 d = NewDecoder(nil, h)
237 }
238 if testUseIoEncDec >= 0 {
239 buf := bytes.NewReader(bs)
240 oldReadBufferSize = bh.ReaderBufferSize
241 bh.ReaderBufferSize = testUseIoEncDec
242 if testUseIoWrapper {
243 d.Reset(ioReaderWrapper{buf})
244 } else {
245 d.Reset(buf)
246 }
247 } else {
248 d.ResetBytes(bs)
249 }
250 return
251 }
252
253 func testSharedCodecDecoderAfter(d *Decoder, oldReadBufferSize int, bh *BasicHandle) {
254 if testUseIoEncDec >= 0 {
255 bh.ReaderBufferSize = oldReadBufferSize
256 }
257 }
258
259 func testSharedCodecDecode(bs []byte, ts interface{}, h Handle, bh *BasicHandle, useMust bool) (err error) {
260 d, oldReadBufferSize := testSharedCodecDecoder(bs, h, bh)
261 if useMust {
262 d.MustDecode(ts)
263 } else {
264 err = d.Decode(ts)
265 }
266 testSharedCodecDecoderAfter(d, oldReadBufferSize, bh)
267 return
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 func fnBenchmarkByteBuf(bsIn []byte) (buf *bytes.Buffer) {
336
337
338 buf = bytes.NewBuffer(bsIn)
339 buf.Truncate(0)
340 return
341 }
342
343
344
345
346
347
348
349
350
View as plain text