| 1 | # go-codec-bench |
| 2 | |
| 3 | This is a comparison of different binary and text encodings. |
| 4 | |
| 5 | We compare the codecs provided by github.com/ugorji/go/codec package, |
| 6 | against other libraries: |
| 7 | |
| 8 | [github.com/ugorji/go/codec](http://github.com/ugorji/go) provides: |
| 9 | |
| 10 | - msgpack: [http://github.com/msgpack/msgpack] |
| 11 | - binc: [http://github.com/ugorji/binc] |
| 12 | - cbor: [http://cbor.io] [http://tools.ietf.org/html/rfc7049] |
| 13 | - simple: |
| 14 | - json: [http://json.org] [http://tools.ietf.org/html/rfc7159] |
| 15 | |
| 16 | Other codecs compared include: |
| 17 | |
| 18 | - [github.com/vmihailenco/msgpack/v4](https://pkg.go.dev/github.com/vmihailenco/msgpack/v4) |
| 19 | - [github.com/globalsign/mgo/bson](https://pkg.go.dev/github.com/globalsign/mgo/bson) |
| 20 | - [go.mongodb.org/mongo-driver/bson](https://pkg.go.dev/go.mongodb.org/mongo-driver/bson) |
| 21 | - [github.com/davecgh/go-xdr/xdr2](https://pkg.go.dev/github.com/davecgh/go-xdr/xdr2) |
| 22 | - [github.com/Sereal/Sereal/Go/sereal](https://pkg.go.dev/github.com/Sereal/Sereal/Go/sereal) |
| 23 | - [bitbucket.org/bodhisnarkva/cbor/go](https://pkg.go.dev/bitbucket.org/bodhisnarkva/cbor/go) |
| 24 | - [github.com/tinylib/msgp](https://pkg.go.dev/github.com/tinylib/msgp) |
| 25 | - [github.com/pquerna/ffjson/ffjson](https://pkg.go.dev/github.com/pquerna/ffjson/ffjson) |
| 26 | - [github.com/json-iterator/go](https://pkg.go.dev/github.com/json-iterator/go) |
| 27 | - [github.com/mailru/easyjson](https://pkg.go.dev/github.com/mailru/easyjson) |
| 28 | - [github.com/fxamacker/cbor/v2](https://pkg.go.dev/github.com/fxamacker/cbor/v2) |
| 29 | |
| 30 | # Data |
| 31 | |
| 32 | The data being serialized is a `TestStruc` randomly generated values. |
| 33 | See [values_test.go](values_test.go) for the |
| 34 | definition of the TestStruc. |
| 35 | |
| 36 | # Run Benchmarks |
| 37 | |
| 38 | See [bench.sh](bench.sh) |
| 39 | for how to download the external libraries which we benchmark against, |
| 40 | generate the files for the types when needed, |
| 41 | and run the suite of tests. |
| 42 | |
| 43 | The 3 suite of benchmarks are |
| 44 | |
| 45 | - CodecSuite |
| 46 | - XSuite |
| 47 | - CodecXSuite |
| 48 | |
| 49 | ``` |
| 50 | # Note that `bench.sh` may be in the codec sub-directory, and should be run from there. |
| 51 | |
| 52 | # download the code and all its dependencies |
| 53 | ./bench.sh -d |
| 54 | |
| 55 | # code-generate files needed for benchmarks against ffjson, easyjson, msgp, etc |
| 56 | ./bench.sh -c |
| 57 | |
| 58 | # run the full suite of tests (not including external formats) |
| 59 | ./bench.sh -s |
| 60 | |
| 61 | # Below, see how to just run some specific suite of tests, knowing the right tags and flags ... |
| 62 | # See bench.sh for different iterations |
| 63 | |
| 64 | # Run suite of tests in default mode (selectively using unsafe in specific areas) |
| 65 | go test -tags "alltests x" -bench "CodecXSuite" -benchmem |
| 66 | # Run suite of tests in safe mode (no usage of unsafe) |
| 67 | go test -tags "alltests x codec.safe" -bench "CodecXSuite" -benchmem |
| 68 | # Run suite of tests in codecgen mode, including all tests which are generated (msgp, ffjson, etc) |
| 69 | go test -tags "alltests x generated" -bench "CodecXGenSuite" -benchmem |
| 70 | |
| 71 | ``` |
| 72 | |
| 73 | # Issues |
| 74 | |
| 75 | The following issues are seen currently (11/20/2014): |
| 76 | |
| 77 | - _code.google.com/p/cbor/go_ fails on encoding and decoding the test struct |
| 78 | - _github.com/davecgh/go-xdr/xdr2_ fails on encoding and decoding the test struct |
| 79 | - _github.com/Sereal/Sereal/Go/sereal_ fails on decoding the serialized test struct |
| 80 | |
| 81 | # Representative Benchmark Results |
| 82 | |
| 83 | Please see the [benchmarking blog post for detailed representative results](http://ugorji.net/blog/benchmarking-serialization-in-go). |
| 84 | |
| 85 | A snapshot of some results on my Core-i5 2018 Dell Inspiron laptop is below. |
| 86 | **Note: errors are truncated, and lines re-arranged, for readability**. |
| 87 | |
| 88 | What you should notice: |
| 89 | |
| 90 | - Results get better with codecgen, resulting in about 50% performance improvement. |
| 91 | Users should carefully weigh the performance improvements against the |
| 92 | usability and binary-size increases, as performance is already extremely good |
| 93 | without the codecgen path. |
| 94 | |
| 95 | See [bench.out.txt](bench.out.txt) for representative result from running `bench.sh` as below, as of 2020-11-11. |
| 96 | ```sh |
| 97 | ./bench.sh -z > bench.out.txt |
| 98 | ``` |
| 99 | |
| 100 | *snippet of benchmark output, running without codecgen (2021-02-04)* |
| 101 | *note that the first 5 are from codes (and the following are from other libraries (as named).* |
| 102 | ``` |
| 103 | BenchmarkCodecXSuite/options-false.../ |
| 104 | Benchmark__Msgpack____Encode-8 17764 67504 ns/op 24 B/op 1 allocs/op |
| 105 | Benchmark__Binc_______Encode-8 16442 72011 ns/op 24 B/op 1 allocs/op |
| 106 | Benchmark__Simple_____Encode-8 17060 69965 ns/op 24 B/op 1 allocs/op |
| 107 | Benchmark__Cbor_______Encode-8 16450 70783 ns/op 24 B/op 1 allocs/op |
| 108 | Benchmark__Json_______Encode-8 7303 145232 ns/op 24 B/op 1 allocs/op |
| 109 | Benchmark__Std_Json___Encode-8 5308 229397 ns/op 79216 B/op 556 allocs/op |
| 110 | Benchmark__Gob________Encode-8 7278 165572 ns/op 174169 B/op 683 allocs/op |
| 111 | Benchmark__JsonIter___Encode-8 7950 154060 ns/op 54437 B/op 80 allocs/op |
| 112 | Benchmark__Bson_______Encode-8 3780 305520 ns/op 242263 B/op 1181 allocs/op |
| 113 | Benchmark__Mgobson____Encode-8 3040 397415 ns/op 300345 B/op 1877 allocs/op |
| 114 | Benchmark__VMsgpack___Encode-8 5572 218910 ns/op 164607 B/op 360 allocs/op |
| 115 | Benchmark__Fxcbor_____Encode-8 10000 114321 ns/op 51947 B/op 400 allocs/op |
| 116 | Benchmark__Sereal_____Encode-8 3063 350328 ns/op 277718 B/op 3388 allocs/op |
| 117 | |
| 118 | Benchmark__Msgpack____Decode-8 8372 144322 ns/op 37412 B/op 315 allocs/op |
| 119 | Benchmark__Binc_______Decode-8 8487 141145 ns/op 37413 B/op 315 allocs/op |
| 120 | Benchmark__Simple_____Decode-8 8542 139446 ns/op 37413 B/op 315 allocs/op |
| 121 | Benchmark__Cbor_______Decode-8 7989 154240 ns/op 37414 B/op 315 allocs/op |
| 122 | Benchmark__Json_______Decode-8 2880 416769 ns/op 71880 B/op 592 allocs/op |
| 123 | Benchmark__Std_Json___Decode-8 1207 988487 ns/op 137146 B/op 3113 allocs/op |
| 124 | Benchmark__Gob________Decode-8 4083 300238 ns/op 159394 B/op 2329 allocs/op |
| 125 | Benchmark__JsonIter___Decode-8 2607 476016 ns/op 132690 B/op 2654 allocs/op |
| 126 | Benchmark__Bson_______Decode-8 2418 508113 ns/op 190987 B/op 4544 allocs/op |
| 127 | Benchmark__Mgobson____Decode-8 2217 543875 ns/op 168741 B/op 6776 allocs/op |
| 128 | Benchmark__VMsgpack___Decode-8 3133 391164 ns/op 100126 B/op 2030 allocs/op |
| 129 | Benchmark__Fxcbor_____Decode-8 4645 251379 ns/op 72515 B/op 1395 allocs/op |
| 130 | ``` |
| 131 | |
| 132 | * snippet of bench.out.txt, running with codecgen (2021-02-04) * |
| 133 | ``` |
| 134 | BenchmarkCodecXGenSuite/options-false.../ |
| 135 | Benchmark__Msgpack____Encode-8 26810 45254 ns/op 24 B/op 1 allocs/op |
| 136 | Benchmark__Binc_______Encode-8 25396 48931 ns/op 24 B/op 1 allocs/op |
| 137 | Benchmark__Simple_____Encode-8 28516 46036 ns/op 24 B/op 1 allocs/op |
| 138 | Benchmark__Cbor_______Encode-8 26923 44930 ns/op 24 B/op 1 allocs/op |
| 139 | Benchmark__Json_______Encode-8 9980 124222 ns/op 24 B/op 1 allocs/op |
| 140 | Benchmark__Msgp_______Encode-8 36180 33802 ns/op 0 B/op 0 allocs/op |
| 141 | Benchmark__Easyjson___Encode-8 10000 114262 ns/op 50690 B/op 12 allocs/op |
| 142 | Benchmark__Ffjson_____Encode-8 4051 299364 ns/op 128399 B/op 1121 allocs/op |
| 143 | |
| 144 | Benchmark__Msgpack____Decode-8 13057 94631 ns/op 35909 B/op 308 allocs/op |
| 145 | Benchmark__Binc_______Decode-8 12678 91177 ns/op 35909 B/op 308 allocs/op |
| 146 | Benchmark__Simple_____Decode-8 13144 90958 ns/op 35910 B/op 308 allocs/op |
| 147 | Benchmark__Cbor_______Decode-8 10000 102062 ns/op 35911 B/op 308 allocs/op |
| 148 | Benchmark__Json_______Decode-8 3019 365442 ns/op 70469 B/op 589 allocs/op |
| 149 | Benchmark__Msgp_______Decode-8 14877 79750 ns/op 68788 B/op 969 allocs/op |
| 150 | Benchmark__Easyjson___Decode-8 3150 389608 ns/op 70531 B/op 475 allocs/op |
| 151 | Benchmark__Ffjson_____Decode-8 2758 435859 ns/op 95178 B/op 1290 allocs/op |
| 152 | ``` |