x_bench_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // +build x
  2. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. package codec
  5. import (
  6. "bytes"
  7. "testing"
  8. gcbor "bitbucket.org/bodhisnarkva/cbor/go" // gcbor "code.google.com/p/cbor/go"
  9. "github.com/Sereal/Sereal/Go/sereal"
  10. xdr "github.com/davecgh/go-xdr/xdr2"
  11. jsoniter "github.com/json-iterator/go"
  12. "go.mongodb.org/mongo-driver/bson" // "github.com/mongodb/mongo-go-driver/bson"
  13. mgobson "gopkg.in/mgo.v2/bson" //"labix.org/v2/mgo/bson"
  14. vmsgpack "gopkg.in/vmihailenco/msgpack.v2" //"github.com/vmihailenco/msgpack"
  15. )
  16. /*
  17. To update all these, use:
  18. go get -u github.com/tinylib/msgp/msgp github.com/tinylib/msgp \
  19. github.com/pquerna/ffjson/ffjson github.com/pquerna/ffjson \
  20. github.com/Sereal/Sereal/Go/sereal \
  21. bitbucket.org/bodhisnarkva/cbor/go \
  22. github.com/davecgh/go-xdr/xdr2 \
  23. gopkg.in/mgo.v2/bson \
  24. gopkg.in/vmihailenco/msgpack.v2 \
  25. github.com/json-iterator/go \
  26. github.com/mailru/easyjson/...
  27. Known Issues with external libraries:
  28. - msgp io.R/W support doesn't work. It throws error
  29. */
  30. func init() {
  31. testPreInitFns = append(testPreInitFns, benchXPreInit)
  32. _ = bson.NewDecoder
  33. }
  34. func benchXPreInit() {
  35. benchCheckers = append(benchCheckers,
  36. benchChecker{"json-iter", fnJsonIterEncodeFn, fnJsonIterDecodeFn},
  37. benchChecker{"v-msgpack", fnVMsgpackEncodeFn, fnVMsgpackDecodeFn},
  38. benchChecker{"bson", fnBsonEncodeFn, fnBsonDecodeFn},
  39. benchChecker{"mgobson", fnMgobsonEncodeFn, fnMgobsonDecodeFn},
  40. // place codecs with issues at the end, so as not to make results too ugly
  41. benchChecker{"gcbor", fnGcborEncodeFn, fnGcborDecodeFn}, // this logs fat ugly message, but we log.SetOutput(ioutil.Discard)
  42. benchChecker{"xdr", fnXdrEncodeFn, fnXdrDecodeFn},
  43. benchChecker{"sereal", fnSerealEncodeFn, fnSerealDecodeFn},
  44. )
  45. }
  46. func fnVMsgpackEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  47. if testUseIoEncDec >= 0 {
  48. buf := bytes.NewBuffer(bsIn[:0]) // new(bytes.Buffer)
  49. err := vmsgpack.NewEncoder(buf).Encode(ts)
  50. return buf.Bytes(), err
  51. }
  52. return vmsgpack.Marshal(ts)
  53. }
  54. func fnVMsgpackDecodeFn(buf []byte, ts interface{}) error {
  55. if testUseIoEncDec >= 0 {
  56. return vmsgpack.NewDecoder(bytes.NewReader(buf)).Decode(ts)
  57. }
  58. return vmsgpack.Unmarshal(buf, ts)
  59. }
  60. func fnBsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  61. return bson.Marshal(ts)
  62. }
  63. func fnBsonDecodeFn(buf []byte, ts interface{}) error {
  64. return bson.Unmarshal(buf, ts)
  65. }
  66. func fnMgobsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  67. return mgobson.Marshal(ts)
  68. }
  69. func fnMgobsonDecodeFn(buf []byte, ts interface{}) error {
  70. return mgobson.Unmarshal(buf, ts)
  71. }
  72. func fnJsonIterEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  73. if testUseIoEncDec >= 0 {
  74. buf := bytes.NewBuffer(bsIn[:0]) // new(bytes.Buffer)
  75. err := jsoniter.NewEncoder(buf).Encode(ts)
  76. return buf.Bytes(), err
  77. }
  78. return jsoniter.Marshal(ts)
  79. }
  80. func fnJsonIterDecodeFn(buf []byte, ts interface{}) error {
  81. if testUseIoEncDec >= 0 {
  82. return jsoniter.NewDecoder(bytes.NewReader(buf)).Decode(ts)
  83. }
  84. return jsoniter.Unmarshal(buf, ts)
  85. }
  86. func fnXdrEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  87. buf := fnBenchmarkByteBuf(bsIn)
  88. i, err := xdr.Marshal(buf, ts)
  89. return buf.Bytes()[:i], err
  90. }
  91. func fnXdrDecodeFn(buf []byte, ts interface{}) error {
  92. _, err := xdr.Unmarshal(bytes.NewReader(buf), ts)
  93. return err
  94. }
  95. func fnSerealEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  96. return sereal.Marshal(ts)
  97. }
  98. func fnSerealDecodeFn(buf []byte, ts interface{}) error {
  99. return sereal.Unmarshal(buf, ts)
  100. }
  101. func fnGcborEncodeFn(ts interface{}, bsIn []byte) (bs []byte, err error) {
  102. buf := fnBenchmarkByteBuf(bsIn)
  103. err = gcbor.NewEncoder(buf).Encode(ts)
  104. return buf.Bytes(), err
  105. }
  106. func fnGcborDecodeFn(buf []byte, ts interface{}) error {
  107. return gcbor.NewDecoder(bytes.NewReader(buf)).Decode(ts)
  108. }
  109. func Benchmark__JsonIter___Encode(b *testing.B) {
  110. fnBenchmarkEncode(b, "jsoniter", benchTs, fnJsonIterEncodeFn)
  111. }
  112. func Benchmark__JsonIter___Decode(b *testing.B) {
  113. fnBenchmarkDecode(b, "jsoniter", benchTs, fnJsonIterEncodeFn, fnJsonIterDecodeFn, fnBenchNewTs)
  114. }
  115. // Place codecs with issues at the bottom, so as not to make results look too ugly.
  116. func Benchmark__Mgobson____Encode(b *testing.B) {
  117. fnBenchmarkEncode(b, "mgobson", benchTs, fnMgobsonEncodeFn)
  118. }
  119. func Benchmark__Mgobson____Decode(b *testing.B) {
  120. fnBenchmarkDecode(b, "mgobson", benchTs, fnMgobsonEncodeFn, fnMgobsonDecodeFn, fnBenchNewTs)
  121. }
  122. func Benchmark__Bson_______Encode(b *testing.B) {
  123. fnBenchmarkEncode(b, "bson", benchTs, fnBsonEncodeFn)
  124. }
  125. func Benchmark__Bson_______Decode(b *testing.B) {
  126. fnBenchmarkDecode(b, "bson", benchTs, fnBsonEncodeFn, fnBsonDecodeFn, fnBenchNewTs)
  127. }
  128. func Benchmark__VMsgpack___Encode(b *testing.B) {
  129. fnBenchmarkEncode(b, "v-msgpack", benchTs, fnVMsgpackEncodeFn)
  130. }
  131. func Benchmark__VMsgpack___Decode(b *testing.B) {
  132. fnBenchmarkDecode(b, "v-msgpack", benchTs, fnVMsgpackEncodeFn, fnVMsgpackDecodeFn, fnBenchNewTs)
  133. }
  134. func Benchmark__Gcbor______Encode(b *testing.B) {
  135. fnBenchmarkEncode(b, "gcbor", benchTs, fnGcborEncodeFn)
  136. }
  137. func Benchmark__Gcbor______Decode(b *testing.B) {
  138. fnBenchmarkDecode(b, "gcbor", benchTs, fnGcborEncodeFn, fnGcborDecodeFn, fnBenchNewTs)
  139. }
  140. func Benchmark__Xdr________Encode(b *testing.B) {
  141. fnBenchmarkEncode(b, "xdr", benchTs, fnXdrEncodeFn)
  142. }
  143. func Benchmark__Xdr________Decode(b *testing.B) {
  144. fnBenchmarkDecode(b, "xdr", benchTs, fnXdrEncodeFn, fnXdrDecodeFn, fnBenchNewTs)
  145. }
  146. func Benchmark__Sereal_____Encode(b *testing.B) {
  147. fnBenchmarkEncode(b, "sereal", benchTs, fnSerealEncodeFn)
  148. }
  149. func Benchmark__Sereal_____Decode(b *testing.B) {
  150. fnBenchmarkDecode(b, "sereal", benchTs, fnSerealEncodeFn, fnSerealDecodeFn, fnBenchNewTs)
  151. }