Skip to content

Commit ffc2e02

Browse files
add cosmos-proto benchmarks (alecthomas#115)
1 parent 2a5f1fc commit ffc2e02

File tree

7 files changed

+1077
-122
lines changed

7 files changed

+1077
-122
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This is necessary due to the use of two conflicting generator commands for capnproto
22
.NOTPARALLEL:
33

4-
all: Colfer.go FlatBufferA.go msgp_gen.go structdef-gogo.pb.go structdef.pb.go structdefxdr_generated.go structdef-bebop.go structdef_msgpackgen.go musgo.go
4+
all: Colfer.go FlatBufferA.go msgp_gen.go structdef-gogo.pb.go structdef.pb.go structdefxdr_generated.go structdef-bebop.go structdef_msgpackgen.go musgo.go structdef.pulsar.go
55

66
Colfer.go:
77
go run github.com/pascaldekloe/colfer/cmd/colf@latest go
@@ -25,6 +25,9 @@ structdef-gogo.pb.go: structdef-gogo.proto
2525
structdef.pb.go: structdef.proto
2626
protoc --go_opt=paths=source_relative --go_out=. structdef.proto
2727

28+
structdef-pulsar.pulsar.go: structdef-pulsar.proto
29+
protoc --go-pulsar_out=. --go-pulsar_opt=paths=source_relative --go-pulsar_opt=features=protoc+fast -I . structdef-pulsar.proto
30+
2831
#structdef.capnp2.go: structdef.capnp2
2932
# go run zombiezen.com/go/capnproto2/capnpc-go@latest compile -I${GOPATH}/src -ogo structdef.capnp2
3033

@@ -79,3 +82,4 @@ install:
7982
go install github.com/200sc/bebop/main/bebopc-go@latest
8083
go install github.com/shamaton/msgpackgen@latest
8184
go install github.com/ymz-ncnk/musgo@latest
85+
go install github.com/cosmos/cosmos-proto/cmd/protoc-gen-go-pulsar@latest

README.md

Lines changed: 124 additions & 121 deletions
Large diffs are not rendered by default.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ require (
3838

3939
require (
4040
github.com/DataDog/zstd v1.5.2 // indirect
41+
github.com/cosmos/cosmos-proto v1.0.0-beta.3 // indirect
4142
github.com/cybriq/gotiny v0.0.5 // indirect
4243
github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31 // indirect
4344
github.com/glycerine/rbtree v0.0.0-20190406191118-ceb71889d809 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc
4040
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
4141
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
4242
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
43+
github.com/cosmos/cosmos-proto v1.0.0-beta.3 h1:VitvZ1lPORTVxkmF2fAp3IiA61xVwArQYKXTdEcpW6o=
44+
github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I=
4345
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
4446
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
4547
github.com/cybriq/gotiny v0.0.5 h1:VgEZDiDXaObPVhK9O+JNM6USguc+vUsz4dLMIMhuL7M=

serialization_benchmarks_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
vmihailenco "github.com/vmihailenco/msgpack/v5"
3535
"go.dedis.ch/protobuf"
3636
mongobson "go.mongodb.org/mongo-driver/bson"
37+
pproto "google.golang.org/protobuf/proto"
3738
"gopkg.in/mgo.v2/bson"
3839
capnp "zombiezen.com/go/capnproto2"
3940
)
@@ -867,6 +868,73 @@ func Benchmark_Goprotobuf_Unmarshal(b *testing.B) {
867868
}
868869
*/
869870

871+
// github.com/cosmos/cosmos-proto
872+
873+
func generatePulsar() []*PulsarBufA {
874+
a := make([]*PulsarBufA, 0, 1000)
875+
for i := 0; i < 1000; i++ {
876+
a = append(a, &PulsarBufA{
877+
Name: randString(16),
878+
BirthDay: time.Now().UnixNano(),
879+
Phone: randString(10),
880+
Siblings: rand.Int31n(5),
881+
Spouse: rand.Intn(2) == 1,
882+
Money: rand.Float64(),
883+
})
884+
}
885+
return a
886+
}
887+
888+
func Benchmark_Pulsar_Marshal(b *testing.B) {
889+
data := generatePulsar()
890+
b.ReportAllocs()
891+
b.ResetTimer()
892+
var serialSize int
893+
for i := 0; i < b.N; i++ {
894+
bytes, err := pproto.Marshal(data[rand.Intn(len(data))])
895+
if err != nil {
896+
b.Fatal(err)
897+
}
898+
serialSize += len(bytes)
899+
}
900+
b.ReportMetric(float64(serialSize)/float64(b.N), "B/serial")
901+
}
902+
903+
func Benchmark_Pulsar_Unmarshal(b *testing.B) {
904+
b.StopTimer()
905+
data := generatePulsar()
906+
ser := make([][]byte, len(data))
907+
var serialSize int
908+
for i, d := range data {
909+
var err error
910+
ser[i], err = pproto.Marshal(d)
911+
if err != nil {
912+
b.Fatal(err)
913+
}
914+
serialSize += len(ser[i])
915+
}
916+
b.ReportMetric(float64(serialSize)/float64(len(data)), "B/serial")
917+
b.ReportAllocs()
918+
b.StartTimer()
919+
920+
for i := 0; i < b.N; i++ {
921+
n := rand.Intn(len(ser))
922+
o := &PulsarBufA{}
923+
err := pproto.Unmarshal(ser[n], o)
924+
if err != nil {
925+
b.Fatalf("goprotobuf failed to unmarshal: %s (%s)", err, ser[n])
926+
}
927+
// Validate unmarshalled data.
928+
if validate != "" {
929+
i := data[n]
930+
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)
931+
if !correct {
932+
b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o)
933+
}
934+
}
935+
}
936+
}
937+
870938
// github.com/gogo/protobuf/proto
871939

872940
func generateGogoProto() []*GogoProtoBufA {

structdef-pulsar.proto

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
syntax = "proto3";
2+
3+
package goserbench;
4+
5+
option go_package = "github.com/alecthomas/go_serialization_benchmarks;goserbench";
6+
7+
message PulsarBufA {
8+
string name = 1;
9+
int64 birthDay = 2;
10+
string phone = 3;
11+
int32 siblings = 4;
12+
bool spouse = 5;
13+
double money = 6;
14+
}

0 commit comments

Comments
 (0)