grpc 测试开始

This commit is contained in:
huangsimin 2020-03-04 18:09:19 +08:00
commit f27ac05f9a
8 changed files with 432 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
edb
main
*.log
log

13
api.go Normal file
View File

@ -0,0 +1,13 @@
package main
import (
context "context"
)
// server is used to implement
type server struct{}
// SayHello implements
func (s *server) CreateTable(ctx context.Context, in *TableRequest) (*TableReply, error) {
return &TableReply{Msg: "Hello " + in.Name}, nil
}

209
api.pb.go Normal file
View File

@ -0,0 +1,209 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// source: api.proto
package main
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 TableRequest struct {
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *TableRequest) Reset() { *m = TableRequest{} }
func (m *TableRequest) String() string { return proto.CompactTextString(m) }
func (*TableRequest) ProtoMessage() {}
func (*TableRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_00212fb1f9d3bf1c, []int{0}
}
func (m *TableRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_TableRequest.Unmarshal(m, b)
}
func (m *TableRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_TableRequest.Marshal(b, m, deterministic)
}
func (m *TableRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_TableRequest.Merge(m, src)
}
func (m *TableRequest) XXX_Size() int {
return xxx_messageInfo_TableRequest.Size(m)
}
func (m *TableRequest) XXX_DiscardUnknown() {
xxx_messageInfo_TableRequest.DiscardUnknown(m)
}
var xxx_messageInfo_TableRequest proto.InternalMessageInfo
func (m *TableRequest) GetName() string {
if m != nil {
return m.Name
}
return ""
}
// 服务端响应信息
type TableReply struct {
Msg string `protobuf:"bytes,1,opt,name=msg,proto3" json:"msg,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *TableReply) Reset() { *m = TableReply{} }
func (m *TableReply) String() string { return proto.CompactTextString(m) }
func (*TableReply) ProtoMessage() {}
func (*TableReply) Descriptor() ([]byte, []int) {
return fileDescriptor_00212fb1f9d3bf1c, []int{1}
}
func (m *TableReply) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_TableReply.Unmarshal(m, b)
}
func (m *TableReply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_TableReply.Marshal(b, m, deterministic)
}
func (m *TableReply) XXX_Merge(src proto.Message) {
xxx_messageInfo_TableReply.Merge(m, src)
}
func (m *TableReply) XXX_Size() int {
return xxx_messageInfo_TableReply.Size(m)
}
func (m *TableReply) XXX_DiscardUnknown() {
xxx_messageInfo_TableReply.DiscardUnknown(m)
}
var xxx_messageInfo_TableReply proto.InternalMessageInfo
func (m *TableReply) GetMsg() string {
if m != nil {
return m.Msg
}
return ""
}
func init() {
proto.RegisterType((*TableRequest)(nil), "main.TableRequest")
proto.RegisterType((*TableReply)(nil), "main.TableReply")
}
func init() {
proto.RegisterFile("api.proto", fileDescriptor_00212fb1f9d3bf1c)
}
var fileDescriptor_00212fb1f9d3bf1c = []byte{
// 140 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4c, 0x2c, 0xc8, 0xd4,
0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xc9, 0x4d, 0xcc, 0xcc, 0x53, 0x52, 0xe2, 0xe2, 0x09,
0x49, 0x4c, 0xca, 0x49, 0x0d, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x11, 0x12, 0xe2, 0x62, 0xc9,
0x4b, 0xcc, 0x4d, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x02, 0xb3, 0x95, 0xe4, 0xb8, 0xb8,
0xa0, 0x6a, 0x0a, 0x72, 0x2a, 0x85, 0x04, 0xb8, 0x98, 0x73, 0x8b, 0xd3, 0xa1, 0x0a, 0x40, 0x4c,
0x23, 0x47, 0x2e, 0x0e, 0xd7, 0xc4, 0xe2, 0x4a, 0x97, 0xc4, 0x92, 0x44, 0x21, 0x53, 0x2e, 0x6e,
0xe7, 0xa2, 0xd4, 0xc4, 0x92, 0x54, 0xb0, 0x0e, 0x21, 0x21, 0x3d, 0x90, 0x2d, 0x7a, 0xc8, 0x56,
0x48, 0x09, 0xa0, 0x88, 0x15, 0xe4, 0x54, 0x2a, 0x31, 0x24, 0xb1, 0x81, 0xdd, 0x64, 0x0c, 0x08,
0x00, 0x00, 0xff, 0xff, 0x2d, 0x8a, 0xa9, 0x47, 0xa0, 0x00, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConnInterface
// 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.SupportPackageIsVersion6
// EasyDataClient is the client API for EasyData service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type EasyDataClient interface {
// 服务端返馈信息方法
CreateTable(ctx context.Context, in *TableRequest, opts ...grpc.CallOption) (*TableReply, error)
}
type easyDataClient struct {
cc grpc.ClientConnInterface
}
func NewEasyDataClient(cc grpc.ClientConnInterface) EasyDataClient {
return &easyDataClient{cc}
}
func (c *easyDataClient) CreateTable(ctx context.Context, in *TableRequest, opts ...grpc.CallOption) (*TableReply, error) {
out := new(TableReply)
err := c.cc.Invoke(ctx, "/main.EasyData/CreateTable", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// EasyDataServer is the server API for EasyData service.
type EasyDataServer interface {
// 服务端返馈信息方法
CreateTable(context.Context, *TableRequest) (*TableReply, error)
}
// UnimplementedEasyDataServer can be embedded to have forward compatible implementations.
type UnimplementedEasyDataServer struct {
}
func (*UnimplementedEasyDataServer) CreateTable(ctx context.Context, req *TableRequest) (*TableReply, error) {
return nil, status.Errorf(codes.Unimplemented, "method CreateTable not implemented")
}
func RegisterEasyDataServer(s *grpc.Server, srv EasyDataServer) {
s.RegisterService(&_EasyData_serviceDesc, srv)
}
func _EasyData_CreateTable_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(TableRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(EasyDataServer).CreateTable(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/main.EasyData/CreateTable",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(EasyDataServer).CreateTable(ctx, req.(*TableRequest))
}
return interceptor(ctx, in, info, handler)
}
var _EasyData_serviceDesc = grpc.ServiceDesc{
ServiceName: "main.EasyData",
HandlerType: (*EasyDataServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "CreateTable",
Handler: _EasyData_CreateTable_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "api.proto",
}

16
api.proto Normal file
View File

@ -0,0 +1,16 @@
syntax = "proto3";
package main;
//
service EasyData {
//
rpc CreateTable(TableRequest) returns (TableReply) {}
}
//
message TableRequest {
string name = 1;
}
//
message TableReply {
string msg = 1;
}

87
api_test.go Normal file
View File

@ -0,0 +1,87 @@
package main
import (
"context"
"log"
"net"
"runtime"
"sync"
"testing"
"time"
grpc "google.golang.org/grpc"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
// createServer()
}
func createServer() {
//起服务
listen, err := net.Listen("tcp", "localhost:"+port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
gserver := grpc.NewServer()
RegisterEasyDataServer(gserver, &server{})
go gserver.Serve(listen)
time.Sleep(time.Millisecond * 50)
}
func ClientCall(wg *sync.WaitGroup, COUNT int) {
defer wg.Done()
conn, err := grpc.Dial("localhost:"+port, grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()
client := NewEasyDataClient(conn)
for i := 0; i < COUNT; i++ {
reply, err := client.CreateTable(context.Background(), &TableRequest{Name: "user"})
if err == nil {
reply.GetMsg()
}
}
}
func TestSayHello(t *testing.T) {
now := time.Now().Unix()
wg := &sync.WaitGroup{}
GCOUNT := 1000
COUNT := 100
wg.Add(GCOUNT)
for i := 0; i < GCOUNT; i++ {
go ClientCall(wg, COUNT)
}
wg.Wait()
t.Error(int64(COUNT*GCOUNT) / (time.Now().Unix() - now))
}
func BenchmarkCreateTable(b *testing.B) {
conn, err := grpc.Dial("localhost:"+port, grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()
client := NewEasyDataClient(conn)
for i := 0; i < b.N; i++ {
reply, err := client.CreateTable(context.Background(), &TableRequest{Name: "user"})
if err != nil {
panic(err)
}
reply.GetMsg()
}
// b.Error(reply.GetMsg())
}

12
go.mod Normal file
View File

@ -0,0 +1,12 @@
module edb
go 1.14
require (
github.com/golang/protobuf v1.3.4
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
golang.org/x/text v0.3.2 // indirect
google.golang.org/genproto v0.0.0-20200303153909-beee998c1893 // indirect
google.golang.org/grpc v1.27.1
)

61
go.sum Normal file
View File

@ -0,0 +1,61 @@
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/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
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 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.3.4 h1:87PNWwrRvUSnqS4dlcBU/ftvOIBep4sYuBLlh6rX2wk=
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
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 h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/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/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
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/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
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=
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 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200303153909-beee998c1893 h1:OTjq5CN+5TpMIvzqxSFCjbBX3jNKjX0XOPi4SdBxQU8=
google.golang.org/genproto v0.0.0-20200303153909-beee998c1893/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
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.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=
google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
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=

30
main.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"log"
"net"
"runtime"
grpc "google.golang.org/grpc"
)
const (
port = "20051"
)
func CreateServer() {
//起服务
listen, err := net.Listen("tcp", "localhost:"+port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
gserver := grpc.NewServer()
RegisterEasyDataServer(gserver, &server{})
gserver.Serve(listen)
// time.Sleep(time.Millisecond * 50)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
CreateServer()
}