json 类库
| 序号 | 类库 | 地址 | 备注 |
|---|---|---|---|
| 1 | encoding/json | Golan | |
| 2 | easyjson | github.com/mailru/easyjson | |
| 3 | ffjson | github.com/mailru/easyjson | |
| 4 | iterator/json | github.com/json-iterator/go |
主要是针对上述的类型进行,本人采用了对不同的类库使用不同的结构体(仅仅是结构体名称不同,字段顺序和类型一样)。
环境
环境为MacBook Pro(Core i5处理器/8GB内存)go1.8.3 darwin/amd64
代码
bench代码如下:
package jsonbenchimport ("encoding/gob""encoding/json""github.com/json-iterator/go""github.com/mailru/easyjson""github.com/pquerna/ffjson/ffjson""testing"
)var (iterator = jsoniter.ConfigCompatibleWithStandardLibrary// easyjsonas = 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)// stdasstd = AgentServiceSTD{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,}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 = iGobEncode(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 = ierr := 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 = ierr := 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 = ierr := 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 = ierr := 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 = ierr := 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 = ierr := 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 = ierr := 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 = ierr := 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 = ierr := 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 = ierr := 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 = iGobDecode(asGonBytes, &tmp)}
}func Benchmark_GOB_Decode2(b *testing.B) {tmp := Service{}for i := 0; i < b.N*10; i++ {as.Port = iGobDecode(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 结论
easyjsonBenchmark_EASYJSON_STD_*MarshalJSONUnmarshalJSONjson.marshalJSONjson.UnmarshalJSONMarshalJSONeasyjsonUnmarshalJSONeasyjsoninterfaceeasyjson