protobuf/benchmarks/go/go_benchmark_test.go

125 lines
3.3 KiB
Go
Raw Normal View History

2018-03-07 08:35:42 +00:00
package main
import (
benchmarkWrapper "../tmp"
googleMessage1Proto2 "../tmp/datasets/google_message1/proto2"
googleMessage1Proto3 "../tmp/datasets/google_message1/proto3"
googleMessage2 "../tmp/datasets/google_message2"
googleMessage3 "../tmp/datasets/google_message3"
googleMessage4 "../tmp/datasets/google_message4"
2018-03-13 08:18:20 +00:00
"flag"
"github.com/golang/protobuf/proto"
"io/ioutil"
"testing"
2018-03-07 08:35:42 +00:00
)
// Data is returned by the Load function.
2018-03-14 14:03:44 +00:00
type Dataset struct {
name string
newMessage func() proto.Message
marshaled [][]byte
2018-03-13 08:18:20 +00:00
unmarshaled []proto.Message
2018-03-07 08:35:42 +00:00
}
2018-03-14 14:03:44 +00:00
var datasets []Dataset
2018-03-07 08:35:42 +00:00
// This is used to getDefaultInstance for a message type.
2018-03-14 14:03:44 +00:00
func generateNewMessageFunction(dataset benchmarkWrapper.BenchmarkDataset) func() proto.Message {
2018-03-07 08:35:42 +00:00
switch dataset.MessageName {
case "benchmarks.proto3.GoogleMessage1":
2018-03-14 14:03:44 +00:00
return func() proto.Message { return new(googleMessage1Proto3.GoogleMessage1) }
2018-03-07 08:35:42 +00:00
case "benchmarks.proto2.GoogleMessage1":
2018-03-14 14:03:44 +00:00
return func() proto.Message { return new(googleMessage1Proto2.GoogleMessage1) }
2018-03-07 08:35:42 +00:00
case "benchmarks.proto2.GoogleMessage2":
2018-03-14 14:03:44 +00:00
return func() proto.Message { return new(googleMessage2.GoogleMessage2) }
2018-03-07 08:35:42 +00:00
case "benchmarks.google_message3.GoogleMessage3":
2018-03-14 14:03:44 +00:00
return func() proto.Message { return new(googleMessage3.GoogleMessage3) }
2018-03-07 08:35:42 +00:00
case "benchmarks.google_message4.GoogleMessage4":
2018-03-14 14:03:44 +00:00
return func() proto.Message { return new(googleMessage4.GoogleMessage4) }
2018-03-07 08:35:42 +00:00
default:
2018-03-14 14:03:44 +00:00
panic("Unknown message type: " + dataset.MessageName)
2018-03-07 08:35:42 +00:00
}
}
2018-03-14 14:03:44 +00:00
func init() {
2018-03-07 08:35:42 +00:00
flag.Parse()
2018-03-14 14:03:44 +00:00
for _, f := range flag.Args() {
// Load the benchmark.
b, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
2018-03-07 08:35:42 +00:00
}
2018-03-14 14:03:44 +00:00
// Parse the benchmark.
var dm benchmarkWrapper.BenchmarkDataset
if err := proto.Unmarshal(b, &dm); err != nil {
panic(err)
2018-03-07 08:35:42 +00:00
}
2018-03-14 14:03:44 +00:00
// Determine the concrete protobuf message type to use.
var ds Dataset
ds.newMessage = generateNewMessageFunction(dm)
// Unmarshal each test message.
for _, payload := range dm.Payload {
ds.marshaled = append(ds.marshaled, payload)
m := ds.newMessage()
if err := proto.Unmarshal(payload, m); err != nil {
panic(err)
}
ds.unmarshaled = append(ds.unmarshaled, m)
}
ds.name = f
2018-03-07 08:35:42 +00:00
2018-03-14 14:03:44 +00:00
datasets = append(datasets, ds)
2018-03-07 08:35:42 +00:00
}
}
2018-03-14 14:03:44 +00:00
func Benchmark(b *testing.B) {
for _, ds := range datasets {
b.Run(ds.name, func(b *testing.B) {
b.Run("Unmarshal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for j, payload := range ds.marshaled {
out := ds.newMessage()
if err := proto.Unmarshal(payload, out); err != nil {
b.Fatalf("can't unmarshal message %d %v", j, err)
}
2018-03-14 14:03:44 +00:00
}
}
})
b.Run("Marshal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for j, m := range ds.unmarshaled {
if _, err := proto.Marshal(m); err != nil {
b.Fatalf("can't marshal message %d %+v: %v", j, m, err)
}
2018-03-14 14:03:44 +00:00
}
}
})
b.Run("Size", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, m := range ds.unmarshaled {
proto.Size(m)
}
2018-03-14 14:03:44 +00:00
}
})
b.Run("Clone", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, m := range ds.unmarshaled {
proto.Clone(m)
}
2018-03-14 14:03:44 +00:00
}
})
b.Run("Merge", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, m := range ds.unmarshaled {
out := ds.newMessage()
proto.Merge(out, m)
}
2018-03-14 14:03:44 +00:00
}
})
})
2018-03-07 08:35:42 +00:00
}
}