edb/api_test.go

90 lines
1.6 KiB
Go
Raw Permalink Normal View History

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