Skip to content

Instantly share code, notes, and snippets.

@prestonvanloon
Created March 16, 2019 16:26
Show Gist options
  • Save prestonvanloon/6663510164f967fa05553ead157cd5c1 to your computer and use it in GitHub Desktop.
Save prestonvanloon/6663510164f967fa05553ead157cd5c1 to your computer and use it in GitHub Desktop.
Testing out hobbits
package example
import (
"testing"
proto "github.com/golang/protobuf/proto"
)
func BenchmarkHobbits(b *testing.B) {
example_ping_req := "EWP 0.1 PING none none 0 5\n12345"
for i := 0; i < b.N; i++ {
_ = parse(example_ping_req)
}
}
func BenchmarkProtobuf(b *testing.B) {
req := &RequestPB{
Proto: "proto",
Version: "version",
Command: "command",
Compression: "compression",
ResponseCompression: []string{"response", "compression"},
HeadOnlyIndicator: false,
Headers: []byte("headers"),
Body: []byte("body"),
}
data, err := proto.Marshal(req)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
err := proto.Unmarshal(data, req)
if err != nil {
b.Fatal(err)
}
}
}
package example
import (
"fmt"
"strconv"
"strings"
)
type Request struct {
proto string
version string
command string
compression string
responseCompression []string
headOnlyIndicator bool
headers []byte
body []byte
}
func parse(req string) Request {
res := strings.Split(req, "\n")
reqLine := res[0]
payload := res[1]
r := strings.Split(reqLine, " ")
if len(r) < 8 {
r = append(r, " ")
}
headersLen, _ := strconv.Atoi(r[5])
bodyLen, _ := strconv.Atoi(r[6])
headers := payload[0:headersLen]
body := payload[headersLen:bodyLen]
request := Request{
proto: r[0],
version: r[1],
command: r[2],
compression: r[3],
responseCompression: strings.Split(r[4], ","),
headOnlyIndicator: r[7] == "H",
headers: []byte(headers),
body: []byte(body),
}
return request
}
func main() {
example_ping_req := "EWP 0.1 PING none none 0 5\n12345"
fmt.Println(parse(example_ping_req))
}
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: request.proto
package example
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type RequestPB struct {
Proto string `protobuf:"bytes,1,opt,name=proto,proto3" json:"proto,omitempty"`
Version string `protobuf:"bytes,2,opt,name=version,proto3" json:"version,omitempty"`
Command string `protobuf:"bytes,3,opt,name=command,proto3" json:"command,omitempty"`
Compression string `protobuf:"bytes,4,opt,name=compression,proto3" json:"compression,omitempty"`
ResponseCompression []string `protobuf:"bytes,5,rep,name=responseCompression,proto3" json:"responseCompression,omitempty"`
HeadOnlyIndicator bool `protobuf:"varint,6,opt,name=headOnlyIndicator,proto3" json:"headOnlyIndicator,omitempty"`
Headers []byte `protobuf:"bytes,7,opt,name=headers,proto3" json:"headers,omitempty"`
Body []byte `protobuf:"bytes,8,opt,name=body,proto3" json:"body,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *RequestPB) Reset() { *m = RequestPB{} }
func (m *RequestPB) String() string { return proto.CompactTextString(m) }
func (*RequestPB) ProtoMessage() {}
func (*RequestPB) Descriptor() ([]byte, []int) {
return fileDescriptor_request_934884e457efc9fa, []int{0}
}
func (m *RequestPB) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_RequestPB.Unmarshal(m, b)
}
func (m *RequestPB) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_RequestPB.Marshal(b, m, deterministic)
}
func (dst *RequestPB) XXX_Merge(src proto.Message) {
xxx_messageInfo_RequestPB.Merge(dst, src)
}
func (m *RequestPB) XXX_Size() int {
return xxx_messageInfo_RequestPB.Size(m)
}
func (m *RequestPB) XXX_DiscardUnknown() {
xxx_messageInfo_RequestPB.DiscardUnknown(m)
}
var xxx_messageInfo_RequestPB proto.InternalMessageInfo
func (m *RequestPB) GetProto() string {
if m != nil {
return m.Proto
}
return ""
}
func (m *RequestPB) GetVersion() string {
if m != nil {
return m.Version
}
return ""
}
func (m *RequestPB) GetCommand() string {
if m != nil {
return m.Command
}
return ""
}
func (m *RequestPB) GetCompression() string {
if m != nil {
return m.Compression
}
return ""
}
func (m *RequestPB) GetResponseCompression() []string {
if m != nil {
return m.ResponseCompression
}
return nil
}
func (m *RequestPB) GetHeadOnlyIndicator() bool {
if m != nil {
return m.HeadOnlyIndicator
}
return false
}
func (m *RequestPB) GetHeaders() []byte {
if m != nil {
return m.Headers
}
return nil
}
func (m *RequestPB) GetBody() []byte {
if m != nil {
return m.Body
}
return nil
}
func init() {
proto.RegisterType((*RequestPB)(nil), "example.RequestPB")
}
func init() { proto.RegisterFile("request.proto", fileDescriptor_request_934884e457efc9fa) }
var fileDescriptor_request_934884e457efc9fa = []byte{
// 204 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x8f, 0x4d, 0x4a, 0xc6, 0x30,
0x10, 0x40, 0xc9, 0xf7, 0xd7, 0x76, 0xd4, 0x85, 0xa3, 0x8b, 0x2c, 0x83, 0xab, 0x2c, 0x44, 0x04,
0x6f, 0xa0, 0x2b, 0x57, 0x4a, 0x6e, 0x90, 0x36, 0x03, 0x16, 0x9a, 0x4c, 0x4c, 0xaa, 0xd8, 0x03,
0x78, 0x6f, 0x69, 0x6a, 0xa1, 0xa0, 0xbb, 0xbc, 0xf7, 0x32, 0x30, 0x03, 0x17, 0x89, 0xde, 0x3f,
0x28, 0x8f, 0x77, 0x31, 0xf1, 0xc8, 0x58, 0xd1, 0x97, 0xf5, 0x71, 0xa0, 0x9b, 0xef, 0x1d, 0x34,
0x66, 0x49, 0xaf, 0x8f, 0x78, 0x0d, 0xc7, 0xd2, 0xa5, 0x50, 0x42, 0x37, 0x66, 0x01, 0x94, 0x50,
0x7d, 0x52, 0xca, 0x3d, 0x07, 0xb9, 0x2b, 0x7e, 0xc5, 0xb9, 0x74, 0xec, 0xbd, 0x0d, 0x4e, 0xee,
0x97, 0xf2, 0x8b, 0xa8, 0xe0, 0xac, 0x63, 0x1f, 0x13, 0xe5, 0x32, 0x77, 0x28, 0x75, 0xab, 0xf0,
0x1e, 0xae, 0x12, 0xe5, 0xc8, 0x21, 0xd3, 0xd3, 0xe6, 0xe7, 0x51, 0xed, 0x75, 0x63, 0xfe, 0x4b,
0x78, 0x0b, 0x97, 0x6f, 0x64, 0xdd, 0x4b, 0x18, 0xa6, 0xe7, 0xe0, 0xfa, 0xce, 0x8e, 0x9c, 0xe4,
0x49, 0x09, 0x5d, 0x9b, 0xbf, 0x61, 0xde, 0x6d, 0x96, 0x94, 0xb2, 0xac, 0x94, 0xd0, 0xe7, 0x66,
0x45, 0x44, 0x38, 0xb4, 0xec, 0x26, 0x59, 0x17, 0x5d, 0xde, 0xed, 0xa9, 0x9c, 0xfa, 0xf0, 0x13,
0x00, 0x00, 0xff, 0xff, 0x60, 0xe3, 0x40, 0x85, 0x28, 0x01, 0x00, 0x00,
}
syntax ="proto3";
package example;
message RequestPB {
string proto = 1;
string version = 2;
string command = 3;
string compression = 4;
repeated string responseCompression = 5;
bool headOnlyIndicator = 6;
bytes headers = 7;
bytes body = 8;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment