From 2b4b14aaf026d08276531cf76f42de90efd3bc61 Mon Sep 17 00:00:00 2001 From: link1st <562117637@qq.com> Date: Thu, 4 Feb 2021 00:49:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=20grpc=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=8E=8B=E6=B5=8B=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 + go.sum | 77 ++++++++++++ model/request_model.go | 3 + proto/pb.pb.go | 229 +++++++++++++++++++++++++++++++++++ proto/pb.proto | 26 ++++ server/client/grpc_client.go | 66 ++++++++++ server/dispose.go | 9 ++ server/golink/grpc_link.go | 79 ++++++++++++ tests/grpc/main.go | 51 ++++++++ 9 files changed, 542 insertions(+) create mode 100644 proto/pb.pb.go create mode 100644 proto/pb.proto create mode 100644 server/client/grpc_client.go create mode 100644 server/golink/grpc_link.go create mode 100644 tests/grpc/main.go diff --git a/go.mod b/go.mod index 4a8c992..6150d8d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module go-stress-testing go 1.14 require ( + github.com/golang/protobuf v1.4.2 golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 golang.org/x/text v0.3.0 + google.golang.org/grpc v1.35.0 ) diff --git a/go.sum b/go.sum index bc41edb..ed4a46a 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,83 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.35.0 h1:TwIQcH3es+MojMVojxxfQ3l3OF2KzlRxML2xZq0kRo8= +google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/model/request_model.go b/model/request_model.go index 7b2a197..02aa87a 100644 --- a/model/request_model.go +++ b/model/request_model.go @@ -25,6 +25,7 @@ const ( FormTypeHttp = "http" FormTypeWebSocket = "webSocket" + FormTypeGRPC = "grpc" ) var ( @@ -157,6 +158,8 @@ func NewRequest(url string, verify string, timeout time.Duration, debug bool, pa form = FormTypeHttp } else if strings.HasPrefix(url, "ws://") || strings.HasPrefix(url, "wss://") { form = FormTypeWebSocket + } else if strings.HasPrefix(url, "grpc://") || strings.HasPrefix(url, "rpc://") { + form = FormTypeGRPC } else { form = FormTypeHttp url = fmt.Sprintf("http://%s", url) diff --git a/proto/pb.pb.go b/proto/pb.pb.go new file mode 100644 index 0000000..9d25eae --- /dev/null +++ b/proto/pb.pb.go @@ -0,0 +1,229 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: pb.proto + +package protobuf + +import ( + context "context" + fmt "fmt" + proto "github.com/golang/protobuf/proto" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + 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.ProtoPackageIsVersion3 // please upgrade the proto package + +// 请求 +type Request struct { + // UserName 用户昵称 + UserName string `protobuf:"bytes,1,opt,name=userName,proto3" json:"userName,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} +func (*Request) Descriptor() ([]byte, []int) { + return fileDescriptor_f80abaa17e25ccc8, []int{0} +} + +func (m *Request) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Request.Unmarshal(m, b) +} +func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Request.Marshal(b, m, deterministic) +} +func (m *Request) XXX_Merge(src proto.Message) { + xxx_messageInfo_Request.Merge(m, src) +} +func (m *Request) XXX_Size() int { + return xxx_messageInfo_Request.Size(m) +} +func (m *Request) XXX_DiscardUnknown() { + xxx_messageInfo_Request.DiscardUnknown(m) +} + +var xxx_messageInfo_Request proto.InternalMessageInfo + +func (m *Request) GetUserName() string { + if m != nil { + return m.UserName + } + return "" +} + +// 响应 +type Response struct { + // 状态码 + Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"` + // 状态码说明 + Msg string `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"` + // Data 返回数据 + Data string `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Response) Reset() { *m = Response{} } +func (m *Response) String() string { return proto.CompactTextString(m) } +func (*Response) ProtoMessage() {} +func (*Response) Descriptor() ([]byte, []int) { + return fileDescriptor_f80abaa17e25ccc8, []int{1} +} + +func (m *Response) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Response.Unmarshal(m, b) +} +func (m *Response) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Response.Marshal(b, m, deterministic) +} +func (m *Response) XXX_Merge(src proto.Message) { + xxx_messageInfo_Response.Merge(m, src) +} +func (m *Response) XXX_Size() int { + return xxx_messageInfo_Response.Size(m) +} +func (m *Response) XXX_DiscardUnknown() { + xxx_messageInfo_Response.DiscardUnknown(m) +} + +var xxx_messageInfo_Response proto.InternalMessageInfo + +func (m *Response) GetCode() int32 { + if m != nil { + return m.Code + } + return 0 +} + +func (m *Response) GetMsg() string { + if m != nil { + return m.Msg + } + return "" +} + +func (m *Response) GetData() string { + if m != nil { + return m.Data + } + return "" +} + +func init() { + proto.RegisterType((*Request)(nil), "protobuf.Request") + proto.RegisterType((*Response)(nil), "protobuf.Response") +} + +func init() { proto.RegisterFile("pb.proto", fileDescriptor_f80abaa17e25ccc8) } + +var fileDescriptor_f80abaa17e25ccc8 = []byte{ + // 170 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x28, 0x48, 0xd2, 0x2b, + 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x00, 0x53, 0x49, 0xa5, 0x69, 0x4a, 0xaa, 0x5c, 0xec, 0x41, + 0xa9, 0x85, 0xa5, 0xa9, 0xc5, 0x25, 0x42, 0x52, 0x5c, 0x1c, 0xa5, 0xc5, 0xa9, 0x45, 0x7e, 0x89, + 0xb9, 0xa9, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x70, 0xbe, 0x92, 0x0b, 0x17, 0x47, 0x50, + 0x6a, 0x71, 0x41, 0x7e, 0x5e, 0x71, 0xaa, 0x90, 0x10, 0x17, 0x4b, 0x72, 0x7e, 0x0a, 0x44, 0x0d, + 0x6b, 0x10, 0x98, 0x2d, 0x24, 0xc0, 0xc5, 0x9c, 0x5b, 0x9c, 0x2e, 0xc1, 0x04, 0xd6, 0x06, 0x62, + 0x82, 0x54, 0xa5, 0x24, 0x96, 0x24, 0x4a, 0x30, 0x83, 0x85, 0xc0, 0x6c, 0x23, 0x27, 0x2e, 0x4e, + 0xc7, 0x82, 0xcc, 0xe0, 0xd4, 0xa2, 0xb2, 0xd4, 0x22, 0x21, 0x53, 0x2e, 0x2e, 0x8f, 0xd4, 0x9c, + 0x9c, 0xfc, 0xf0, 0xfc, 0xa2, 0x9c, 0x14, 0x21, 0x41, 0x3d, 0x98, 0x93, 0xf4, 0xa0, 0xee, 0x91, + 0x12, 0x42, 0x16, 0x82, 0xd8, 0xad, 0xc4, 0x90, 0xc4, 0x06, 0x16, 0x34, 0x06, 0x04, 0x00, 0x00, + 0xff, 0xff, 0x72, 0xb6, 0x62, 0x67, 0xcd, 0x00, 0x00, 0x00, +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// ApiServerClient is the client API for ApiServer service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type ApiServerClient interface { + // 接口 + HelloWorld(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) +} + +type apiServerClient struct { + cc *grpc.ClientConn +} + +func NewApiServerClient(cc *grpc.ClientConn) ApiServerClient { + return &apiServerClient{cc} +} + +func (c *apiServerClient) HelloWorld(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) { + out := new(Response) + err := c.cc.Invoke(ctx, "/protobuf.ApiServer/HelloWorld", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// ApiServerServer is the server API for ApiServer service. +type ApiServerServer interface { + // 接口 + HelloWorld(context.Context, *Request) (*Response, error) +} + +// UnimplementedApiServerServer can be embedded to have forward compatible implementations. +type UnimplementedApiServerServer struct { +} + +func (*UnimplementedApiServerServer) HelloWorld(ctx context.Context, req *Request) (*Response, error) { + return nil, status.Errorf(codes.Unimplemented, "method HelloWorld not implemented") +} + +func RegisterApiServerServer(s *grpc.Server, srv ApiServerServer) { + s.RegisterService(&_ApiServer_serviceDesc, srv) +} + +func _ApiServer_HelloWorld_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Request) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ApiServerServer).HelloWorld(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/protobuf.ApiServer/HelloWorld", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ApiServerServer).HelloWorld(ctx, req.(*Request)) + } + return interceptor(ctx, in, info, handler) +} + +var _ApiServer_serviceDesc = grpc.ServiceDesc{ + ServiceName: "protobuf.ApiServer", + HandlerType: (*ApiServerServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "HelloWorld", + Handler: _ApiServer_HelloWorld_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "pb.proto", +} diff --git a/proto/pb.proto b/proto/pb.proto new file mode 100644 index 0000000..75198d6 --- /dev/null +++ b/proto/pb.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; + +package protobuf; + +// ApiServer api 接口 +service ApiServer { + // HelloWorld 接口 + rpc HelloWorld (Request) returns (Response) { + } +} + +// 请求 +message Request { + // UserName 用户昵称 + string userName = 1; +} + +// 响应 +message Response { + // 状态码 + int32 code = 1; + // 状态码说明 + string msg = 2; + // Data 返回数据 + string data = 3; +} \ No newline at end of file diff --git a/server/client/grpc_client.go b/server/client/grpc_client.go new file mode 100644 index 0000000..475d337 --- /dev/null +++ b/server/client/grpc_client.go @@ -0,0 +1,66 @@ +/** +* Created by GoLand. +* User: link1st +* Date: 2019-08-15 +* Time: 21:03 + */ + +package client + +import ( + "context" + "fmt" + "google.golang.org/grpc" + "strings" + "time" +) + +type GrpcSocket struct { + conn *grpc.ClientConn + address string +} + +func NewGrpcSocket(address string) (s *GrpcSocket) { + var ( + newAddr string + ) + arr := strings.Split(address, "//") + if len(arr) >= 2 { + newAddr = arr[1] + } + s = &GrpcSocket{ + address: newAddr, + } + return +} + +func (g *GrpcSocket) getAddress() (address string) { + return g.address +} + +// 关闭 +func (g *GrpcSocket) Close() (err error) { + if g == nil { + return + } + if g.conn == nil { + return + } + g.conn.Close() + return +} + +// Link 建立连接 +func (g *GrpcSocket) Link() (err error) { + ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) + conn, err := grpc.DialContext(ctx, g.address, grpc.WithInsecure(), grpc.WithBlock()) + if err != nil { + return fmt.Errorf("GetConn: 连接失败 address:%s %w", g.address, err) + } + g.conn = conn + return +} + +func (g *GrpcSocket) GetConn() (conn *grpc.ClientConn) { + return g.conn +} diff --git a/server/dispose.go b/server/dispose.go index ee28403..3d078a3 100644 --- a/server/dispose.go +++ b/server/dispose.go @@ -90,6 +90,15 @@ func Dispose(concurrency, totalNumber uint64, request *model.Request) { panic(data) } + case model.FormTypeGRPC: + // 连接以后再启动协程 + ws := client.NewGrpcSocket(request.Url) + err := ws.Link() + if err != nil { + fmt.Println("连接失败:", i, err) + continue + } + go golink.Grpc(i, ch, totalNumber, &wg, request, ws) default: // 类型不支持 wg.Done() diff --git a/server/golink/grpc_link.go b/server/golink/grpc_link.go new file mode 100644 index 0000000..a0de908 --- /dev/null +++ b/server/golink/grpc_link.go @@ -0,0 +1,79 @@ +/** +* Created by GoLand. +* User: link1st +* Date: 2019-08-21 +* Time: 15:43 + */ + +package golink + +import ( + "context" + pb "go-stress-testing/proto" + "sync" + "time" + + "go-stress-testing/heper" + "go-stress-testing/model" + "go-stress-testing/server/client" +) + +// Grpc grpc 接口请求 +func Grpc(chanId uint64, ch chan<- *model.RequestResults, totalNumber uint64, wg *sync.WaitGroup, request *model.Request, ws *client.GrpcSocket) { + + defer func() { + wg.Done() + }() + defer func() { + ws.Close() + }() + for i := uint64(0); i < totalNumber; i++ { + grpcRequest(chanId, ch, i, request, ws) + } + return +} + +// 请求 +func grpcRequest(chanId uint64, ch chan<- *model.RequestResults, i uint64, request *model.Request, ws *client.GrpcSocket) { + var ( + startTime = time.Now() + isSucceed = false + errCode = model.HttpOk + ) + + // 需要发送的数据 + conn := ws.GetConn() + if conn == nil { + errCode = model.RequestErr + } else { + // TODO::请求接口示例 + c := pb.NewApiServerClient(conn) + var ( + ctx = context.Background() + req = &pb.Request{ + UserName: request.Body, + } + ) + rsp, err := c.HelloWorld(ctx, req) + // fmt.Printf("rsp:%+v", rsp) + if err != nil { + errCode = model.RequestErr + } else { + // 200 为成功 + if rsp.Code != 200 { + errCode = model.RequestErr + } else { + isSucceed = true + } + } + } + requestTime := uint64(heper.DiffNano(startTime)) + requestResults := &model.RequestResults{ + Time: requestTime, + IsSucceed: isSucceed, + ErrCode: errCode, + } + requestResults.SetId(chanId, i) + + ch <- requestResults +} diff --git a/tests/grpc/main.go b/tests/grpc/main.go new file mode 100644 index 0000000..c144fb9 --- /dev/null +++ b/tests/grpc/main.go @@ -0,0 +1,51 @@ +/** +* Created by GoLand. +* User: link1st +* Date: 2021/2/3 +* Time: 23:44 + */ + +package main + +import ( + "context" + "fmt" + pb "go-stress-testing/proto" + "google.golang.org/grpc" + "log" + "net" +) + +const ( + // port 监听端口 + port = ":8099" +) + +// server is used to implement helloWorld.GreeterServer. +type server struct { + pb.UnimplementedApiServerServer +} + +// HelloWorld hello world 接口 +func (s *server) HelloWorld(_ context.Context, req *pb.Request) (rsp *pb.Response, err error) { + rsp = &pb.Response{ + Code: 200, + Msg: "success", + Data: fmt.Sprintf("hello %s !", req.UserName), + } + return +} + +// main 主函数 +func main() { + fmt.Println("trpc server 启动中...") + lis, err := net.Listen("tcp", port) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + s := grpc.NewServer() + pb.RegisterApiServerServer(s, &server{}) + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +}