Debian Codegraph @

instrumentation/google.golang.org/grpc/otelgrpc/benchmark_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
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 pb "google.golang.org/grpc/interop/grpc_testing"
14 "google.golang.org/grpc/test/bufconn"
15
16 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/internal/test"
17 )
18
19 const bufSize = 2048
20
21 func benchmark(b *testing.B, cOpt []grpc.DialOption, sOpt []grpc.ServerOption) {
22 l := bufconn.Listen(bufSize)
23 defer l.Close()
24
25 s := grpc.NewServer(sOpt...)
26 pb.RegisterTestServiceServer(s, test.NewTestServer())
27 go func() {
28 if err := s.Serve(l); err != nil {
29 panic(err)
30 }
31 }()
32 defer s.Stop()
33
34 ctx := b.Context()
35 dial := func(context.Context, string) (net.Conn, error) { return l.Dial() }
36 conn, err := grpc.NewClient(
37 "passthrough:bufnet",
38 append([]grpc.DialOption{
39 grpc.WithContextDialer(dial),
40 grpc.WithTransportCredentials(insecure.NewCredentials()),
41 }, cOpt...)...,
42 )
43 if err != nil {
44 b.Fatalf("Failed to dial bufnet: %v", err)
45 }
46 defer conn.Close()
47 client := pb.NewTestServiceClient(conn)
48
49 b.ReportAllocs()
50 b.ResetTimer()
51
52 for range b.N {
53 test.DoEmptyUnaryCall(ctx, client)
54 test.DoLargeUnaryCall(ctx, client)
55 test.DoClientStreaming(ctx, client)
56 test.DoServerStreaming(ctx, client)
57 test.DoPingPong(ctx, client)
58 test.DoEmptyStream(ctx, client)
59 }
60
61 b.StopTimer()
62 }
63
64 func BenchmarkNoInstrumentation(b *testing.B) {
65 benchmark(b, nil, nil)
66 }