z_all_bench_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // +build alltests
  2. // +build go1.7
  3. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  4. // Use of this source code is governed by a MIT license found in the LICENSE file.
  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. testDepth int
  19. testMapStringKeyOnly bool
  20. }
  21. func benchmarkGroupInitAll() {
  22. testInitAll() // calls flag.Parse
  23. benchmarkGroupSave.testUseIoEncDec = testUseIoEncDec
  24. benchmarkGroupSave.testUseReset = testUseReset
  25. benchmarkGroupSave.testDepth = testDepth
  26. benchmarkGroupSave.testMapStringKeyOnly = testMapStringKeyOnly
  27. }
  28. func benchmarkGroupReset() {
  29. testUseIoEncDec = benchmarkGroupSave.testUseIoEncDec
  30. testUseReset = benchmarkGroupSave.testUseReset
  31. testDepth = benchmarkGroupSave.testDepth
  32. testMapStringKeyOnly = benchmarkGroupSave.testMapStringKeyOnly
  33. testDecodeOptions.ZeroCopy = true
  34. }
  35. func benchmarkOneFn(fns []func(*testing.B)) func(*testing.B) {
  36. switch len(fns) {
  37. case 0:
  38. return nil
  39. case 1:
  40. return fns[0]
  41. default:
  42. return func(t *testing.B) {
  43. for _, f := range fns {
  44. f(t)
  45. }
  46. }
  47. }
  48. }
  49. func benchmarkSuiteNoop(b *testing.B) {
  50. testOnce.Do(testInitAll)
  51. b.ResetTimer()
  52. for i := 0; i < b.N; i++ {
  53. time.Sleep(10 * time.Millisecond)
  54. }
  55. }
  56. func benchmarkSuite(t *testing.B, fns ...func(t *testing.B)) {
  57. benchmarkGroupOnce.Do(benchmarkGroupInitAll)
  58. f := benchmarkOneFn(fns)
  59. // find . -name "*_test.go" | xargs grep -e 'flag.' | cut -d '&' -f 2 | cut -d ',' -f 1 | grep -e '^bench'
  60. testReinit() // so flag.Parse() is called first, and never called again
  61. benchReinit()
  62. testDecodeOptions = DecodeOptions{}
  63. testEncodeOptions = EncodeOptions{}
  64. benchmarkGroupReset()
  65. testReinit()
  66. benchReinit()
  67. t.Run("options-false...", f)
  68. benchmarkGroupReset()
  69. testUseIoEncDec = 1024
  70. testReinit()
  71. benchReinit()
  72. t.Run("use-bufio-!bytes", f)
  73. benchmarkGroupReset()
  74. testUseReset = true
  75. testReinit()
  76. benchReinit()
  77. t.Run("reset-enc-dec...", f)
  78. benchmarkGroupReset()
  79. }
  80. func benchmarkVeryQuickSuite(t *testing.B, name string, fns ...func(t *testing.B)) {
  81. benchmarkDivider()
  82. benchmarkGroupOnce.Do(benchmarkGroupInitAll)
  83. benchmarkGroupReset()
  84. // bd=1 2 | ti=-1, 1024 |
  85. testUseIoEncDec = -1
  86. // testDepth = depth
  87. testReinit()
  88. benchReinit()
  89. t.Run(name+"-bd"+strconv.Itoa(testDepth)+"........", benchmarkOneFn(fns))
  90. benchmarkGroupReset()
  91. }
  92. func benchmarkQuickSuite(t *testing.B, name string, fns ...func(t *testing.B)) {
  93. benchmarkVeryQuickSuite(t, name, fns...)
  94. // encoded size of TestStruc is between 20K and 30K for bd=1 // consider buffer=1024 * 16 * testDepth
  95. testUseIoEncDec = 1024 // (value of defEncByteBufSize): use smaller buffer, and more flushes - it's ok.
  96. // testDepth = depth
  97. testReinit()
  98. benchReinit()
  99. t.Run(name+"-bd"+strconv.Itoa(testDepth)+"-buf"+strconv.Itoa(testUseIoEncDec), benchmarkOneFn(fns))
  100. testUseIoEncDec = 0
  101. // testDepth = depth
  102. testReinit()
  103. benchReinit()
  104. t.Run(name+"-bd"+strconv.Itoa(testDepth)+"-io.....", benchmarkOneFn(fns))
  105. benchmarkGroupReset()
  106. }
  107. /*
  108. z='bench_test.go'
  109. find . -name "$z" | xargs grep -e '^func Benchmark.*Encode' | \
  110. cut -d '(' -f 1 | cut -d ' ' -f 2 | \
  111. while read f; do echo "t.Run(\"$f\", $f)"; done &&
  112. echo &&
  113. find . -name "$z" | xargs grep -e '^func Benchmark.*Decode' | \
  114. cut -d '(' -f 1 | cut -d ' ' -f 2 | \
  115. while read f; do echo "t.Run(\"$f\", $f)"; done
  116. */
  117. func benchmarkCodecGroup(t *testing.B) {
  118. benchmarkDivider()
  119. t.Run("Benchmark__Msgpack____Encode", Benchmark__Msgpack____Encode)
  120. t.Run("Benchmark__Binc_______Encode", Benchmark__Binc_______Encode)
  121. t.Run("Benchmark__Simple_____Encode", Benchmark__Simple_____Encode)
  122. t.Run("Benchmark__Cbor_______Encode", Benchmark__Cbor_______Encode)
  123. t.Run("Benchmark__Json_______Encode", Benchmark__Json_______Encode)
  124. benchmarkDivider()
  125. t.Run("Benchmark__Msgpack____Decode", Benchmark__Msgpack____Decode)
  126. t.Run("Benchmark__Binc_______Decode", Benchmark__Binc_______Decode)
  127. t.Run("Benchmark__Simple_____Decode", Benchmark__Simple_____Decode)
  128. t.Run("Benchmark__Cbor_______Decode", Benchmark__Cbor_______Decode)
  129. t.Run("Benchmark__Json_______Decode", Benchmark__Json_______Decode)
  130. }
  131. func BenchmarkCodecSuite(t *testing.B) { benchmarkSuite(t, benchmarkCodecGroup) }
  132. func benchmarkJsonEncodeGroup(t *testing.B) {
  133. t.Run("Benchmark__Json_______Encode", Benchmark__Json_______Encode)
  134. }
  135. func benchmarkJsonDecodeGroup(t *testing.B) {
  136. t.Run("Benchmark__Json_______Decode", Benchmark__Json_______Decode)
  137. }
  138. func benchmarkCborEncodeGroup(t *testing.B) {
  139. t.Run("Benchmark__Cbor_______Encode", Benchmark__Cbor_______Encode)
  140. }
  141. func benchmarkCborDecodeGroup(t *testing.B) {
  142. t.Run("Benchmark__Cbor_______Decode", Benchmark__Cbor_______Decode)
  143. }
  144. func BenchmarkCodecQuickSuite(t *testing.B) {
  145. benchmarkQuickSuite(t, "cbor", benchmarkCborEncodeGroup)
  146. benchmarkQuickSuite(t, "cbor", benchmarkCborDecodeGroup)
  147. benchmarkQuickSuite(t, "json", benchmarkJsonEncodeGroup)
  148. benchmarkQuickSuite(t, "json", benchmarkJsonDecodeGroup)
  149. // depths := [...]int{1, 4}
  150. // for _, d := range depths {
  151. // benchmarkQuickSuite(t, d, benchmarkJsonEncodeGroup)
  152. // benchmarkQuickSuite(t, d, benchmarkJsonDecodeGroup)
  153. // }
  154. // benchmarkQuickSuite(t, 1, benchmarkJsonEncodeGroup)
  155. // benchmarkQuickSuite(t, 4, benchmarkJsonEncodeGroup)
  156. // benchmarkQuickSuite(t, 1, benchmarkJsonDecodeGroup)
  157. // benchmarkQuickSuite(t, 4, benchmarkJsonDecodeGroup)
  158. // benchmarkQuickSuite(t, 1, benchmarkJsonEncodeGroup, benchmarkJsonDecodeGroup)
  159. // benchmarkQuickSuite(t, 4, benchmarkJsonEncodeGroup, benchmarkJsonDecodeGroup)
  160. // benchmarkQuickSuite(t, benchmarkJsonEncodeGroup)
  161. // benchmarkQuickSuite(t, benchmarkJsonDecodeGroup)
  162. }