Skip to content

Commit

Permalink
Add protobuf.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Oct 23, 2014
1 parent d0b5d4e commit 34f7bed
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 24 deletions.
51 changes: 27 additions & 24 deletions README.md
Expand Up @@ -60,41 +60,44 @@ type A struct {
Results on my late 2013 MacBook Pro 15" are:

```
BenchmarkUgorjiMsgpackMarshal 500000 3787 ns/op 1327 B/op 21 allocs/op
BenchmarkUgorjiMsgpackUnmarshal 500000 3730 ns/op 644 B/op 20 allocs/op
BenchmarkUgorjiMsgpackMarshal 500000 3876 ns/op 1326 B/op 21 allocs/op
BenchmarkUgorjiMsgpackUnmarshal 500000 3541 ns/op 644 B/op 20 allocs/op
BenchmarkVmihailencoMsgpackMarshal 1000000 1697 ns/op 412 B/op 6 allocs/op
BenchmarkVmihailencoMsgpackUnmarshal 1000000 1971 ns/op 421 B/op 10 allocs/op
BenchmarkVmihailencoMsgpackMarshal 1000000 1682 ns/op 413 B/op 6 allocs/op
BenchmarkVmihailencoMsgpackUnmarshal 1000000 2012 ns/op 421 B/op 10 allocs/op
BenchmarkJsonMarshal 500000 3253 ns/op 590 B/op 7 allocs/op
BenchmarkJsonUnmarshal 500000 5348 ns/op 468 B/op 7 allocs/op
BenchmarkJsonMarshal 1000000 2969 ns/op 590 B/op 7 allocs/op
BenchmarkJsonUnmarshal 500000 4745 ns/op 468 B/op 7 allocs/op
BenchmarkBsonMarshal 1000000 2200 ns/op 488 B/op 13 allocs/op
BenchmarkBsonUnmarshal 1000000 2590 ns/op 281 B/op 10 allocs/op
BenchmarkBsonMarshal 1000000 1991 ns/op 488 B/op 13 allocs/op
BenchmarkBsonUnmarshal 1000000 2294 ns/op 281 B/op 10 allocs/op
BenchmarkVitessBsonMarshal 1000000 1460 ns/op 1169 B/op 4 allocs/op
BenchmarkVitessBsonUnmarshal 2000000 959 ns/op 227 B/op 4 allocs/op
BenchmarkVitessBsonMarshal 1000000 1452 ns/op 1169 B/op 4 allocs/op
BenchmarkVitessBsonUnmarshal 2000000 823 ns/op 227 B/op 4 allocs/op
BenchmarkGobMarshal 500000 6813 ns/op 1661 B/op 25 allocs/op
BenchmarkGobUnmarshal 50000 48258 ns/op 18986 B/op 365 allocs/op
BenchmarkGobMarshal 500000 6797 ns/op 1661 B/op 25 allocs/op
BenchmarkGobUnmarshal 50000 48448 ns/op 19196 B/op 365 allocs/op
BenchmarkXdrMarshal 1000000 2596 ns/op 519 B/op 15 allocs/op
BenchmarkXdrUnmarshal 1000000 1909 ns/op 274 B/op 9 allocs/op
BenchmarkXdrMarshal 1000000 2623 ns/op 520 B/op 15 allocs/op
BenchmarkXdrUnmarshal 1000000 1954 ns/op 274 B/op 9 allocs/op
BenchmarkUgorjiCodecMsgpackMarshal 500000 3669 ns/op 1428 B/op 22 allocs/op
BenchmarkUgorjiCodecMsgpackUnmarshal 500000 3679 ns/op 1145 B/op 29 allocs/op
BenchmarkUgorjiCodecMsgpackMarshal 500000 3629 ns/op 1428 B/op 22 allocs/op
BenchmarkUgorjiCodecMsgpackUnmarshal 500000 3616 ns/op 1146 B/op 29 allocs/op
BenchmarkUgorjiCodecBincMarshal 500000 4973 ns/op 2141 B/op 24 allocs/op
BenchmarkUgorjiCodecBincUnmarshal 500000 4843 ns/op 2064 B/op 34 allocs/op
BenchmarkUgorjiCodecBincMarshal 500000 4984 ns/op 2141 B/op 24 allocs/op
BenchmarkUgorjiCodecBincUnmarshal 500000 4902 ns/op 2066 B/op 34 allocs/op
BenchmarkSerealMarshal 500000 4035 ns/op 1278 B/op 21 allocs/op
BenchmarkSerealUnmarshal 500000 4437 ns/op 696 B/op 30 allocs/op
BenchmarkSerealMarshal 500000 4189 ns/op 1278 B/op 21 allocs/op
BenchmarkSerealUnmarshal 500000 4433 ns/op 697 B/op 30 allocs/op
BenchmarkBinaryMarshal 1000000 2193 ns/op 479 B/op 15 allocs/op
BenchmarkBinaryUnmarshal 1000000 2221 ns/op 432 B/op 17 allocs/op
BenchmarkBinaryMarshal 1000000 2184 ns/op 479 B/op 15 allocs/op
BenchmarkBinaryUnmarshal 1000000 2198 ns/op 433 B/op 17 allocs/op
BenchmarkMsgpMarshal 5000000 648 ns/op 128 B/op 2 allocs/op
BenchmarkMsgpUnmarshal 5000000 476 ns/op 113 B/op 3 allocs/op
BenchmarkMsgpMarshal 5000000 654 ns/op 128 B/op 2 allocs/op
BenchmarkMsgpUnmarshal 5000000 466 ns/op 113 B/op 3 allocs/op
BenchmarkGoprotobufMarshal 2000000 827 ns/op 314 B/op 3 allocs/op
BenchmarkGoprotobufUnmarshal 1000000 1104 ns/op 440 B/op 9 allocs/op
```

**Note:** the gob results are not really representative of normal performance, as gob is designed for serializing streams or vectors of a single type, not individual values.
Expand Down
53 changes: 53 additions & 0 deletions serialization_benchmarks_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"code.google.com/p/goprotobuf/proto"
"github.com/Sereal/Sereal/Go/sereal"
"github.com/alecthomas/binary"
"github.com/davecgh/go-xdr/xdr"
Expand Down Expand Up @@ -48,6 +49,21 @@ func generate() []*A {
return a
}

func generateProto() []*ProtoBufA {
a := make([]*ProtoBufA, 0, 1000)
for i := 0; i < 1000; i++ {
a = append(a, &ProtoBufA{
Name: proto.String(randString(16)),
BirthDay: proto.Int64(time.Now().Unix()),
Phone: proto.String(randString(10)),
Siblings: proto.Int32(rand.Int31n(5)),
Spouse: proto.Bool(rand.Intn(2) == 1),
Money: proto.Float64(rand.Float64()),
})
}
return a
}

type Serializer interface {
Marshal(o interface{}) []byte
Unmarshal(d []byte, o interface{}) error
Expand Down Expand Up @@ -420,3 +436,40 @@ func BenchmarkMsgpUnmarshal(b *testing.B) {
s.en = enc.NewEncoder(&s.buf)
benchUnmarshal(b, &s)
}

func BenchmarkGoprotobufMarshal(b *testing.B) {
b.StopTimer()
data := generateProto()
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
proto.Marshal(data[rand.Intn(len(data))])
}
}

func BenchmarkGoprotobufUnmarshal(b *testing.B) {
b.StopTimer()
data := generateProto()
ser := make([][]byte, len(data))
for i, d := range data {
ser[i], _ = proto.Marshal(d)
}
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
n := rand.Intn(len(ser))
o := &ProtoBufA{}
err := proto.Unmarshal(ser[n], o)
if err != nil {
b.Fatalf("goprotobuf failed to unmarshal: %s (%s)", err, ser[n])
}
// Validate unmarshalled data.
if validate != "" {
i := data[n]
correct := *o.Name == *i.Name && *o.Phone == *i.Phone && *o.Siblings == *i.Siblings && *o.Spouse == *i.Spouse && *o.Money == *i.Money && *o.BirthDay == *i.BirthDay //&& cmpTags(o.Tags, i.Tags) && cmpAliases(o.Aliases, i.Aliases)
if !correct {
b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
}
}
}
}
80 changes: 80 additions & 0 deletions structdef.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions structdef.proto
@@ -0,0 +1,10 @@
package goserbench;

message ProtoBufA {
required string name = 1;
required int64 birthDay = 2;
required string phone = 3;
required int32 siblings = 4;
required bool spouse = 5;
required double money = 6;
}

0 comments on commit 34f7bed

Please sign in to comment.