shared_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. // This file sets up the variables used, including testInitFns.
  5. // Each file should add initialization that should be performed
  6. // after flags are parsed.
  7. //
  8. // init is a multi-step process:
  9. // - setup vars (handled by init functions in each file)
  10. // - parse flags
  11. // - setup derived vars (handled by pre-init registered functions - registered in init function)
  12. // - post init (handled by post-init registered functions - registered in init function)
  13. // This way, no one has to manage carefully control the initialization
  14. // using file names, etc.
  15. //
  16. // Tests which require external dependencies need the -tag=x parameter.
  17. // They should be run as:
  18. // go test -tags=x -run=. <other parameters ...>
  19. // Benchmarks should also take this parameter, to include the sereal, xdr, etc.
  20. // To run against codecgen, etc, make sure you pass extra parameters.
  21. // Example usage:
  22. // go test "-tags=x codecgen" -bench=. <other parameters ...>
  23. //
  24. // To fully test everything:
  25. // go test -tags=x -benchtime=100ms -tv -bg -bi -brw -bu -v -run=. -bench=.
  26. // Handling flags
  27. // codec_test.go will define a set of global flags for testing, including:
  28. // - Use Reset
  29. // - Use IO reader/writer (vs direct bytes)
  30. // - Set Canonical
  31. // - Set InternStrings
  32. // - Use Symbols
  33. //
  34. // This way, we can test them all by running same set of tests with a different
  35. // set of flags.
  36. //
  37. // Following this, all the benchmarks will utilize flags set by codec_test.go
  38. // and will not redefine these "global" flags.
  39. import (
  40. "bytes"
  41. "flag"
  42. "fmt"
  43. "io"
  44. "io/ioutil"
  45. "log"
  46. "sync"
  47. "testing"
  48. )
  49. // DO NOT REMOVE - replacement line for go-codec-bench import declaration tag //
  50. type testHED struct {
  51. H Handle
  52. E *Encoder
  53. D *Decoder
  54. }
  55. type ioReaderWrapper struct {
  56. r io.Reader
  57. }
  58. func (x ioReaderWrapper) Read(p []byte) (n int, err error) {
  59. return x.r.Read(p)
  60. }
  61. type ioWriterWrapper struct {
  62. w io.Writer
  63. }
  64. func (x ioWriterWrapper) Write(p []byte) (n int, err error) {
  65. return x.w.Write(p)
  66. }
  67. var (
  68. // testNoopH = NoopHandle(8)
  69. testMsgpackH = &MsgpackHandle{}
  70. testBincH = &BincHandle{}
  71. testSimpleH = &SimpleHandle{}
  72. testCborH = &CborHandle{}
  73. testJsonH = &JsonHandle{}
  74. testHandles []Handle
  75. testPreInitFns []func()
  76. testPostInitFns []func()
  77. testOnce sync.Once
  78. testHEDs []testHED
  79. )
  80. // flag variables used by tests (and bench)
  81. var (
  82. testDepth int
  83. testVerbose bool
  84. testInitDebug bool
  85. testStructToArray bool
  86. testCanonical bool
  87. testUseReset bool
  88. testSkipIntf bool
  89. testInternStr bool
  90. testUseMust bool
  91. testCheckCircRef bool
  92. testUseIoEncDec int
  93. testUseIoWrapper bool
  94. testMaxInitLen int
  95. testNumRepeatString int
  96. testRpcBufsize int
  97. )
  98. // variables that are not flags, but which can configure the handles
  99. var (
  100. testEncodeOptions EncodeOptions
  101. testDecodeOptions DecodeOptions
  102. )
  103. // flag variables used by bench
  104. var (
  105. benchDoInitBench bool
  106. benchVerify bool
  107. benchUnscientificRes bool = false
  108. benchMapStringKeyOnly bool
  109. //depth of 0 maps to ~400bytes json-encoded string, 1 maps to ~1400 bytes, etc
  110. //For depth>1, we likely trigger stack growth for encoders, making benchmarking unreliable.
  111. benchDepth int
  112. benchInitDebug bool
  113. )
  114. func init() {
  115. log.SetOutput(ioutil.Discard) // don't allow things log to standard out/err
  116. testHEDs = make([]testHED, 0, 32)
  117. testHandles = append(testHandles,
  118. // testNoopH,
  119. testMsgpackH, testBincH, testSimpleH, testCborH, testJsonH)
  120. // set ExplicitRelease on each handle
  121. testMsgpackH.ExplicitRelease = true
  122. testBincH.ExplicitRelease = true
  123. testSimpleH.ExplicitRelease = true
  124. testCborH.ExplicitRelease = true
  125. testJsonH.ExplicitRelease = true
  126. testInitFlags()
  127. benchInitFlags()
  128. }
  129. func testInitFlags() {
  130. // delete(testDecOpts.ExtFuncs, timeTyp)
  131. flag.IntVar(&testDepth, "tsd", 0, "Test Struc Depth")
  132. flag.BoolVar(&testVerbose, "tv", false, "Test Verbose (no longer used - here for compatibility)")
  133. flag.BoolVar(&testInitDebug, "tg", false, "Test Init Debug")
  134. flag.IntVar(&testUseIoEncDec, "ti", -1, "Use IO Reader/Writer for Marshal/Unmarshal ie >= 0")
  135. flag.BoolVar(&testUseIoWrapper, "tiw", false, "Wrap the IO Reader/Writer with a base pass-through reader/writer")
  136. flag.BoolVar(&testStructToArray, "ts", false, "Set StructToArray option")
  137. flag.BoolVar(&testCanonical, "tc", false, "Set Canonical option")
  138. flag.BoolVar(&testInternStr, "te", false, "Set InternStr option")
  139. flag.BoolVar(&testSkipIntf, "tf", false, "Skip Interfaces")
  140. flag.BoolVar(&testUseReset, "tr", false, "Use Reset")
  141. flag.IntVar(&testNumRepeatString, "trs", 8, "Create string variables by repeating a string N times")
  142. flag.IntVar(&testMaxInitLen, "tx", 0, "Max Init Len")
  143. flag.BoolVar(&testUseMust, "tm", true, "Use Must(En|De)code")
  144. flag.BoolVar(&testCheckCircRef, "tl", false, "Use Check Circular Ref")
  145. }
  146. func benchInitFlags() {
  147. flag.BoolVar(&benchMapStringKeyOnly, "bs", false, "Bench use maps with string keys only")
  148. flag.BoolVar(&benchInitDebug, "bg", false, "Bench Debug")
  149. flag.IntVar(&benchDepth, "bd", 1, "Bench Depth")
  150. flag.BoolVar(&benchDoInitBench, "bi", false, "Run Bench Init")
  151. flag.BoolVar(&benchVerify, "bv", false, "Verify Decoded Value during Benchmark")
  152. flag.BoolVar(&benchUnscientificRes, "bu", false, "Show Unscientific Results during Benchmark")
  153. }
  154. func testHEDGet(h Handle) *testHED {
  155. for i := range testHEDs {
  156. v := &testHEDs[i]
  157. if v.H == h {
  158. return v
  159. }
  160. }
  161. testHEDs = append(testHEDs, testHED{h, NewEncoder(nil, h), NewDecoder(nil, h)})
  162. return &testHEDs[len(testHEDs)-1]
  163. }
  164. func testReinit() {
  165. testOnce = sync.Once{}
  166. testHEDs = nil
  167. }
  168. func testInitAll() {
  169. // only parse it once.
  170. if !flag.Parsed() {
  171. flag.Parse()
  172. }
  173. for _, f := range testPreInitFns {
  174. f()
  175. }
  176. for _, f := range testPostInitFns {
  177. f()
  178. }
  179. }
  180. func sTestCodecEncode(ts interface{}, bsIn []byte, fn func([]byte) *bytes.Buffer,
  181. h Handle, bh *BasicHandle) (bs []byte, err error) {
  182. // bs = make([]byte, 0, approxSize)
  183. var e *Encoder
  184. var buf *bytes.Buffer
  185. if testUseReset {
  186. e = testHEDGet(h).E
  187. } else {
  188. e = NewEncoder(nil, h)
  189. }
  190. var oldWriteBufferSize int
  191. if testUseIoEncDec >= 0 {
  192. buf = fn(bsIn)
  193. // set the encode options for using a buffer
  194. oldWriteBufferSize = bh.WriterBufferSize
  195. bh.WriterBufferSize = testUseIoEncDec
  196. if testUseIoWrapper {
  197. e.Reset(ioWriterWrapper{buf})
  198. } else {
  199. e.Reset(buf)
  200. }
  201. } else {
  202. bs = bsIn
  203. e.ResetBytes(&bs)
  204. }
  205. if testUseMust {
  206. e.MustEncode(ts)
  207. } else {
  208. err = e.Encode(ts)
  209. }
  210. if testUseIoEncDec >= 0 {
  211. bs = buf.Bytes()
  212. bh.WriterBufferSize = oldWriteBufferSize
  213. }
  214. if !testUseReset {
  215. e.Release()
  216. }
  217. return
  218. }
  219. func sTestCodecDecode(bs []byte, ts interface{}, h Handle, bh *BasicHandle) (err error) {
  220. var d *Decoder
  221. // var buf *bytes.Reader
  222. if testUseReset {
  223. d = testHEDGet(h).D
  224. } else {
  225. d = NewDecoder(nil, h)
  226. }
  227. var oldReadBufferSize int
  228. if testUseIoEncDec >= 0 {
  229. buf := bytes.NewReader(bs)
  230. oldReadBufferSize = bh.ReaderBufferSize
  231. bh.ReaderBufferSize = testUseIoEncDec
  232. if testUseIoWrapper {
  233. d.Reset(ioReaderWrapper{buf})
  234. } else {
  235. d.Reset(buf)
  236. }
  237. } else {
  238. d.ResetBytes(bs)
  239. }
  240. if testUseMust {
  241. d.MustDecode(ts)
  242. } else {
  243. err = d.Decode(ts)
  244. }
  245. if testUseIoEncDec >= 0 {
  246. bh.ReaderBufferSize = oldReadBufferSize
  247. }
  248. if !testUseReset {
  249. d.Release()
  250. }
  251. return
  252. }
  253. // --- functions below are used by both benchmarks and tests
  254. func logT(x interface{}, format string, args ...interface{}) {
  255. if t, ok := x.(*testing.T); ok && t != nil {
  256. t.Logf(format, args...)
  257. } else if b, ok := x.(*testing.B); ok && b != nil {
  258. b.Logf(format, args...)
  259. } else { // if testing.Verbose() { // if testVerbose {
  260. if len(format) == 0 || format[len(format)-1] != '\n' {
  261. format = format + "\n"
  262. }
  263. fmt.Printf(format, args...)
  264. }
  265. }
  266. // --- functions below are used only by benchmarks alone
  267. func fnBenchmarkByteBuf(bsIn []byte) (buf *bytes.Buffer) {
  268. // var buf bytes.Buffer
  269. // buf.Grow(approxSize)
  270. buf = bytes.NewBuffer(bsIn)
  271. buf.Truncate(0)
  272. return
  273. }
  274. // func benchFnCodecEncode(ts interface{}, bsIn []byte, h Handle) (bs []byte, err error) {
  275. // return testCodecEncode(ts, bsIn, fnBenchmarkByteBuf, h)
  276. // }
  277. // func benchFnCodecDecode(bs []byte, ts interface{}, h Handle) (err error) {
  278. // return testCodecDecode(bs, ts, h)
  279. // }