This commit is contained in:
iamqizhao
2015-03-04 11:14:39 -08:00
4 changed files with 97 additions and 9 deletions

View File

@ -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
}

View File

@ -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)
}

View 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() {
}

View 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;
}