Code Monkey home page Code Monkey logo

gosercomp's Introduction

Golang 序列化反序列化库的性能比较

测试的 Serializers

以golang自带的_encoding/json_和_encoding/xml_为基准,测试以下性能比较好的几种序列化库。

排除的 Serializers

基于 alecthomas 已有的测试,下面的库由于性能的原因没有进行测试。

测试环境

go version: 1.8.1

  • 对于 MessagePack,你需要安装库以及利用go generate生成相关的类:

    go get github.com/tinylib/msgp
    go generate
  • 对于ProtoBuf,你需要安装protoc编译器,以及protoc库以及生成相关的类:

    go get github.com/golang/protobuf
    go generate
  • 对于gogo/protobuf,你需要安装库以及生成相关的类:

    go get github.com/gogo/protobuf/gogoproto
    go get -u github.com/gogo/protobuf/protoc-gen-gogofaster
    go generate
  • 对于flatbuffers,你需要安装[flatbuffers编译器](https://github.com/google/flatbuffers/releases, 以及flatbuffers库:

    go get github.com/google/flatbuffers/go
    go generate
  • 对于thrift,), 你需要安装thrift编译器以及thrift库:

    go get git.apache.org/thrift.git/lib/go/thrift
    go generate
  • 对于Avro,你需要安装goavro库:

    go get github.com/linkedin/goavro
    go generate
  • 对于gencode,你需要安装gencode库,并使用gencode库的工具产生数据对象:

    go get github.com/andyleap/gencode
    bin\gencode.exe go -schema=gencode.schema -package gosercomp

    gencode也是一个高性能的编解码库,提供了代码生成工具,而且产生的数据非常的小。

  • 对于easyjson,你需要安装easyjson库:

    go get github.com/mailru/easyjson
    go generate
  • 对于zebraPack ,你需要安装zebraPack库,并使用zebraPack工具产生数据对象:

    go get github.com/glycerine/zebrapack
    go generate zebrapack_data.go 
  • 对于ugorji/go/codec,你需要安装代码生成工具和codec库:

  go get -tags=unsafe  -u github.com/ugorji/go/codec/codecgen
  go get -tags=unsafe -u github.com/ugorji/go/codec

  codecgen.exe -o data_codec.go data.go

ugorji/go/codec是一个高性能的编解码框架,支持 msgpack、cbor、binc、json等格式。本测试中测试了 cbor 和 msgpack的编解码,可以和上面的 tinylib/msgp框架进行比较。

事实上,这里通过go generate生成相关的类,你也可以通过命令行生成,请参考data.go中的注释。 但是你需要安装相关的工具,如Thrift,并把它们加入到环境变量Path中

运行下面的命令测试:

go test -bench=. -benchmem

测试数据

所有的测试基于以下的struct,自动生成的struct, 比如protobuf也和此结构基本一致。 所以本测试的数据以小数据为主, 不同的测试数据(数据大小,数据类型)可能会导致各框架的表现不一样,注意区别。

type ColorGroup struct {
    ID     int `json:"id" xml:"id,attr""`
    Name   string `json:"name" xml:"name"`
    Colors []string `json:"colors" xml:"colors"`
}
`

性能测试结果

BenchmarkMarshalByJson-4                       	 1000000	      1092 ns/op	     376 B/op	       4 allocs/op
BenchmarkUnmarshalByJson-4                     	  500000	      2765 ns/op	     344 B/op	       9 allocs/op

BenchmarkMarshalByXml-4                        	  500000	      3958 ns/op	    4801 B/op	      12 allocs/op
BenchmarkUnmarshalByXml-4                      	  100000	     16309 ns/op	    3139 B/op	      75 allocs/op

BenchmarkMarshalByMsgp-4                       	20000000	       117 ns/op	      80 B/op	       1 allocs/op
BenchmarkUnmarshalByMsgp-4                     	 5000000	       283 ns/op	      32 B/op	       5 allocs/op

BenchmarkMarshalByProtoBuf-4                   	 3000000	       492 ns/op	     328 B/op	       5 allocs/op
BenchmarkUnmarshalByProtoBuf-4                 	 2000000	       849 ns/op	     400 B/op	      11 allocs/op

BenchmarkMarshalByGogoProtoBuf-4               	20000000	       114 ns/op	      48 B/op	       1 allocs/op
BenchmarkUnmarshalByGogoProtoBuf-4             	 3000000	       467 ns/op	     144 B/op	       8 allocs/op

BenchmarkMarshalByFlatBuffers-4                	 3000000	       416 ns/op	      16 B/op	       1 allocs/op
BenchmarkUnmarshalByFlatBuffers-4              	500000000	         3.25 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnmarshalByFlatBuffers_withFields-4   	10000000	       188 ns/op	       0 B/op	       0 allocs/op

BenchmarkMarshalByThrift-4                     	 3000000	       590 ns/op	      64 B/op	       1 allocs/op
BenchmarkUnmarshalByThrift-4                   	 1000000	      1358 ns/op	     656 B/op	      11 allocs/op

BenchmarkMarshalByAvro-4                       	 2000000	       726 ns/op	      56 B/op	       7 allocs/op
BenchmarkUnmarshalByAvro-4                     	  500000	      3648 ns/op	    1680 B/op	      63 allocs/op

BenchmarkMarshalByGencode-4                    	30000000	        43.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnmarshalByGencode-4                  	10000000	       171 ns/op	      32 B/op	       5 allocs/op

BenchmarkMarshalByUgorjiCodecAndCbor-4         	 2000000	       744 ns/op	      96 B/op	       2 allocs/op
BenchmarkUnmarshalByUgorjiCodecAndCbor-4       	 2000000	       610 ns/op	      32 B/op	       5 allocs/op

BenchmarkMarshalByUgorjiCodecAndMsgp-4         	 2000000	       723 ns/op	      96 B/op	       2 allocs/op
BenchmarkUnmarshalByUgorjiCodecAndMsgp-4       	 2000000	       600 ns/op	      32 B/op	       5 allocs/op

BenchmarkMarshalByUgorjiCodecAndBinc-4         	 2000000	       746 ns/op	      96 B/op	       2 allocs/op
BenchmarkUnmarshalByUgorjiCodecAndBinc-4       	 2000000	       631 ns/op	      32 B/op	       5 allocs/op

BenchmarkMarshalByUgorjiCodecAndJson-4         	 1000000	      1096 ns/op	      96 B/op	       2 allocs/op
BenchmarkUnmarshalByUgorjiCodecAndJson-4       	 1000000	      1068 ns/op	     128 B/op	       8 allocs/op

BenchmarkMarshalByEasyjson-4                   	 5000000	       361 ns/op	     128 B/op	       1 allocs/op
BenchmarkUnmarshalByEasyjson-4                 	 3000000	       584 ns/op	      32 B/op	       5 allocs/op

BenchmarkMarshalByFfjson-4                     	 1000000	      1032 ns/op	     424 B/op	       9 allocs/op
BenchmarkUnmarshalByFfjson-4                   	 1000000	      1532 ns/op	     480 B/op	      13 allocs/op

BenchmarkMarshalByJsoniter-4                   	 1000000	      1725 ns/op	    4384 B/op	       5 allocs/op
BenchmarkUnmarshalByJsoniter-4                 	 3000000	       524 ns/op	     240 B/op	       6 allocs/op

BenchmarkUnmarshalByGJSON-4                    	 1000000	      1975 ns/op	     624 B/op	       7 allocs/op

BenchmarkMarshalByGoMemdump-4                  	  300000	      5892 ns/op	    1040 B/op	      31 allocs/op
BenchmarkUnmarshalByGoMemdump-4                	 1000000	      1331 ns/op	    2288 B/op	      11 allocs/op

BenchmarkMarshalByColfer-4                     	50000000	        33.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnmarshalByColfer-4                   	 5000000	       248 ns/op	      96 B/op	       6 allocs/op

BenchmarkMarshalByZebrapack-4                  	10000000	       276 ns/op	     132 B/op	       0 allocs/op
BenchmarkUnmarshalByZebrapack-4                	10000000	       172 ns/op	       0 B/op	       0 allocs/op

BenchmarkMarshalByGotiny-4                     	 5000000	       387 ns/op	     144 B/op	       5 allocs/op
BenchmarkUnmarshalByGotiny-4                   	 5000000	       277 ns/op	      88 B/op	       2 allocs/op

BenchmarkMarshalByHprose-4                     	 3000000	       520 ns/op	     210 B/op	       1 allocs/op
BenchmarkUnmarshalByHprose-4                   	 2000000	       712 ns/op	     288 B/op	       9 allocs/op

BenchmarkMarshalBySereal-4                     	  500000	      2512 ns/op	     792 B/op	      22 allocs/op
BenchmarkUnmarshalBySereal-4                   	 2000000	       846 ns/op	      80 B/op	       6 allocs/op

BenchmarkMarshalByMsgpackV2-4                  	 1000000	      2121 ns/op	     192 B/op	       4 allocs/op
BenchmarkUnmarshalByMsgpackv2-4                	 1000000	      1893 ns/op	     232 B/op	      11 allocs/op

多次测试结果差不多。 从结果上上来看, MessagePack,gogo/protobuf,和flatbuffers差不多,这三个优秀的库在序列化和反序列化上各有千秋,而且都是跨语言的。 从便利性上来讲,你可以选择MessagePackgogo/protobuf都可以,两者都有大厂在用。 flatbuffers有点反人类,因为它的操作很底层,而且从结果上来看,序列化的性能要差一点。但是它有一个好处,那就是如果你只需要特定的字段, 你无须将所有的字段都反序列化。从结果上看,不反序列化字段每个调用只用了9.54纳秒,这是因为字段只有在被访问的时候才从byte数组转化为相应的类型。 因此在特殊的场景下,它可以提高N被的性能。但是序列化的代码的面相太难看了。

新增加了gencode的测试,它表现相当出色,而且生成的字节也非常的小。

Codec的Unmarshal性能不错,但是Marshal性能不是太好。

colfer的性能也不错,它能够跨Go,Java, Javascript平台。

新加入的zebrapack性能抢眼,不但性能卓越,而且可以实现zero allocation,值得关注。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.