package main import ( "context" proto "edb/goproto" "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() proto.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 := proto.NewEasyDataClient(conn) for i := 0; i < COUNT; i++ { reply, err := client.CreateTable(context.Background(), &proto.TableRequest{Name: "user"}) if err == nil { reply.GetMsg() } } } func TestSayHello(t *testing.T) { now := time.Now().Unix() wg := &sync.WaitGroup{} GCOUNT := 400000 COUNT := 10 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 := proto.NewEasyDataClient(conn) for i := 0; i < b.N; i++ { reply, err := client.CreateTable(context.Background(), &proto.TableRequest{Name: "user"}) if err != nil { panic(err) } reply.GetMsg() } // b.Error(reply.GetMsg()) }