z_all_bench_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // +build alltests codecgen
  4. // +build go1.7
  5. package codec
  6. // see notes in z_all_test.go
  7. import (
  8. "strconv"
  9. "sync"
  10. "testing"
  11. "time"
  12. )
  13. import . "github.com/ugorji/go/codec"
  14. var benchmarkGroupOnce sync.Once
  15. var benchmarkGroupSave struct {
  16. testUseIoEncDec int
  17. testUseReset bool
  18. testInternStr bool
  19. benchDepth int
  20. benchMapStringKeyOnly bool
  21. benchInitDebug bool
  22. benchVerify bool
  23. benchDoInitBench bool
  24. benchUnscientificRes bool
  25. }
  26. func benchmarkGroupInitAll() {
  27. testInitAll() // calls flag.Parse
  28. benchmarkGroupSave.testUseIoEncDec = testUseIoEncDec
  29. benchmarkGroupSave.testUseReset = testUseReset
  30. benchmarkGroupSave.testInternStr = testInternStr
  31. benchmarkGroupSave.benchDepth = benchDepth
  32. benchmarkGroupSave.benchMapStringKeyOnly = benchMapStringKeyOnly
  33. benchmarkGroupSave.benchInitDebug = benchInitDebug
  34. benchmarkGroupSave.benchVerify = benchVerify
  35. benchmarkGroupSave.benchDoInitBench = benchDoInitBench
  36. benchmarkGroupSave.benchUnscientificRes = benchUnscientificRes
  37. }
  38. func benchmarkGroupReset() {
  39. testUseIoEncDec = benchmarkGroupSave.testUseIoEncDec
  40. testUseReset = benchmarkGroupSave.testUseReset
  41. testInternStr = benchmarkGroupSave.testInternStr
  42. benchDepth = benchmarkGroupSave.benchDepth
  43. benchMapStringKeyOnly = benchmarkGroupSave.benchMapStringKeyOnly
  44. benchInitDebug = benchmarkGroupSave.benchInitDebug
  45. benchVerify = benchmarkGroupSave.benchVerify
  46. benchDoInitBench = benchmarkGroupSave.benchDoInitBench
  47. benchUnscientificRes = benchmarkGroupSave.benchUnscientificRes
  48. }
  49. func benchmarkDivider() {
  50. // logT(nil, "-------------------------------\n")
  51. println()
  52. }
  53. func benchmarkOneFn(fns []func(*testing.B)) func(*testing.B) {
  54. switch len(fns) {
  55. case 0:
  56. return nil
  57. case 1:
  58. return fns[0]
  59. default:
  60. return func(t *testing.B) {
  61. for _, f := range fns {
  62. f(t)
  63. }
  64. }
  65. }
  66. }
  67. func benchmarkSuiteNoop(b *testing.B) {
  68. testOnce.Do(testInitAll)
  69. b.ResetTimer()
  70. for i := 0; i < b.N; i++ {
  71. time.Sleep(10 * time.Millisecond)
  72. }
  73. }
  74. func benchmarkSuite(t *testing.B, fns ...func(t *testing.B)) {
  75. benchmarkGroupOnce.Do(benchmarkGroupInitAll)
  76. f := benchmarkOneFn(fns)
  77. // find . -name "*_test.go" | xargs grep -e 'flag.' | cut -d '&' -f 2 | cut -d ',' -f 1 | grep -e '^bench'
  78. testReinit() // so flag.Parse() is called first, and never called again
  79. benchReinit()
  80. testDecodeOptions = DecodeOptions{}
  81. testEncodeOptions = EncodeOptions{}
  82. benchmarkGroupReset()
  83. benchVerify = true
  84. benchDoInitBench = true
  85. benchUnscientificRes = true
  86. testReinit()
  87. benchReinit()
  88. t.Run("init-metrics....", func(t *testing.B) { t.Run("Benchmark__Noop.............", benchmarkSuiteNoop) })
  89. benchmarkGroupReset()
  90. benchVerify = false
  91. benchDoInitBench = false
  92. benchUnscientificRes = false
  93. testReinit()
  94. benchReinit()
  95. t.Run("options-false...", f)
  96. benchmarkGroupReset()
  97. testUseIoEncDec = 1024
  98. testReinit()
  99. benchReinit()
  100. t.Run("use-bufio-!bytes", f)
  101. benchmarkGroupReset()
  102. testUseReset = true
  103. testReinit()
  104. benchReinit()
  105. t.Run("reset-enc-dec...", f)
  106. benchmarkGroupReset()
  107. // intern string only applies to binc: don't do a full run of it
  108. // testInternStr = true
  109. // testReinit()
  110. // benchReinit()
  111. // t.Run("intern-strings", f)
  112. // testInternStr = false
  113. // benchVerify is kinda lame - serves no real purpose.
  114. // benchVerify = true
  115. // testReinit()
  116. // benchReinit()
  117. // t.Run("verify-on-decode", f)
  118. // benchVerify = false
  119. }
  120. func benchmarkVeryQuickSuite(t *testing.B, name string, fns ...func(t *testing.B)) {
  121. benchmarkDivider()
  122. benchmarkGroupOnce.Do(benchmarkGroupInitAll)
  123. benchmarkGroupReset()
  124. // bd=1 2 | ti=-1, 1024 |
  125. testUseIoEncDec = -1
  126. // benchDepth = depth
  127. testReinit()
  128. benchReinit()
  129. t.Run(name+"-bd"+strconv.Itoa(benchDepth)+"........", benchmarkOneFn(fns))
  130. benchmarkGroupReset()
  131. }
  132. func benchmarkQuickSuite(t *testing.B, name string, fns ...func(t *testing.B)) {
  133. benchmarkVeryQuickSuite(t, name, fns...)
  134. // encoded size of TestStruc is between 20K and 30K for bd=1 // consider buffer=1024 * 16 * benchDepth
  135. testUseIoEncDec = 1024 // (value of defEncByteBufSize): use smaller buffer, and more flushes - it's ok.
  136. // benchDepth = depth
  137. testReinit()
  138. benchReinit()
  139. t.Run(name+"-bd"+strconv.Itoa(benchDepth)+"-buf"+strconv.Itoa(testUseIoEncDec), benchmarkOneFn(fns))
  140. testUseIoEncDec = 0
  141. // benchDepth = depth
  142. testReinit()
  143. benchReinit()
  144. t.Run(name+"-bd"+strconv.Itoa(benchDepth)+"-io.....", benchmarkOneFn(fns))
  145. benchmarkGroupReset()
  146. }
  147. /*
  148. z='bench_test.go'
  149. find . -name "$z" | xargs grep -e '^func Benchmark.*Encode' | \
  150. cut -d '(' -f 1 | cut -d ' ' -f 2 | \
  151. while read f; do echo "t.Run(\"$f\", $f)"; done &&
  152. echo &&
  153. find . -name "$z" | xargs grep -e '^func Benchmark.*Decode' | \
  154. cut -d '(' -f 1 | cut -d ' ' -f 2 | \
  155. while read f; do echo "t.Run(\"$f\", $f)"; done
  156. */
  157. func benchmarkCodecGroup(t *testing.B) {
  158. benchmarkDivider()
  159. t.Run("Benchmark__Msgpack____Encode", Benchmark__Msgpack____Encode)
  160. t.Run("Benchmark__Binc_______Encode", Benchmark__Binc_______Encode)
  161. t.Run("Benchmark__Simple_____Encode", Benchmark__Simple_____Encode)
  162. t.Run("Benchmark__Cbor_______Encode", Benchmark__Cbor_______Encode)
  163. t.Run("Benchmark__Json_______Encode", Benchmark__Json_______Encode)
  164. t.Run("Benchmark__Std_Json___Encode", Benchmark__Std_Json___Encode)
  165. t.Run("Benchmark__Gob________Encode", Benchmark__Gob________Encode)
  166. // t.Run("Benchmark__Std_Xml____Encode", Benchmark__Std_Xml____Encode)
  167. benchmarkDivider()
  168. t.Run("Benchmark__Msgpack____Decode", Benchmark__Msgpack____Decode)
  169. t.Run("Benchmark__Binc_______Decode", Benchmark__Binc_______Decode)
  170. t.Run("Benchmark__Simple_____Decode", Benchmark__Simple_____Decode)
  171. t.Run("Benchmark__Cbor_______Decode", Benchmark__Cbor_______Decode)
  172. t.Run("Benchmark__Json_______Decode", Benchmark__Json_______Decode)
  173. t.Run("Benchmark__Std_Json___Decode", Benchmark__Std_Json___Decode)
  174. t.Run("Benchmark__Gob________Decode", Benchmark__Gob________Decode)
  175. // t.Run("Benchmark__Std_Xml____Decode", Benchmark__Std_Xml____Decode)
  176. }
  177. func BenchmarkCodecSuite(t *testing.B) { benchmarkSuite(t, benchmarkCodecGroup) }
  178. func benchmarkJsonEncodeGroup(t *testing.B) {
  179. t.Run("Benchmark__Json_______Encode", Benchmark__Json_______Encode)
  180. }
  181. func benchmarkJsonDecodeGroup(t *testing.B) {
  182. t.Run("Benchmark__Json_______Decode", Benchmark__Json_______Decode)
  183. }
  184. func benchmarkCborEncodeGroup(t *testing.B) {
  185. t.Run("Benchmark__Cbor_______Encode", Benchmark__Cbor_______Encode)
  186. }
  187. func benchmarkCborDecodeGroup(t *testing.B) {
  188. t.Run("Benchmark__Cbor_______Decode", Benchmark__Cbor_______Decode)
  189. }
  190. func BenchmarkCodecQuickSuite(t *testing.B) {
  191. benchmarkQuickSuite(t, "cbor", benchmarkCborEncodeGroup)
  192. benchmarkQuickSuite(t, "cbor", benchmarkCborDecodeGroup)
  193. benchmarkQuickSuite(t, "json", benchmarkJsonEncodeGroup)
  194. benchmarkQuickSuite(t, "json", benchmarkJsonDecodeGroup)
  195. // depths := [...]int{1, 4}
  196. // for _, d := range depths {
  197. // benchmarkQuickSuite(t, d, benchmarkJsonEncodeGroup)
  198. // benchmarkQuickSuite(t, d, benchmarkJsonDecodeGroup)
  199. // }
  200. // benchmarkQuickSuite(t, 1, benchmarkJsonEncodeGroup)
  201. // benchmarkQuickSuite(t, 4, benchmarkJsonEncodeGroup)
  202. // benchmarkQuickSuite(t, 1, benchmarkJsonDecodeGroup)
  203. // benchmarkQuickSuite(t, 4, benchmarkJsonDecodeGroup)
  204. // benchmarkQuickSuite(t, 1, benchmarkJsonEncodeGroup, benchmarkJsonDecodeGroup)
  205. // benchmarkQuickSuite(t, 4, benchmarkJsonEncodeGroup, benchmarkJsonDecodeGroup)
  206. // benchmarkQuickSuite(t, benchmarkJsonEncodeGroup)
  207. // benchmarkQuickSuite(t, benchmarkJsonDecodeGroup)
  208. }