...
1
2
3
4 package otelgrpc_test
5
6 import (
7 "context"
8 "net"
9 "testing"
10
11 "google.golang.org/grpc"
12 "google.golang.org/grpc/credentials/insecure"
13 "google.golang.org/grpc/test/bufconn"
14
15 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
16 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal/test"
17 "go.opentelemetry.io/otel/trace/noop"
18
19 pb "google.golang.org/grpc/interop/grpc_testing"
20 )
21
22 const bufSize = 2048
23
24 var tracerProvider = noop.NewTracerProvider()
25
26 func benchmark(b *testing.B, cOpt []grpc.DialOption, sOpt []grpc.ServerOption) {
27 l := bufconn.Listen(bufSize)
28 defer l.Close()
29
30 s := grpc.NewServer(sOpt...)
31 pb.RegisterTestServiceServer(s, test.NewTestServer())
32 go func() {
33 if err := s.Serve(l); err != nil {
34 panic(err)
35 }
36 }()
37 defer s.Stop()
38
39 ctx := context.Background()
40 dial := func(context.Context, string) (net.Conn, error) { return l.Dial() }
41 conn, err := grpc.DialContext(
42 ctx,
43 "bufnet",
44 append([]grpc.DialOption{
45 grpc.WithContextDialer(dial),
46 grpc.WithTransportCredentials(insecure.NewCredentials()),
47 }, cOpt...)...,
48 )
49 if err != nil {
50 b.Fatalf("Failed to dial bufnet: %v", err)
51 }
52 defer conn.Close()
53 client := pb.NewTestServiceClient(conn)
54
55 b.ReportAllocs()
56 b.ResetTimer()
57
58 for n := 0; n < b.N; n++ {
59 test.DoEmptyUnaryCall(ctx, client)
60 test.DoLargeUnaryCall(ctx, client)
61 test.DoClientStreaming(ctx, client)
62 test.DoServerStreaming(ctx, client)
63 test.DoPingPong(ctx, client)
64 test.DoEmptyStream(ctx, client)
65 }
66
67 b.StopTimer()
68 }
69
70 func BenchmarkNoInstrumentation(b *testing.B) {
71 benchmark(b, nil, nil)
72 }
73
74 func BenchmarkUnaryServerInterceptor(b *testing.B) {
75 benchmark(b, nil, []grpc.ServerOption{
76 grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(
77 otelgrpc.WithTracerProvider(tracerProvider),
78 )),
79 })
80 }
81
82 func BenchmarkStreamServerInterceptor(b *testing.B) {
83 benchmark(b, nil, []grpc.ServerOption{
84 grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor(
85 otelgrpc.WithTracerProvider(tracerProvider),
86 )),
87 })
88 }
89
90 func BenchmarkUnaryClientInterceptor(b *testing.B) {
91 benchmark(b, []grpc.DialOption{
92 grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor(
93 otelgrpc.WithTracerProvider(tracerProvider),
94 )),
95 }, nil)
96 }
97
98 func BenchmarkStreamClientInterceptor(b *testing.B) {
99 benchmark(b, []grpc.DialOption{
100 grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor(
101 otelgrpc.WithTracerProvider(tracerProvider),
102 )),
103 }, nil)
104 }
105
View as plain text