x_bench_test.go 5.8 KB

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