forked from icco/logrus-stackdriver-formatter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_test.go
120 lines (107 loc) · 2.76 KB
/
benchmark_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package logadapter_test
import (
"context"
"io"
"net"
"testing"
logadapter "github.com/StevenACoffman/logrus-stackdriver-formatter"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/interop"
pb "google.golang.org/grpc/interop/grpc_testing"
"google.golang.org/grpc/test/bufconn"
)
const bufSize = 2048
func benchmark(b *testing.B, opt ...grpc.ServerOption) {
l := bufconn.Listen(bufSize)
defer l.Close()
s := grpc.NewServer(opt...)
pb.RegisterTestServiceServer(s, interop.NewTestServer())
go func() {
if err := s.Serve(l); err != nil {
panic(err)
}
}()
defer s.Stop()
ctx := context.Background()
dial := func(context.Context, string) (net.Conn, error) { return l.Dial() }
conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithContextDialer(dial), grpc.WithInsecure())
if err != nil {
b.Fatalf("Failed to dial bufnet: %v", err)
}
defer conn.Close()
client := pb.NewTestServiceClient(conn)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
interop.DoEmptyUnaryCall(client)
interop.DoLargeUnaryCall(client)
interop.DoClientStreaming(client)
interop.DoServerStreaming(client)
interop.DoPingPong(client)
interop.DoEmptyStream(client)
}
b.StopTimer()
}
func BenchmarkMiddleware(b *testing.B) {
logger := logrus.New()
formatter := logadapter.NewFormatter(
logadapter.WithProjectID("test-project"),
logadapter.WithService("benchmark"),
logadapter.WithVersion("v1.0.0"),
)
logger.SetFormatter(formatter)
logger.SetOutput(io.Discard)
logger.SetReportCaller(true)
tcases := []struct {
Name string
Middlewares []grpc.ServerOption
}{
{
Name: "NoInstrumentation",
Middlewares: []grpc.ServerOption{},
},
{
Name: "UnaryLoggingInterceptor",
Middlewares: []grpc.ServerOption{
grpc.UnaryInterceptor(logadapter.UnaryLoggingInterceptor(logger)),
},
},
{
Name: "StreamLoggingInterceptor",
Middlewares: []grpc.ServerOption{
grpc.StreamInterceptor(logadapter.StreamLoggingInterceptor(logger)),
},
},
{
Name: "UnaryRecoverInterceptor",
Middlewares: []grpc.ServerOption{
grpc.UnaryInterceptor(logadapter.UnaryRecoveryInterceptor),
},
},
{
Name: "StreamRecoverInterceptor",
Middlewares: []grpc.ServerOption{
grpc.StreamInterceptor(logadapter.StreamRecoveryInterceptor),
},
},
{
Name: "LoggingRecoverInterceptor",
Middlewares: []grpc.ServerOption{
grpc.ChainUnaryInterceptor(
logadapter.UnaryLoggingInterceptor(logger),
logadapter.UnaryRecoveryInterceptor,
),
grpc.ChainStreamInterceptor(
logadapter.StreamLoggingInterceptor(logger),
logadapter.StreamRecoveryInterceptor,
),
},
},
}
for _, tc := range tcases {
b.Run(tc.Name, func(b *testing.B) {
benchmark(b, tc.Middlewares...)
})
}
}