forked from plutov/benchmark-grpc-protobuf-vs-http-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_protobuf_test.go
45 lines (36 loc) · 980 Bytes
/
grpc_protobuf_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package benchmarks
import (
"testing"
"time"
"github.com/plutov/benchmark-grpc-protobuf-vs-http-json/grpc-protobuf"
"github.com/plutov/benchmark-grpc-protobuf-vs-http-json/grpc-protobuf/proto"
"golang.org/x/net/context"
g "google.golang.org/grpc"
)
func init() {
go grpcprotobuf.Start()
time.Sleep(time.Second)
}
func BenchmarkGRPCProtobuf(b *testing.B) {
conn, err := g.Dial("127.0.0.1:60000", g.WithInsecure())
if err != nil {
b.Fatalf("grpc connection failed: %v", err)
}
client := proto.NewAPIClient(conn)
for n := 0; n < b.N; n++ {
doGRPC(client, b)
}
}
func doGRPC(client proto.APIClient, b *testing.B) {
resp, err := client.CreateUser(context.Background(), &proto.User{
Email: "[email protected]",
Name: "Bench",
Password: "bench",
})
if err != nil {
b.Fatalf("grpc request failed: %v", err)
}
if resp == nil || resp.Code != 200 || resp.User == nil || resp.User.Id != "1000000" {
b.Fatalf("grpc response is wrong: %v", resp)
}
}