x_bench_gen_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // +build x
  2. // +build generated
  3. // Copyright (c) 2012-2015 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. if _, ok := ts.(easyjson.Marshaler); !ok {
  35. return nil, errors.New("easyjson: input is not a easyjson.Marshaler")
  36. }
  37. if testUseIoEncDec {
  38. buf := new(bytes.Buffer)
  39. _, err := easyjson.MarshalToWriter(ts.(easyjson.Marshaler), buf)
  40. return buf.Bytes(), err
  41. }
  42. return easyjson.Marshal(ts.(easyjson.Marshaler))
  43. // return ts.(json.Marshaler).MarshalJSON()
  44. }
  45. func fnEasyjsonDecodeFn(buf []byte, ts interface{}) error {
  46. if _, ok := ts.(easyjson.Unmarshaler); !ok {
  47. return errors.New("easyjson: input is not a easyjson.Unmarshaler")
  48. }
  49. if testUseIoEncDec {
  50. return easyjson.UnmarshalFromReader(bytes.NewReader(buf), ts.(easyjson.Unmarshaler))
  51. }
  52. return easyjson.Unmarshal(buf, ts.(easyjson.Unmarshaler))
  53. // return ts.(json.Unmarshaler).UnmarshalJSON(buf)
  54. }
  55. func fnFfjsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  56. return ffjson.Marshal(ts)
  57. // return ts.(json.Marshaler).MarshalJSON()
  58. }
  59. func fnFfjsonDecodeFn(buf []byte, ts interface{}) error {
  60. return ffjson.Unmarshal(buf, ts)
  61. // return ts.(json.Unmarshaler).UnmarshalJSON(buf)
  62. }
  63. func fnMsgpEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  64. if _, ok := ts.(msgp.Encodable); !ok {
  65. return nil, fmt.Errorf("msgp: input of type %T is not a msgp.Encodable", ts)
  66. }
  67. if testUseIoEncDec {
  68. buf := fnBenchmarkByteBuf(bsIn)
  69. err := ts.(msgp.Encodable).EncodeMsg(msgp.NewWriter(buf))
  70. return buf.Bytes(), err
  71. }
  72. return ts.(msgp.Marshaler).MarshalMsg(bsIn[:0]) // msgp appends to slice.
  73. }
  74. func fnMsgpDecodeFn(buf []byte, ts interface{}) (err error) {
  75. if _, ok := ts.(msgp.Decodable); !ok {
  76. return fmt.Errorf("msgp: input of type %T is not a msgp.Decodable", ts)
  77. }
  78. if testUseIoEncDec {
  79. err = ts.(msgp.Decodable).DecodeMsg(msgp.NewReader(bytes.NewReader(buf)))
  80. return
  81. }
  82. _, err = ts.(msgp.Unmarshaler).UnmarshalMsg(buf)
  83. return
  84. }
  85. func Benchmark__Msgp_______Encode(b *testing.B) {
  86. fnBenchmarkEncode(b, "msgp", benchTs, fnMsgpEncodeFn)
  87. }
  88. func Benchmark__Msgp_______Decode(b *testing.B) {
  89. fnBenchmarkDecode(b, "msgp", benchTs, fnMsgpEncodeFn, fnMsgpDecodeFn, fnBenchNewTs)
  90. }
  91. func Benchmark__Easyjson___Encode(b *testing.B) {
  92. fnBenchmarkEncode(b, "easyjson", benchTs, fnEasyjsonEncodeFn)
  93. }
  94. func Benchmark__Easyjson___Decode(b *testing.B) {
  95. fnBenchmarkDecode(b, "easyjson", benchTs, fnEasyjsonEncodeFn, fnEasyjsonDecodeFn, fnBenchNewTs)
  96. }
  97. func Benchmark__Ffjson_____Encode(b *testing.B) {
  98. fnBenchmarkEncode(b, "ffjson", benchTs, fnFfjsonEncodeFn)
  99. }
  100. func Benchmark__Ffjson_____Decode(b *testing.B) {
  101. fnBenchmarkDecode(b, "ffjson", benchTs, fnFfjsonEncodeFn, fnFfjsonDecodeFn, fnBenchNewTs)
  102. }