shared_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright (c) 2012-2015 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. "io"
  43. "sync"
  44. )
  45. // DO NOT REMOVE - replacement line for go-codec-bench import declaration tag //
  46. type testHED struct {
  47. H Handle
  48. E *Encoder
  49. D *Decoder
  50. }
  51. type ioReaderWrapper struct {
  52. r io.Reader
  53. }
  54. func (x ioReaderWrapper) Read(p []byte) (n int, err error) {
  55. return x.r.Read(p)
  56. }
  57. type ioWriterWrapper struct {
  58. w io.Writer
  59. }
  60. func (x ioWriterWrapper) Write(p []byte) (n int, err error) {
  61. return x.w.Write(p)
  62. }
  63. var (
  64. // testNoopH = NoopHandle(8)
  65. testMsgpackH = &MsgpackHandle{}
  66. testBincH = &BincHandle{}
  67. testSimpleH = &SimpleHandle{}
  68. testCborH = &CborHandle{}
  69. testJsonH = &JsonHandle{}
  70. testHandles []Handle
  71. testPreInitFns []func()
  72. testPostInitFns []func()
  73. testOnce sync.Once
  74. testHEDs []testHED
  75. )
  76. // flag variables used by tests (and bench)
  77. var (
  78. testDepth int
  79. testVerbose bool
  80. testInitDebug bool
  81. testStructToArray bool
  82. testCanonical bool
  83. testUseReset bool
  84. testSkipIntf bool
  85. testInternStr bool
  86. testUseMust bool
  87. testCheckCircRef bool
  88. testUseIoEncDec int
  89. testUseIoWrapper bool
  90. testMaxInitLen int
  91. testNumRepeatString int
  92. )
  93. // variables that are not flags, but which can configure the handles
  94. var (
  95. testEncodeOptions EncodeOptions
  96. testDecodeOptions DecodeOptions
  97. )
  98. // flag variables used by bench
  99. var (
  100. benchDoInitBench bool
  101. benchVerify bool
  102. benchUnscientificRes bool = false
  103. benchMapStringKeyOnly bool
  104. //depth of 0 maps to ~400bytes json-encoded string, 1 maps to ~1400 bytes, etc
  105. //For depth>1, we likely trigger stack growth for encoders, making benchmarking unreliable.
  106. benchDepth int
  107. benchInitDebug bool
  108. )
  109. func init() {
  110. testHEDs = make([]testHED, 0, 32)
  111. testHandles = append(testHandles,
  112. // testNoopH,
  113. testMsgpackH, testBincH, testSimpleH,
  114. testCborH, testJsonH)
  115. testInitFlags()
  116. benchInitFlags()
  117. }
  118. func testInitFlags() {
  119. // delete(testDecOpts.ExtFuncs, timeTyp)
  120. flag.IntVar(&testDepth, "tsd", 0, "Test Struc Depth")
  121. flag.BoolVar(&testVerbose, "tv", false, "Test Verbose")
  122. flag.BoolVar(&testInitDebug, "tg", false, "Test Init Debug")
  123. flag.IntVar(&testUseIoEncDec, "ti", -1, "Use IO Reader/Writer for Marshal/Unmarshal ie >= 0")
  124. flag.BoolVar(&testUseIoWrapper, "tiw", false, "Wrap the IO Reader/Writer with a base pass-through reader/writer")
  125. flag.BoolVar(&testStructToArray, "ts", false, "Set StructToArray option")
  126. flag.BoolVar(&testCanonical, "tc", false, "Set Canonical option")
  127. flag.BoolVar(&testInternStr, "te", false, "Set InternStr option")
  128. flag.BoolVar(&testSkipIntf, "tf", false, "Skip Interfaces")
  129. flag.BoolVar(&testUseReset, "tr", false, "Use Reset")
  130. flag.IntVar(&testNumRepeatString, "trs", 8, "Create string variables by repeating a string N times")
  131. flag.IntVar(&testMaxInitLen, "tx", 0, "Max Init Len")
  132. flag.BoolVar(&testUseMust, "tm", true, "Use Must(En|De)code")
  133. flag.BoolVar(&testCheckCircRef, "tl", false, "Use Check Circular Ref")
  134. }
  135. func benchInitFlags() {
  136. flag.BoolVar(&benchMapStringKeyOnly, "bs", false, "Bench use maps with string keys only")
  137. flag.BoolVar(&benchInitDebug, "bg", false, "Bench Debug")
  138. flag.IntVar(&benchDepth, "bd", 1, "Bench Depth")
  139. flag.BoolVar(&benchDoInitBench, "bi", false, "Run Bench Init")
  140. flag.BoolVar(&benchVerify, "bv", false, "Verify Decoded Value during Benchmark")
  141. flag.BoolVar(&benchUnscientificRes, "bu", false, "Show Unscientific Results during Benchmark")
  142. }
  143. func testHEDGet(h Handle) *testHED {
  144. for i := range testHEDs {
  145. v := &testHEDs[i]
  146. if v.H == h {
  147. return v
  148. }
  149. }
  150. testHEDs = append(testHEDs, testHED{h, NewEncoder(nil, h), NewDecoder(nil, h)})
  151. return &testHEDs[len(testHEDs)-1]
  152. }
  153. func testReinit() {
  154. testOnce = sync.Once{}
  155. testHEDs = nil
  156. }
  157. func testInitAll() {
  158. // only parse it once.
  159. if !flag.Parsed() {
  160. flag.Parse()
  161. }
  162. for _, f := range testPreInitFns {
  163. f()
  164. }
  165. for _, f := range testPostInitFns {
  166. f()
  167. }
  168. }
  169. func testCodecEncode(ts interface{}, bsIn []byte,
  170. fn func([]byte) *bytes.Buffer, h Handle) (bs []byte, err error) {
  171. // bs = make([]byte, 0, approxSize)
  172. var e *Encoder
  173. var buf *bytes.Buffer
  174. if testUseReset {
  175. e = testHEDGet(h).E
  176. } else {
  177. e = NewEncoder(nil, h)
  178. }
  179. bh := BasicHandleDoNotUse(h)
  180. var oldWriteBufferSize int
  181. if testUseIoEncDec >= 0 {
  182. buf = fn(bsIn)
  183. // set the encode options for using a buffer
  184. oldWriteBufferSize = bh.WriterBufferSize
  185. bh.WriterBufferSize = testUseIoEncDec
  186. if testUseIoWrapper {
  187. e.Reset(ioWriterWrapper{buf})
  188. } else {
  189. e.Reset(buf)
  190. }
  191. } else {
  192. bs = bsIn
  193. e.ResetBytes(&bs)
  194. }
  195. if testUseMust {
  196. e.MustEncode(ts)
  197. } else {
  198. err = e.Encode(ts)
  199. }
  200. if testUseIoEncDec >= 0 {
  201. bs = buf.Bytes()
  202. bh.WriterBufferSize = oldWriteBufferSize
  203. }
  204. return
  205. }
  206. func testCodecDecode(bs []byte, ts interface{}, h Handle) (err error) {
  207. var d *Decoder
  208. // var buf *bytes.Reader
  209. if testUseReset {
  210. d = testHEDGet(h).D
  211. } else {
  212. d = NewDecoder(nil, h)
  213. }
  214. bh := BasicHandleDoNotUse(h)
  215. var oldReadBufferSize int
  216. if testUseIoEncDec >= 0 {
  217. buf := bytes.NewReader(bs)
  218. oldReadBufferSize = bh.ReaderBufferSize
  219. bh.ReaderBufferSize = testUseIoEncDec
  220. if testUseIoWrapper {
  221. d.Reset(ioReaderWrapper{buf})
  222. } else {
  223. d.Reset(buf)
  224. }
  225. } else {
  226. d.ResetBytes(bs)
  227. }
  228. if testUseMust {
  229. d.MustDecode(ts)
  230. } else {
  231. err = d.Decode(ts)
  232. }
  233. if testUseIoEncDec >= 0 {
  234. bh.ReaderBufferSize = oldReadBufferSize
  235. }
  236. return
  237. }
  238. // ----- functions below are used only by benchmarks alone
  239. func fnBenchmarkByteBuf(bsIn []byte) (buf *bytes.Buffer) {
  240. // var buf bytes.Buffer
  241. // buf.Grow(approxSize)
  242. buf = bytes.NewBuffer(bsIn)
  243. buf.Truncate(0)
  244. return
  245. }
  246. func benchFnCodecEncode(ts interface{}, bsIn []byte, h Handle) (bs []byte, err error) {
  247. return testCodecEncode(ts, bsIn, fnBenchmarkByteBuf, h)
  248. }
  249. func benchFnCodecDecode(bs []byte, ts interface{}, h Handle) (err error) {
  250. return testCodecDecode(bs, ts, h)
  251. }