Skip to content

gogo jsonpb is 10x slower than golang json Unmarshal? #699

Description

@blackbeans

Go version:1.13
protobuf: 3.6.x

code :


message Cursor {
    string ID = 1 [json_name = "id",(gogoproto.jsontag)="id"]; 
    uint32 Type = 2 [json_name = "type",(gogoproto.jsontag)="type",(gogoproto.customtype)="uint16",(gogoproto.nullable) = false]; 
    int64 Offset = 3 [json_name = "offset",(gogoproto.jsontag)="offset",(gogoproto.nullable) = false,(gogoproto.customtype) ="encoding/json.Number"]; 
    int64 QTimestamp = 4 [json_name = "timestamp",(gogoproto.jsontag)="timestamp",(gogoproto.nullable) = false,(gogoproto.customtype) ="encoding/json.Number"];
    int64 EndOffset = 5 [json_name = "end_offset",(gogoproto.jsontag)="end_offset",(gogoproto.nullable) = false,(gogoproto.customtype) ="encoding/json.Number"]; 
}


func BenchmarkJsonPb(t *testing.B) {
	rawMark := []byte(`{"id":"1224","type":2,"offset":9136,"timestamp":1593935873,"end_offset":0}`)
	unmarshaler := &jsonpb.Unmarshaler{AllowUnknownFields: true}
	for i := 0; i < t.N; i++ {
		var cursor Cursor
		unmarshaler.Unmarshal(bytes.NewReader(rawMark), &cursor)
	}
}

func BenchmarkJson(t *testing.B) {
	rawMark := []byte(`{"id":"1224","type":2,"offset":9136,"timestamp":1593935873,"end_offset":0}`)

	for i := 0; i < t.N; i++ {
		var cursor Cursor
		json.Unmarshal(rawMark, &cursor)
	}
}
goos: darwin
goarch: amd64
BenchmarkJsonPb-4         104812             11316 ns/op
BenchmarkJson-4           671342              1805 ns/op


above all, i have upgrade our project to protobuf and used jsonpb to output json format for communication .But i found than jsonpb Unmarshals the same json-data is 10x slower than golang 1.13.x .

i lost my way~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions