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.
### Go
Golang protobuf is under [golang repo](https://github.com/golang/protobuf), you
need to install golang and golang protobuf's proto and protoc-gen-go before
running the benchmark. And we're using [testing package](https://golang.org/pkg/testing/)
to test benchmark.
Go protobufs are maintained at [github.com/golang/protobuf](
http://github.com/golang/protobuf). If not done already, you need to install the
toolchain and the Go protoc-gen-go plugin for protoc.
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/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
There's some optional big testing data which is not included in the directory

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

Loading…
Cancel
Save