Skip to content

Commit 34f7bed

Browse files
committed
Add protobuf.
1 parent d0b5d4e commit 34f7bed

File tree

4 files changed

+170
-24
lines changed

4 files changed

+170
-24
lines changed

README.md

+27-24
Original file line numberDiff line numberDiff line change
@@ -60,41 +60,44 @@ type A struct {
6060
Results on my late 2013 MacBook Pro 15" are:
6161

6262
```
63-
BenchmarkUgorjiMsgpackMarshal 500000 3787 ns/op 1327 B/op 21 allocs/op
64-
BenchmarkUgorjiMsgpackUnmarshal 500000 3730 ns/op 644 B/op 20 allocs/op
63+
BenchmarkUgorjiMsgpackMarshal 500000 3876 ns/op 1326 B/op 21 allocs/op
64+
BenchmarkUgorjiMsgpackUnmarshal 500000 3541 ns/op 644 B/op 20 allocs/op
6565
66-
BenchmarkVmihailencoMsgpackMarshal 1000000 1697 ns/op 412 B/op 6 allocs/op
67-
BenchmarkVmihailencoMsgpackUnmarshal 1000000 1971 ns/op 421 B/op 10 allocs/op
66+
BenchmarkVmihailencoMsgpackMarshal 1000000 1682 ns/op 413 B/op 6 allocs/op
67+
BenchmarkVmihailencoMsgpackUnmarshal 1000000 2012 ns/op 421 B/op 10 allocs/op
6868
69-
BenchmarkJsonMarshal 500000 3253 ns/op 590 B/op 7 allocs/op
70-
BenchmarkJsonUnmarshal 500000 5348 ns/op 468 B/op 7 allocs/op
69+
BenchmarkJsonMarshal 1000000 2969 ns/op 590 B/op 7 allocs/op
70+
BenchmarkJsonUnmarshal 500000 4745 ns/op 468 B/op 7 allocs/op
7171
72-
BenchmarkBsonMarshal 1000000 2200 ns/op 488 B/op 13 allocs/op
73-
BenchmarkBsonUnmarshal 1000000 2590 ns/op 281 B/op 10 allocs/op
72+
BenchmarkBsonMarshal 1000000 1991 ns/op 488 B/op 13 allocs/op
73+
BenchmarkBsonUnmarshal 1000000 2294 ns/op 281 B/op 10 allocs/op
7474
75-
BenchmarkVitessBsonMarshal 1000000 1460 ns/op 1169 B/op 4 allocs/op
76-
BenchmarkVitessBsonUnmarshal 2000000 959 ns/op 227 B/op 4 allocs/op
75+
BenchmarkVitessBsonMarshal 1000000 1452 ns/op 1169 B/op 4 allocs/op
76+
BenchmarkVitessBsonUnmarshal 2000000 823 ns/op 227 B/op 4 allocs/op
7777
78-
BenchmarkGobMarshal 500000 6813 ns/op 1661 B/op 25 allocs/op
79-
BenchmarkGobUnmarshal 50000 48258 ns/op 18986 B/op 365 allocs/op
78+
BenchmarkGobMarshal 500000 6797 ns/op 1661 B/op 25 allocs/op
79+
BenchmarkGobUnmarshal 50000 48448 ns/op 19196 B/op 365 allocs/op
8080
81-
BenchmarkXdrMarshal 1000000 2596 ns/op 519 B/op 15 allocs/op
82-
BenchmarkXdrUnmarshal 1000000 1909 ns/op 274 B/op 9 allocs/op
81+
BenchmarkXdrMarshal 1000000 2623 ns/op 520 B/op 15 allocs/op
82+
BenchmarkXdrUnmarshal 1000000 1954 ns/op 274 B/op 9 allocs/op
8383
84-
BenchmarkUgorjiCodecMsgpackMarshal 500000 3669 ns/op 1428 B/op 22 allocs/op
85-
BenchmarkUgorjiCodecMsgpackUnmarshal 500000 3679 ns/op 1145 B/op 29 allocs/op
84+
BenchmarkUgorjiCodecMsgpackMarshal 500000 3629 ns/op 1428 B/op 22 allocs/op
85+
BenchmarkUgorjiCodecMsgpackUnmarshal 500000 3616 ns/op 1146 B/op 29 allocs/op
8686
87-
BenchmarkUgorjiCodecBincMarshal 500000 4973 ns/op 2141 B/op 24 allocs/op
88-
BenchmarkUgorjiCodecBincUnmarshal 500000 4843 ns/op 2064 B/op 34 allocs/op
87+
BenchmarkUgorjiCodecBincMarshal 500000 4984 ns/op 2141 B/op 24 allocs/op
88+
BenchmarkUgorjiCodecBincUnmarshal 500000 4902 ns/op 2066 B/op 34 allocs/op
8989
90-
BenchmarkSerealMarshal 500000 4035 ns/op 1278 B/op 21 allocs/op
91-
BenchmarkSerealUnmarshal 500000 4437 ns/op 696 B/op 30 allocs/op
90+
BenchmarkSerealMarshal 500000 4189 ns/op 1278 B/op 21 allocs/op
91+
BenchmarkSerealUnmarshal 500000 4433 ns/op 697 B/op 30 allocs/op
9292
93-
BenchmarkBinaryMarshal 1000000 2193 ns/op 479 B/op 15 allocs/op
94-
BenchmarkBinaryUnmarshal 1000000 2221 ns/op 432 B/op 17 allocs/op
93+
BenchmarkBinaryMarshal 1000000 2184 ns/op 479 B/op 15 allocs/op
94+
BenchmarkBinaryUnmarshal 1000000 2198 ns/op 433 B/op 17 allocs/op
9595
96-
BenchmarkMsgpMarshal 5000000 648 ns/op 128 B/op 2 allocs/op
97-
BenchmarkMsgpUnmarshal 5000000 476 ns/op 113 B/op 3 allocs/op
96+
BenchmarkMsgpMarshal 5000000 654 ns/op 128 B/op 2 allocs/op
97+
BenchmarkMsgpUnmarshal 5000000 466 ns/op 113 B/op 3 allocs/op
98+
99+
BenchmarkGoprotobufMarshal 2000000 827 ns/op 314 B/op 3 allocs/op
100+
BenchmarkGoprotobufUnmarshal 1000000 1104 ns/op 440 B/op 9 allocs/op
98101
```
99102

100103
**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.

serialization_benchmarks_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13+
"code.google.com/p/goprotobuf/proto"
1314
"github.com/Sereal/Sereal/Go/sereal"
1415
"github.com/alecthomas/binary"
1516
"github.com/davecgh/go-xdr/xdr"
@@ -48,6 +49,21 @@ func generate() []*A {
4849
return a
4950
}
5051

52+
func generateProto() []*ProtoBufA {
53+
a := make([]*ProtoBufA, 0, 1000)
54+
for i := 0; i < 1000; i++ {
55+
a = append(a, &ProtoBufA{
56+
Name: proto.String(randString(16)),
57+
BirthDay: proto.Int64(time.Now().Unix()),
58+
Phone: proto.String(randString(10)),
59+
Siblings: proto.Int32(rand.Int31n(5)),
60+
Spouse: proto.Bool(rand.Intn(2) == 1),
61+
Money: proto.Float64(rand.Float64()),
62+
})
63+
}
64+
return a
65+
}
66+
5167
type Serializer interface {
5268
Marshal(o interface{}) []byte
5369
Unmarshal(d []byte, o interface{}) error
@@ -420,3 +436,40 @@ func BenchmarkMsgpUnmarshal(b *testing.B) {
420436
s.en = enc.NewEncoder(&s.buf)
421437
benchUnmarshal(b, &s)
422438
}
439+
440+
func BenchmarkGoprotobufMarshal(b *testing.B) {
441+
b.StopTimer()
442+
data := generateProto()
443+
b.ReportAllocs()
444+
b.StartTimer()
445+
for i := 0; i < b.N; i++ {
446+
proto.Marshal(data[rand.Intn(len(data))])
447+
}
448+
}
449+
450+
func BenchmarkGoprotobufUnmarshal(b *testing.B) {
451+
b.StopTimer()
452+
data := generateProto()
453+
ser := make([][]byte, len(data))
454+
for i, d := range data {
455+
ser[i], _ = proto.Marshal(d)
456+
}
457+
b.ReportAllocs()
458+
b.StartTimer()
459+
for i := 0; i < b.N; i++ {
460+
n := rand.Intn(len(ser))
461+
o := &ProtoBufA{}
462+
err := proto.Unmarshal(ser[n], o)
463+
if err != nil {
464+
b.Fatalf("goprotobuf failed to unmarshal: %s (%s)", err, ser[n])
465+
}
466+
// Validate unmarshalled data.
467+
if validate != "" {
468+
i := data[n]
469+
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)
470+
if !correct {
471+
b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
472+
}
473+
}
474+
}
475+
}

structdef.pb.go

+80
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

structdef.proto

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package goserbench;
2+
3+
message ProtoBufA {
4+
required string name = 1;
5+
required int64 birthDay = 2;
6+
required string phone = 3;
7+
required int32 siblings = 4;
8+
required bool spouse = 5;
9+
required double money = 6;
10+
}

0 commit comments

Comments
 (0)