Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.3 KiB

7 years ago
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"
7 years ago
"flag"
"github.com/golang/protobuf/proto"
"io/ioutil"
"testing"
7 years ago
)
// Data is returned by the Load function.
type Dataset struct {
name string
newMessage func() proto.Message
marshaled [][]byte
7 years ago
unmarshaled []proto.Message
7 years ago
}
var datasets []Dataset
7 years ago
// This is used to getDefaultInstance for a message type.
func generateNewMessageFunction(dataset benchmarkWrapper.BenchmarkDataset) func() proto.Message {
7 years ago
switch dataset.MessageName {
case "benchmarks.proto3.GoogleMessage1":
return func() proto.Message { return new(googleMessage1Proto3.GoogleMessage1) }
7 years ago
case "benchmarks.proto2.GoogleMessage1":
return func() proto.Message { return new(googleMessage1Proto2.GoogleMessage1) }
7 years ago
case "benchmarks.proto2.GoogleMessage2":
return func() proto.Message { return new(googleMessage2.GoogleMessage2) }
7 years ago
case "benchmarks.google_message3.GoogleMessage3":
return func() proto.Message { return new(googleMessage3.GoogleMessage3) }
7 years ago
case "benchmarks.google_message4.GoogleMessage4":
return func() proto.Message { return new(googleMessage4.GoogleMessage4) }
7 years ago
default:
panic("Unknown message type: " + dataset.MessageName)
7 years ago
}
}
func init() {
7 years ago
flag.Parse()
for _, f := range flag.Args() {
// Load the benchmark.
b, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
7 years ago
}
// Parse the benchmark.
var dm benchmarkWrapper.BenchmarkDataset
if err := proto.Unmarshal(b, &dm); err != nil {
panic(err)
7 years ago
}
// 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
7 years ago
datasets = append(datasets, ds)
7 years ago
}
}
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)
}
}
}
})
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)
}
}
}
})
b.Run("Size", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, m := range ds.unmarshaled {
proto.Size(m)
}
}
})
b.Run("Clone", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, m := range ds.unmarshaled {
proto.Clone(m)
}
}
})
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)
}
}
})
})
7 years ago
}
}