-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc_protobuf_test.go
76 lines (61 loc) · 1.66 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package benchmarks
import (
grpcprotobuf "benchmark-grpc-protobuf/grpc-protobuf"
"benchmark-grpc-protobuf/grpc-protobuf/usertest"
"google.golang.org/grpc/encoding/gzip"
"testing"
"time"
"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 := usertest.NewAPIClient(conn)
for n := 0; n < b.N; n++ {
doGRPC(client, b)
}
}
func BenchmarkGzippedGRPCProtobuf(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 := usertest.NewAPIClient(conn)
for n := 0; n < b.N; n++ {
doGzippedGRPC(client, b)
}
}
func doGRPC(client usertest.APIClient, b *testing.B) {
resp, err := client.CreateUser(context.Background(), &usertest.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)
}
}
func doGzippedGRPC(client usertest.APIClient, b *testing.B) {
resp, err := client.CreateUser(context.Background(), &usertest.User{
Email: "[email protected]",
Name: "Bench",
Password: "bench",
},
g.UseCompressor(gzip.Name))
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)
}
}