edb/api_test.go
2020-03-05 05:21:29 +08:00

90 lines
1.6 KiB
Go

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