stdlib_bench_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // +build !generated
  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. "encoding/gob"
  8. "encoding/json"
  9. "encoding/xml"
  10. "testing"
  11. )
  12. func init() {
  13. testPreInitFns = append(testPreInitFns, stdlibBenchPreInit)
  14. // testPostInitFns = append(testPostInitFns, codecbenchPostInit)
  15. }
  16. func stdlibBenchPreInit() {
  17. benchCheckers = append(benchCheckers,
  18. benchChecker{"std-json", fnStdJsonEncodeFn, fnStdJsonDecodeFn},
  19. benchChecker{"gob", fnGobEncodeFn, fnGobDecodeFn},
  20. benchChecker{"std-xml", fnStdXmlEncodeFn, fnStdXmlDecodeFn},
  21. )
  22. }
  23. // ------------ tests below
  24. func fnGobEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  25. buf := fnBenchmarkByteBuf(bsIn)
  26. err := gob.NewEncoder(buf).Encode(ts)
  27. return buf.Bytes(), err
  28. }
  29. func fnGobDecodeFn(buf []byte, ts interface{}) error {
  30. return gob.NewDecoder(bytes.NewReader(buf)).Decode(ts)
  31. }
  32. func fnStdXmlEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  33. buf := fnBenchmarkByteBuf(bsIn)
  34. err := xml.NewEncoder(buf).Encode(ts)
  35. return buf.Bytes(), err
  36. }
  37. func fnStdXmlDecodeFn(buf []byte, ts interface{}) error {
  38. return xml.NewDecoder(bytes.NewReader(buf)).Decode(ts)
  39. }
  40. func fnStdJsonEncodeFn(ts interface{}, bsIn []byte) ([]byte, error) {
  41. if testUseIoEncDec >= 0 {
  42. buf := fnBenchmarkByteBuf(bsIn)
  43. err := json.NewEncoder(buf).Encode(ts)
  44. return buf.Bytes(), err
  45. }
  46. return json.Marshal(ts)
  47. }
  48. func fnStdJsonDecodeFn(buf []byte, ts interface{}) error {
  49. if testUseIoEncDec >= 0 {
  50. return json.NewDecoder(bytes.NewReader(buf)).Decode(ts)
  51. }
  52. return json.Unmarshal(buf, ts)
  53. }
  54. // ----------- ENCODE ------------------
  55. func Benchmark__Std_Json___Encode(b *testing.B) {
  56. fnBenchmarkEncode(b, "std-json", benchTs, fnStdJsonEncodeFn)
  57. }
  58. func Benchmark__Gob________Encode(b *testing.B) {
  59. fnBenchmarkEncode(b, "gob", benchTs, fnGobEncodeFn)
  60. }
  61. func Benchmark__Std_Xml____Encode(b *testing.B) {
  62. fnBenchmarkEncode(b, "std-xml", benchTs, fnStdXmlEncodeFn)
  63. }
  64. // ----------- DECODE ------------------
  65. func Benchmark__Std_Json___Decode(b *testing.B) {
  66. fnBenchmarkDecode(b, "std-json", benchTs, fnStdJsonEncodeFn, fnStdJsonDecodeFn, fnBenchNewTs)
  67. }
  68. func Benchmark__Gob________Decode(b *testing.B) {
  69. fnBenchmarkDecode(b, "gob", benchTs, fnGobEncodeFn, fnGobDecodeFn, fnBenchNewTs)
  70. }
  71. func Benchmark__Std_Xml____Decode(b *testing.B) {
  72. fnBenchmarkDecode(b, "std-xml", benchTs, fnStdXmlEncodeFn, fnStdXmlDecodeFn, fnBenchNewTs)
  73. }