codec_bench_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. import (
  5. "testing"
  6. )
  7. func init() {
  8. testPreInitFns = append(testPreInitFns, codecBenchPreInit)
  9. // testPostInitFns = append(testPostInitFns, codecbenchPostInit)
  10. }
  11. func codecBenchPreInit() {
  12. benchCheckers = append(benchCheckers,
  13. benchChecker{"msgpack", fnMsgpackEncodeFn, fnMsgpackDecodeFn},
  14. benchChecker{"binc", fnBincEncodeFn, fnBincDecodeFn},
  15. benchChecker{"simple", fnSimpleEncodeFn, fnSimpleDecodeFn},
  16. benchChecker{"cbor", fnCborEncodeFn, fnCborDecodeFn},
  17. benchChecker{"json", fnJsonEncodeFn, fnJsonDecodeFn},
  18. )
  19. }
  20. // ------------ tests below
  21. func fnMsgpackEncodeFn(ts interface{}, bsIn []byte) (bs []byte, err error) {
  22. return sTestCodecEncode(ts, bsIn, fnBenchmarkByteBuf, testMsgpackH, &testMsgpackH.BasicHandle)
  23. }
  24. func fnMsgpackDecodeFn(buf []byte, ts interface{}) error {
  25. return sTestCodecDecode(buf, ts, testMsgpackH, &testMsgpackH.BasicHandle)
  26. }
  27. func fnBincEncodeFn(ts interface{}, bsIn []byte) (bs []byte, err error) {
  28. return sTestCodecEncode(ts, bsIn, fnBenchmarkByteBuf, testBincH, &testBincH.BasicHandle)
  29. }
  30. func fnBincDecodeFn(buf []byte, ts interface{}) error {
  31. return sTestCodecDecode(buf, ts, testBincH, &testBincH.BasicHandle)
  32. }
  33. func fnSimpleEncodeFn(ts interface{}, bsIn []byte) (bs []byte, err error) {
  34. return sTestCodecEncode(ts, bsIn, fnBenchmarkByteBuf, testSimpleH, &testSimpleH.BasicHandle)
  35. }
  36. func fnSimpleDecodeFn(buf []byte, ts interface{}) error {
  37. return sTestCodecDecode(buf, ts, testSimpleH, &testSimpleH.BasicHandle)
  38. }
  39. func fnCborEncodeFn(ts interface{}, bsIn []byte) (bs []byte, err error) {
  40. return sTestCodecEncode(ts, bsIn, fnBenchmarkByteBuf, testCborH, &testCborH.BasicHandle)
  41. }
  42. func fnCborDecodeFn(buf []byte, ts interface{}) error {
  43. return sTestCodecDecode(buf, ts, testCborH, &testCborH.BasicHandle)
  44. }
  45. func fnJsonEncodeFn(ts interface{}, bsIn []byte) (bs []byte, err error) {
  46. return sTestCodecEncode(ts, bsIn, fnBenchmarkByteBuf, testJsonH, &testJsonH.BasicHandle)
  47. }
  48. func fnJsonDecodeFn(buf []byte, ts interface{}) error {
  49. return sTestCodecDecode(buf, ts, testJsonH, &testJsonH.BasicHandle)
  50. }
  51. // ----------- ENCODE ------------------
  52. func Benchmark__Msgpack____Encode(b *testing.B) {
  53. fnBenchmarkEncode(b, "msgpack", benchTs, fnMsgpackEncodeFn)
  54. }
  55. func Benchmark__Binc_______Encode(b *testing.B) {
  56. fnBenchmarkEncode(b, "binc", benchTs, fnBincEncodeFn)
  57. }
  58. func Benchmark__Simple_____Encode(b *testing.B) {
  59. fnBenchmarkEncode(b, "simple", benchTs, fnSimpleEncodeFn)
  60. }
  61. func Benchmark__Cbor_______Encode(b *testing.B) {
  62. fnBenchmarkEncode(b, "cbor", benchTs, fnCborEncodeFn)
  63. }
  64. func Benchmark__Json_______Encode(b *testing.B) {
  65. fnBenchmarkEncode(b, "json", benchTs, fnJsonEncodeFn)
  66. }
  67. // ----------- DECODE ------------------
  68. func Benchmark__Msgpack____Decode(b *testing.B) {
  69. fnBenchmarkDecode(b, "msgpack", benchTs, fnMsgpackEncodeFn, fnMsgpackDecodeFn, fnBenchNewTs)
  70. }
  71. func Benchmark__Binc_______Decode(b *testing.B) {
  72. fnBenchmarkDecode(b, "binc", benchTs, fnBincEncodeFn, fnBincDecodeFn, fnBenchNewTs)
  73. }
  74. func Benchmark__Simple_____Decode(b *testing.B) {
  75. fnBenchmarkDecode(b, "simple", benchTs, fnSimpleEncodeFn, fnSimpleDecodeFn, fnBenchNewTs)
  76. }
  77. func Benchmark__Cbor_______Decode(b *testing.B) {
  78. fnBenchmarkDecode(b, "cbor", benchTs, fnCborEncodeFn, fnCborDecodeFn, fnBenchNewTs)
  79. }
  80. func Benchmark__Json_______Decode(b *testing.B) {
  81. fnBenchmarkDecode(b, "json", benchTs, fnJsonEncodeFn, fnJsonDecodeFn, fnBenchNewTs)
  82. }