再测Golang JSON类库
发布时间:2020-12-16 18:22:17 所属栏目:大数据 来源:网络整理
导读:写项目一直需要进行序列化,听到了,也看到了很多同学老师对各个golang的 json 库进行测评。那本人为什么还要继续进行这一次测评呢? 因为实践过的知识最有说服力,也是属于自己的,我也希望看到本博文的同学老师可以修改和执行测评的代码执行一遍,我相信会
写项目一直需要进行序列化,听到了,也看到了很多同学老师对各个golang的 类库
主要是针对上述的类型进行,本人采用了对不同的类库使用不同的结构体(仅仅是结构体名称不同,字段顺序和类型一样)。 环境环境为MacBook Pro(Core i5处理器/8GB内存)go1.8.3 darwin/amd64 代码bench代码如下: package jsonbench import ( "encoding/gob" "encoding/json" "github.com/json-iterator/go" "github.com/mailru/easyjson" "github.com/pquerna/ffjson/ffjson" "testing" ) var ( iterator = jsoniter.ConfigCompatibleWithStandardLibrary // easyjson as = AgentService{ ServiceName: "kaleidoscope_api",Version: "1517558949087295000_1298498081",ServiceId: "kaleidoscope_kaleidoscope.dev.igetget.com_v1.2",Address: "kaleidoscope.dev.igetget.com",Port: 80,Metadata: map[string]string{},ConnectTimeOut: 1000,ConnectType: "LONG",ReadTimeOut: 1000,WriteTimeOut: 1000,Protocol: "HTTP",Balance: "Random",Idcs: "hu,hd,hn",Converter: "json",Retry: 3,} service = as.ToService() asBytes,_ = json.Marshal(as) serviceBytes,_ = json.Marshal(service) asStr = string(asBytes) serviceStr = string(serviceBytes) asGonBytes,_ = GobEncode(as) serviceGonBytes,_ = GobEncode(service) // std asstd = AgentServiceSTD{ ServiceName: "kaleidoscope_api",} servicestd = asstd.ToServiceSTD() asBytesstd,_ = json.Marshal(asstd) serviceBytesstd,_ = json.Marshal(servicestd) asStrstd = string(asBytesstd) serviceStrstd = string(serviceBytesstd) asGonBytesstd,_ = GobEncode(asstd) serviceGonBytesstd,_ = GobEncode(servicestd) ) // go test -bench=".*" func init() { gob.Register(AgentService{}) } func Benchmark_STD_Marshal1(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := json.Marshal(asstd) if err != nil { b.Error(err) } } } func Benchmark_STD_Marshal2(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := json.Marshal(servicestd) if err != nil { b.Error(err) } } } func Benchmark_EASYJSON_STD_Marshal1(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := json.Marshal(as) if err != nil { b.Error(err) } } } func Benchmark_EASYJSON_STD_Marshal2(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := json.Marshal(service) if err != nil { b.Error(err) } } } func Benchmark_EASYJSON_Marshal1(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := easyjson.Marshal(as) if err != nil { b.Error(err) } } } func Benchmark_EASYJSON_Marshal2(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := easyjson.Marshal(service) if err != nil { b.Error(err) } } } // func Benchmark_ITERATOR_Marshal1(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := iterator.Marshal(asstd) if err != nil { b.Error(err) } } } func Benchmark_ITERATOR_Marshal2(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := iterator.Marshal(servicestd) if err != nil { b.Error(err) } } } func Benchmark_FFJSON_Marshal1(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := ffjson.Marshal(asstd) if err != nil { b.Error(err) } } } func Benchmark_FFJSON_Marshal2(b *testing.B) { for i := 0; i < b.N*10; i++ { _,err := ffjson.Marshal(servicestd) if err != nil { b.Error(err) } } } func Benchmark_GOB_Encode1(b *testing.B) { for i := 0; i < b.N*10; i++ { as.Port = i GobEncode(as) } } func Benchmark_GOB_Encode2(b *testing.B) { for i := 0; i < b.N*10; i++ { GobEncode(service) } } func Benchmark_STD_Unmarshal1(b *testing.B) { tmp := AgentServiceSTD{} for i := 0; i < b.N*10; i++ { as.Port = i err := json.Unmarshal(asBytesstd,&tmp) if err != nil { b.Error(err) } } } func Benchmark_STD_Unmarshal2(b *testing.B) { tmp := ServiceSTD{} for i := 0; i < b.N*10; i++ { as.Port = i err := json.Unmarshal(serviceBytesstd,&tmp) if err != nil { b.Error(err) } } } func Benchmark_EASYJSON_STD_Unmarshal1(b *testing.B) { tmp := AgentService{} for i := 0; i < b.N*10; i++ { as.Port = i err := json.Unmarshal(asBytes,&tmp) if err != nil { b.Error(err) } } } func Benchmark_EASYJSON_STD_Unmarshal2(b *testing.B) { tmp := Service{} for i := 0; i < b.N*10; i++ { as.Port = i err := json.Unmarshal(serviceBytes,&tmp) if err != nil { b.Error(err) } } } func Benchmark_EASYJSON_Unmarshal1(b *testing.B) { tmp := AgentService{} for i := 0; i < b.N*10; i++ { as.Port = i err := easyjson.Unmarshal(asBytes,&tmp) if err != nil { b.Error(err) } } } func Benchmark_EASYJSON_Unmarshal2(b *testing.B) { tmp := Service{} for i := 0; i < b.N*10; i++ { as.Port = i err := easyjson.Unmarshal(serviceBytes,&tmp) if err != nil { b.Error(err) } } } func Benchmark_ITERATOR_UnMarshal1(b *testing.B) { tmp := ServiceSTD{} for i := 0; i < b.N*10; i++ { as.Port = i err := iterator.Unmarshal(serviceBytesstd,&tmp) if err != nil { b.Error(err) } } } func Benchmark_ITERATOR_UnMarshal2(b *testing.B) { tmp := ServiceSTD{} for i := 0; i < b.N*10; i++ { as.Port = i err := iterator.Unmarshal(serviceBytesstd,&tmp) if err != nil { b.Error(err) } } } func Benchmark_FFJSON_UnMarshal1(b *testing.B) { tmp := ServiceSTD{} for i := 0; i < b.N*10; i++ { as.Port = i err := ffjson.Unmarshal(serviceBytesstd,&tmp) if err != nil { b.Error(err) } } } func Benchmark_FFJSON_UnMarshal2(b *testing.B) { tmp := ServiceSTD{} for i := 0; i < b.N*10; i++ { as.Port = i err := ffjson.Unmarshal(serviceBytesstd,&tmp) if err != nil { b.Error(err) } } } func Benchmark_GOB_Decode1(b *testing.B) { tmp := AgentService{} for i := 0; i < b.N*10; i++ { as.Port = i GobDecode(asGonBytes,&tmp) } } func Benchmark_GOB_Decode2(b *testing.B) { tmp := Service{} for i := 0; i < b.N*10; i++ { as.Port = i GobDecode(serviceGonBytes,&tmp) } } 执行命令: go test -bench=".*" 测评结果; $ go test -bench=".*" Benchmark_STD_Marshal1-4 50000 31224 ns/op Benchmark_STD_Marshal2-4 30000 49598 ns/op Benchmark_EASYJSON_STD_Marshal1-4 30000 45778 ns/op Benchmark_EASYJSON_STD_Marshal2-4 30000 50440 ns/op Benchmark_EASYJSON_Marshal1-4 100000 14387 ns/op Benchmark_EASYJSON_Marshal2-4 100000 16009 ns/op Benchmark_ITERATOR_Marshal1-4 100000 14899 ns/op Benchmark_ITERATOR_Marshal2-4 100000 21629 ns/op Benchmark_FFJSON_Marshal1-4 50000 31633 ns/op Benchmark_FFJSON_Marshal2-4 30000 51668 ns/op Benchmark_GOB_Encode1-4 20000 97099 ns/op Benchmark_GOB_Encode2-4 10000 153158 ns/op Benchmark_STD_Unmarshal1-4 20000 89211 ns/op Benchmark_STD_Unmarshal2-4 20000 76442 ns/op Benchmark_EASYJSON_STD_Unmarshal1-4 30000 57695 ns/op Benchmark_EASYJSON_STD_Unmarshal2-4 20000 66269 ns/op Benchmark_EASYJSON_Unmarshal1-4 100000 19028 ns/op Benchmark_EASYJSON_Unmarshal2-4 100000 22035 ns/op Benchmark_ITERATOR_UnMarshal1-4 50000 35942 ns/op Benchmark_ITERATOR_UnMarshal2-4 50000 36462 ns/op Benchmark_FFJSON_UnMarshal1-4 20000 80290 ns/op Benchmark_FFJSON_UnMarshal2-4 20000 78431 ns/op Benchmark_GOB_Decode1-4 3000 377698 ns/op Benchmark_GOB_Decode2-4 3000 463472 ns/op PASS ok studygo/jsonbench 49.174s 结论
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |