Created
June 30, 2014 03:02
-
-
Save oxtoacart/e7d09f0f6c27338271f7 to your computer and use it in GitHub Desktop.
Benchmark of JSON, Protocol Buffers and MsgPack in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by protoc-gen-go. | |
// source: datum.proto | |
// DO NOT EDIT! | |
/* | |
Package sbenchmark is a generated protocol buffer package. | |
It is generated from these files: | |
datum.proto | |
It has these top-level messages: | |
PBDatum | |
*/ | |
package sbenchmark | |
import proto "code.google.com/p/goprotobuf/proto" | |
import math "math" | |
// Reference imports to suppress errors if they are not otherwise used. | |
var _ = proto.Marshal | |
var _ = math.Inf | |
type PBDatum struct { | |
S *string `protobuf:"bytes,1,req" json:"S,omitempty"` | |
I *int64 `protobuf:"varint,2,req" json:"I,omitempty"` | |
F *float64 `protobuf:"fixed64,3,req" json:"F,omitempty"` | |
B *bool `protobuf:"varint,4,req" json:"B,omitempty"` | |
XXX_unrecognized []byte `json:"-"` | |
} | |
func (m *PBDatum) Reset() { *m = PBDatum{} } | |
func (m *PBDatum) String() string { return proto.CompactTextString(m) } | |
func (*PBDatum) ProtoMessage() {} | |
func (m *PBDatum) GetS() string { | |
if m != nil && m.S != nil { | |
return *m.S | |
} | |
return "" | |
} | |
func (m *PBDatum) GetI() int64 { | |
if m != nil && m.I != nil { | |
return *m.I | |
} | |
return 0 | |
} | |
func (m *PBDatum) GetF() float64 { | |
if m != nil && m.F != nil { | |
return *m.F | |
} | |
return 0 | |
} | |
func (m *PBDatum) GetB() bool { | |
if m != nil && m.B != nil { | |
return *m.B | |
} | |
return false | |
} | |
func init() { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sbenchmark; | |
message PBDatum { | |
required string S = 1; | |
required int64 I = 2; | |
required double F = 3; | |
required bool B = 4; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sbenchmark | |
import ( | |
"encoding/json" | |
"testing" | |
"code.google.com/p/goprotobuf/proto" | |
"github.com/ugorji/go/codec" | |
) | |
type Datum struct { | |
S string | |
I int64 | |
F float64 | |
B bool | |
} | |
func BenchmarkMsgPack(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var mh codec.MsgpackHandle | |
var buf []byte | |
d := makeDatum() | |
var d2 Datum | |
enc := codec.NewEncoderBytes(&buf, &mh) | |
err := enc.Encode(d) | |
if err != nil { | |
b.Fatalf("Unable to encode: %s", err) | |
} | |
dec := codec.NewDecoderBytes(buf, &mh) | |
err = dec.Decode(&d2) | |
if err != nil { | |
b.Fatalf("Unable to decode: %s", err) | |
} | |
if *d != d2 { | |
b.Fatalf("Decoded did not match encoded. Expected %s, got %s", d, d2) | |
} | |
} | |
} | |
func BenchmarkJSON(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
d := makeDatum() | |
var d2 Datum | |
buf, err := json.Marshal(d) | |
if err != nil { | |
b.Fatalf("Unable to marshal: %s", err) | |
} | |
err = json.Unmarshal(buf, &d2) | |
if err != nil { | |
b.Fatalf("Unable to unmarshal: %s", err) | |
} | |
if *d != d2 { | |
b.Fatalf("Decoded did not match encoded. Expected %s, got %s", d, d2) | |
} | |
} | |
} | |
func BenchmarkProtoBuf(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
d := makePBDatum() | |
var d2 PBDatum | |
buf, err := proto.Marshal(d) | |
if err != nil { | |
b.Fatalf("Unable to marshal: %s", err) | |
} | |
err = proto.Unmarshal(buf, &d2) | |
if err != nil { | |
b.Fatalf("Unable to unmarshal: %s", err) | |
} | |
// if *d != d2 { | |
// b.Fatalf("Decoded did not match encoded. Expected %s, got %s", d, d2) | |
// } | |
} | |
} | |
func makeDatum() *Datum { | |
return &Datum{ | |
S: "Some String", | |
I: 560, | |
F: 56.0024543, | |
B: true, | |
} | |
} | |
var defaultDatum = makeDatum() | |
func makePBDatum() *PBDatum { | |
return &PBDatum{ | |
S: &defaultDatum.S, | |
I: &defaultDatum.I, | |
F: &defaultDatum.F, | |
B: &defaultDatum.B, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment