pull/4361/head
BSBandme 7 years ago
parent 4e3c413493
commit 6d2c6a0099
  1. 15
      benchmarks/README.md
  2. 56
      benchmarks/go_benchmark_test.go

@ -37,17 +37,20 @@ $ sudo apt-get install python3-dev
And you also need to make sure `pkg-config` is installed. And you also need to make sure `pkg-config` is installed.
### Go ### Go
Golang protobuf is under [golang repo](https://github.com/golang/protobuf), you Go protobufs are maintained at [github.com/golang/protobuf](
need to install golang and golang protobuf's proto and protoc-gen-go before http://github.com/golang/protobuf). If not done already, you need to install the
running the benchmark. And we're using [testing package](https://golang.org/pkg/testing/) toolchain and the Go protoc-gen-go plugin for protoc.
to test benchmark.
To install protoc-gen-go, run:
To install golang protobuf proto and protoc-gen-go, you can
``` ```
$ go get -u github.com/golang/protobuf/protoc-gen-go $ go get -u github.com/golang/protobuf/protoc-gen-go
$ go get -u github.com/golang/protobuf/proto $ export PATH=$PATH:$(go env GOPATH)/bin
``` ```
The first command installs `protoc-gen-go` into the `bin` directory in your local `GOPATH`.
The second command adds the `bin` directory to your `PATH` so that `protoc` can locate the plugin later.
### Big data ### Big data
There's some optional big testing data which is not included in the directory There's some optional big testing data which is not included in the directory

@ -1,31 +1,29 @@
package main package main
import ( import (
"errors"
"io/ioutil"
"flag"
"testing"
"os"
benchmarkWrapper "./tmp" benchmarkWrapper "./tmp"
proto "github.com/golang/protobuf/proto"
googleMessage1Proto3 "./tmp/datasets/google_message1/proto3"
googleMessage1Proto2 "./tmp/datasets/google_message1/proto2" googleMessage1Proto2 "./tmp/datasets/google_message1/proto2"
googleMessage1Proto3 "./tmp/datasets/google_message1/proto3"
googleMessage2 "./tmp/datasets/google_message2" googleMessage2 "./tmp/datasets/google_message2"
googleMessage3 "./tmp/datasets/google_message3" googleMessage3 "./tmp/datasets/google_message3"
googleMessage4 "./tmp/datasets/google_message4" googleMessage4 "./tmp/datasets/google_message4"
"errors"
"flag"
"github.com/golang/protobuf/proto"
"io/ioutil"
"os"
"testing"
) )
// Data is returned by the Load function. // Data is returned by the Load function.
type Data struct { type Data struct {
// Marshalled is a slice of marshalled protocol // marshaled is a slice of marshaled protocol
// buffers. 1:1 with Unmarshalled. // buffers. 1:1 with unmarshaled.
Marshalled [][]byte marshaled [][]byte
// Unmarshalled is a slice of unmarshalled protocol // Unmarshaled is a slice of unmarshaled protocol
// buffers. 1:1 with Marshalled. // buffers. 1:1 with marshaled.
Unmarshalled []proto.Message unmarshaled []proto.Message
count int count int
} }
@ -34,6 +32,7 @@ var data *Data
var counter int var counter int
type GetDefaultInstanceFunction func() proto.Message type GetDefaultInstanceFunction func() proto.Message
var getDefaultInstance GetDefaultInstanceFunction var getDefaultInstance GetDefaultInstanceFunction
// This is used to getDefaultInstance for a message type. // This is used to getDefaultInstance for a message type.
@ -62,25 +61,25 @@ func generateGetDefaltInstanceFunction(dataset benchmarkWrapper.BenchmarkDataset
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
flag.Parse() flag.Parse()
data = new(Data) data = new(Data)
rawData, error := ioutil.ReadFile(flag.Arg(0)) rawData, err := ioutil.ReadFile(flag.Arg(0))
if error != nil { if err != nil {
panic("Couldn't find file" + flag.Arg(0)) panic("Couldn't find file" + flag.Arg(0))
} }
var dataset benchmarkWrapper.BenchmarkDataset var dataset benchmarkWrapper.BenchmarkDataset
if err1 := proto.Unmarshal(rawData, &dataset); err1 != nil { if err = proto.Unmarshal(rawData, &dataset); err != nil {
panic("The raw input data can't be parse into BenchmarkDataset message.") panic("The raw input data can't be parse into BenchmarkDataset message.")
} }
generateGetDefaltInstanceFunction(dataset) generateGetDefaltInstanceFunction(dataset)
for _, payload := range dataset.Payload { for _, payload := range dataset.Payload {
data.Marshalled = append(data.Marshalled, payload) data.marshaled = append(data.marshaled, payload)
m := getDefaultInstance() m := getDefaultInstance()
proto.Unmarshal(payload, m) proto.Unmarshal(payload, m)
data.Unmarshalled = append(data.Unmarshalled, m) data.unmarshaled = append(data.unmarshaled, m)
} }
data.count = len(data.Unmarshalled) data.count = len(data.unmarshaled)
os.Exit(m.Run()) os.Exit(m.Run())
} }
@ -88,10 +87,10 @@ func TestMain(m *testing.M) {
func BenchmarkUnmarshal(b *testing.B) { func BenchmarkUnmarshal(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
payload := data.Marshalled[counter % data.count] payload := data.marshaled[counter%data.count]
out := getDefaultInstance() out := getDefaultInstance()
if err := proto.Unmarshal(payload, out); err != nil { if err := proto.Unmarshal(payload, out); err != nil {
b.Fatalf("can't unmarshal message %d %v", counter % data.count, err) b.Fatalf("can't unmarshal message %d %v", counter%data.count, err)
} }
counter++ counter++
} }
@ -100,9 +99,9 @@ func BenchmarkUnmarshal(b *testing.B) {
func BenchmarkMarshal(b *testing.B) { func BenchmarkMarshal(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
m := data.Unmarshalled[counter % data.count] m := data.unmarshaled[counter%data.count]
if _, err := proto.Marshal(m); err != nil { if _, err := proto.Marshal(m); err != nil {
b.Fatalf("can't marshal message %d %+v: %v", counter % data.count, m, err) b.Fatalf("can't marshal message %d %+v: %v", counter%data.count, m, err)
} }
counter++ counter++
} }
@ -111,7 +110,7 @@ func BenchmarkMarshal(b *testing.B) {
func BenchmarkSize(b *testing.B) { func BenchmarkSize(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
proto.Size(data.Unmarshalled[counter % data.count]) proto.Size(data.unmarshaled[counter%data.count])
counter++ counter++
} }
} }
@ -119,7 +118,7 @@ func BenchmarkSize(b *testing.B) {
func BenchmarkClone(b *testing.B) { func BenchmarkClone(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
proto.Clone(data.Unmarshalled[counter % data.count]) proto.Clone(data.unmarshaled[counter%data.count])
counter++ counter++
} }
} }
@ -128,8 +127,7 @@ func BenchmarkMerge(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
out := getDefaultInstance() out := getDefaultInstance()
proto.Merge(out, data.Unmarshalled[counter % data.count]) proto.Merge(out, data.unmarshaled[counter%data.count])
counter++ counter++
} }
} }

Loading…
Cancel
Save