route/vendor/google.golang.org/grpc/codec_benchmark_test.go

100 lines
2.8 KiB
Go
Raw Normal View History

2017-10-06 15:29:20 +00:00
// +build go1.7
/*
*
* Copyright 2014 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package grpc
import (
"fmt"
"testing"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc/test/codec_perf"
)
2018-01-03 19:19:49 +00:00
func setupBenchmarkProtoCodecInputs(payloadBaseSize uint32) []proto.Message {
2017-10-06 15:29:20 +00:00
payloadBase := make([]byte, payloadBaseSize)
// arbitrary byte slices
payloadSuffixes := [][]byte{
[]byte("one"),
[]byte("two"),
[]byte("three"),
[]byte("four"),
[]byte("five"),
}
protoStructs := make([]proto.Message, 0)
for _, p := range payloadSuffixes {
ps := &codec_perf.Buffer{}
ps.Body = append(payloadBase, p...)
protoStructs = append(protoStructs, ps)
}
return protoStructs
}
// The possible use of certain protobuf APIs like the proto.Buffer API potentially involves caching
// on our side. This can add checks around memory allocations and possible contention.
// Example run: go test -v -run=^$ -bench=BenchmarkProtoCodec -benchmem
func BenchmarkProtoCodec(b *testing.B) {
// range of message sizes
payloadBaseSizes := make([]uint32, 0)
for i := uint32(0); i <= 12; i += 4 {
payloadBaseSizes = append(payloadBaseSizes, 1<<i)
}
// range of SetParallelism
2018-01-03 19:19:49 +00:00
parallelisms := make([]int, 0)
2017-10-06 15:29:20 +00:00
for i := uint32(0); i <= 16; i += 4 {
2018-01-03 19:19:49 +00:00
parallelisms = append(parallelisms, int(1<<i))
2017-10-06 15:29:20 +00:00
}
for _, s := range payloadBaseSizes {
for _, p := range parallelisms {
2018-01-03 19:19:49 +00:00
protoStructs := setupBenchmarkProtoCodecInputs(s)
name := fmt.Sprintf("MinPayloadSize:%v/SetParallelism(%v)", s, p)
b.Run(name, func(b *testing.B) {
codec := &protoCodec{}
b.SetParallelism(p)
b.RunParallel(func(pb *testing.PB) {
benchmarkProtoCodec(codec, protoStructs, pb, b)
2017-10-06 15:29:20 +00:00
})
2018-01-03 19:19:49 +00:00
})
2017-10-06 15:29:20 +00:00
}
}
}
func benchmarkProtoCodec(codec *protoCodec, protoStructs []proto.Message, pb *testing.PB, b *testing.B) {
counter := 0
for pb.Next() {
counter++
ps := protoStructs[counter%len(protoStructs)]
fastMarshalAndUnmarshal(codec, ps, b)
}
}
func fastMarshalAndUnmarshal(protoCodec Codec, protoStruct proto.Message, b *testing.B) {
marshaledBytes, err := protoCodec.Marshal(protoStruct)
if err != nil {
b.Errorf("protoCodec.Marshal(_) returned an error")
}
2018-01-03 19:19:49 +00:00
res := codec_perf.Buffer{}
if err := protoCodec.Unmarshal(marshaledBytes, &res); err != nil {
2017-10-06 15:29:20 +00:00
b.Errorf("protoCodec.Unmarshal(_) returned an error")
}
}