x_bench_gen_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "errors"
  9. "fmt"
  10. "testing"
  11. "github.com/mailru/easyjson"
  12. "github.com/pquerna/ffjson/ffjson"
  13. "github.com/tinylib/msgp/msgp"
  14. )
  15. /*
  16. To update all these, use:
  17. go get -u github.com/tinylib/msgp/msgp github.com/tinylib/msgp \
  18. github.com/pquerna/ffjson/ffjson github.com/pquerna/ffjson \
  19. github.com/mailru/easyjson/...
  20. Known Issues with external libraries:
  21. - msgp io.R/W support doesn't work. It throws error
  22. */
  23. func init() {
  24. testPreInitFns = append(testPreInitFns, benchXGenPreInit)
  25. }
  26. func benchXGenPreInit() {
  27. benchCheckers = append(benchCheckers,
  28. benchChecker{"msgp", fnMsgpEncodeFn, fnMsgpDecodeFn},
  29. benchChecker{"easyjson", fnEasyjsonEncodeFn, fnEasyjsonDecodeFn},
  30. benchChecker{"ffjson", fnFfjsonEncodeFn, fnFfjsonDecodeFn},
  31. )
  32. }
  33. func fnEasyjsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  34. ts2, ok := ts.(easyjson.Marshaler)
  35. if !ok {
  36. return nil, errors.New("easyjson: input is not a easyjson.Marshaler")
  37. }
  38. if testUseIoEncDec >= 0 {
  39. buf := bytes.NewBuffer(bsIn[:0]) // new(bytes.Buffer)
  40. _, err := easyjson.MarshalToWriter(ts2, buf)
  41. return buf.Bytes(), err
  42. }
  43. return easyjson.Marshal(ts2)
  44. // return ts.(json.Marshaler).MarshalJSON()
  45. }
  46. func fnEasyjsonDecodeFn(buf []byte, ts interface{}) error {
  47. ts2, ok := ts.(easyjson.Unmarshaler)
  48. if !ok {
  49. return errors.New("easyjson: input is not a easyjson.Unmarshaler")
  50. }
  51. if testUseIoEncDec >= 0 {
  52. return easyjson.UnmarshalFromReader(bytes.NewReader(buf), ts2)
  53. }
  54. return easyjson.Unmarshal(buf, ts2)
  55. // return ts.(json.Unmarshaler).UnmarshalJSON(buf)
  56. }
  57. func fnFfjsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  58. return ffjson.Marshal(ts)
  59. // return ts.(json.Marshaler).MarshalJSON()
  60. }
  61. func fnFfjsonDecodeFn(buf []byte, ts interface{}) error {
  62. return ffjson.Unmarshal(buf, ts)
  63. // return ts.(json.Unmarshaler).UnmarshalJSON(buf)
  64. }
  65. func fnMsgpEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  66. if _, ok := ts.(msgp.Encodable); !ok {
  67. return nil, fmt.Errorf("msgp: input of type %T is not a msgp.Encodable", ts)
  68. }
  69. if testUseIoEncDec >= 0 {
  70. buf := fnBenchmarkByteBuf(bsIn)
  71. err := ts.(msgp.Encodable).EncodeMsg(msgp.NewWriter(buf))
  72. return buf.Bytes(), err
  73. }
  74. return ts.(msgp.Marshaler).MarshalMsg(bsIn[:0]) // msgp appends to slice.
  75. }
  76. func fnMsgpDecodeFn(buf []byte, ts interface{}) (err error) {
  77. if _, ok := ts.(msgp.Decodable); !ok {
  78. return fmt.Errorf("msgp: input of type %T is not a msgp.Decodable", ts)
  79. }
  80. if testUseIoEncDec >= 0 {
  81. err = ts.(msgp.Decodable).DecodeMsg(msgp.NewReader(bytes.NewReader(buf)))
  82. return
  83. }
  84. _, err = ts.(msgp.Unmarshaler).UnmarshalMsg(buf)
  85. return
  86. }
  87. func Benchmark__Msgp_______Encode(b *testing.B) {
  88. fnBenchmarkEncode(b, "msgp", benchTs, fnMsgpEncodeFn)
  89. }
  90. func Benchmark__Msgp_______Decode(b *testing.B) {
  91. fnBenchmarkDecode(b, "msgp", benchTs, fnMsgpEncodeFn, fnMsgpDecodeFn, fnBenchNewTs)
  92. }
  93. func Benchmark__Easyjson___Encode(b *testing.B) {
  94. fnBenchmarkEncode(b, "easyjson", benchTs, fnEasyjsonEncodeFn)
  95. }
  96. func Benchmark__Easyjson___Decode(b *testing.B) {
  97. fnBenchmarkDecode(b, "easyjson", benchTs, fnEasyjsonEncodeFn, fnEasyjsonDecodeFn, fnBenchNewTs)
  98. }
  99. func Benchmark__Ffjson_____Encode(b *testing.B) {
  100. fnBenchmarkEncode(b, "ffjson", benchTs, fnFfjsonEncodeFn)
  101. }
  102. func Benchmark__Ffjson_____Decode(b *testing.B) {
  103. fnBenchmarkDecode(b, "ffjson", benchTs, fnFfjsonEncodeFn, fnFfjsonDecodeFn, fnBenchNewTs)
  104. }