Merge branch 'master' of https://github.com/grpc/grpc-go
This commit is contained in:
14
rpc_util.go
14
rpc_util.go
@ -134,9 +134,7 @@ func (p *parser) recvMsg() (pf payloadFormat, msg []byte, err error) {
|
||||
func encode(msg proto.Message, pf payloadFormat) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
// Write message fixed header.
|
||||
if err := buf.WriteByte(uint8(pf)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf.WriteByte(uint8(pf))
|
||||
var b []byte
|
||||
var length uint32
|
||||
if msg != nil {
|
||||
@ -148,12 +146,10 @@ func encode(msg proto.Message, pf payloadFormat) ([]byte, error) {
|
||||
}
|
||||
length = uint32(len(b))
|
||||
}
|
||||
if err := binary.Write(&buf, binary.BigEndian, length); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := buf.Write(b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var szHdr [4]byte
|
||||
binary.BigEndian.PutUint32(szHdr[:], length)
|
||||
buf.Write(szHdr[:])
|
||||
buf.Write(b)
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
|
@ -44,6 +44,7 @@ import (
|
||||
"github.com/golang/protobuf/proto"
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc/codes"
|
||||
perfpb "google.golang.org/grpc/test/codec_perf"
|
||||
"google.golang.org/grpc/transport"
|
||||
)
|
||||
|
||||
@ -170,3 +171,41 @@ func TestBackoff(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// bmEncode benchmarks encoding a Protocol Buffer message containing mSize
|
||||
// bytes.
|
||||
func bmEncode(b *testing.B, mSize int) {
|
||||
msg := &perfpb.Buffer{Body: make([]byte, mSize)}
|
||||
encoded, _ := encode(msg, compressionNone)
|
||||
encodedSz := int64(len(encoded))
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
encode(msg, compressionNone)
|
||||
}
|
||||
b.SetBytes(encodedSz)
|
||||
}
|
||||
|
||||
func BenchmarkEncode1B(b *testing.B) {
|
||||
bmEncode(b, 1)
|
||||
}
|
||||
|
||||
func BenchmarkEncode1KiB(b *testing.B) {
|
||||
bmEncode(b, 1024)
|
||||
}
|
||||
|
||||
func BenchmarkEncode8KiB(b *testing.B) {
|
||||
bmEncode(b, 8*1024)
|
||||
}
|
||||
|
||||
func BenchmarkEncode64KiB(b *testing.B) {
|
||||
bmEncode(b, 64*1024)
|
||||
}
|
||||
|
||||
func BenchmarkEncode512KiB(b *testing.B) {
|
||||
bmEncode(b, 512*1024)
|
||||
}
|
||||
|
||||
func BenchmarkEncode1MiB(b *testing.B) {
|
||||
bmEncode(b, 1024*1024)
|
||||
}
|
||||
|
42
test/codec_perf/perf.pb.go
Normal file
42
test/codec_perf/perf.pb.go
Normal file
@ -0,0 +1,42 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: perf.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package codec_perf is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
perf.proto
|
||||
|
||||
It has these top-level messages:
|
||||
Buffer
|
||||
*/
|
||||
package codec_perf
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import math "math"
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = math.Inf
|
||||
|
||||
// Buffer is a message that contains a body of bytes that is used to exercise
|
||||
// encoding and decoding overheads.
|
||||
type Buffer struct {
|
||||
Body []byte `protobuf:"bytes,1,opt,name=body" json:"body,omitempty"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Buffer) Reset() { *m = Buffer{} }
|
||||
func (m *Buffer) String() string { return proto.CompactTextString(m) }
|
||||
func (*Buffer) ProtoMessage() {}
|
||||
|
||||
func (m *Buffer) GetBody() []byte {
|
||||
if m != nil {
|
||||
return m.Body
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
}
|
11
test/codec_perf/perf.proto
Normal file
11
test/codec_perf/perf.proto
Normal file
@ -0,0 +1,11 @@
|
||||
// Messages used for performance tests that may not reference grpc directly for
|
||||
// reasons of import cycles.
|
||||
syntax = "proto2";
|
||||
|
||||
package codec.perf;
|
||||
|
||||
// Buffer is a message that contains a body of bytes that is used to exercise
|
||||
// encoding and decoding overheads.
|
||||
message Buffer {
|
||||
optional bytes body = 1;
|
||||
}
|
Reference in New Issue
Block a user