|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package grpc_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "net" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "google.golang.org/grpc" |
| 23 | + "google.golang.org/grpc/interop" |
| 24 | + pb "google.golang.org/grpc/interop/grpc_testing" |
| 25 | + "google.golang.org/grpc/test/bufconn" |
| 26 | + |
| 27 | + otelgrpc "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc" |
| 28 | + "go.opentelemetry.io/otel/api/trace/tracetest" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + bufSize = 2048 |
| 33 | + instName = "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc" |
| 34 | +) |
| 35 | + |
| 36 | +var tracer = tracetest.NewProvider().Tracer(instName) |
| 37 | + |
| 38 | +func benchmark(b *testing.B, cOpt []grpc.DialOption, sOpt []grpc.ServerOption) { |
| 39 | + l := bufconn.Listen(bufSize) |
| 40 | + defer l.Close() |
| 41 | + |
| 42 | + s := grpc.NewServer(sOpt...) |
| 43 | + pb.RegisterTestServiceServer(s, interop.NewTestServer()) |
| 44 | + go func() { |
| 45 | + if err := s.Serve(l); err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | + }() |
| 49 | + defer s.Stop() |
| 50 | + |
| 51 | + ctx := context.Background() |
| 52 | + dial := func(context.Context, string) (net.Conn, error) { return l.Dial() } |
| 53 | + conn, err := grpc.DialContext( |
| 54 | + ctx, |
| 55 | + "bufnet", |
| 56 | + append([]grpc.DialOption{ |
| 57 | + grpc.WithContextDialer(dial), |
| 58 | + grpc.WithInsecure(), |
| 59 | + }, cOpt...)..., |
| 60 | + ) |
| 61 | + if err != nil { |
| 62 | + b.Fatalf("Failed to dial bufnet: %v", err) |
| 63 | + } |
| 64 | + defer conn.Close() |
| 65 | + client := pb.NewTestServiceClient(conn) |
| 66 | + |
| 67 | + b.ReportAllocs() |
| 68 | + b.ResetTimer() |
| 69 | + |
| 70 | + for n := 0; n < b.N; n++ { |
| 71 | + interop.DoEmptyUnaryCall(client) |
| 72 | + interop.DoLargeUnaryCall(client) |
| 73 | + interop.DoClientStreaming(client) |
| 74 | + interop.DoServerStreaming(client) |
| 75 | + interop.DoPingPong(client) |
| 76 | + interop.DoEmptyStream(client) |
| 77 | + } |
| 78 | + |
| 79 | + b.StopTimer() |
| 80 | +} |
| 81 | + |
| 82 | +func BenchmarkNoInstrumentation(b *testing.B) { |
| 83 | + benchmark(b, nil, nil) |
| 84 | +} |
| 85 | + |
| 86 | +func BenchmarkUnaryServerInterceptor(b *testing.B) { |
| 87 | + benchmark(b, nil, []grpc.ServerOption{ |
| 88 | + grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(tracer)), |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +func BenchmarkStreamServerInterceptor(b *testing.B) { |
| 93 | + benchmark(b, nil, []grpc.ServerOption{ |
| 94 | + grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor(tracer)), |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func BenchmarkUnaryClientInterceptor(b *testing.B) { |
| 99 | + benchmark(b, []grpc.DialOption{ |
| 100 | + grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor(tracer)), |
| 101 | + }, nil) |
| 102 | +} |
| 103 | + |
| 104 | +func BenchmarkStreamClientInterceptor(b *testing.B) { |
| 105 | + benchmark(b, []grpc.DialOption{ |
| 106 | + grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor(tracer)), |
| 107 | + }, nil) |
| 108 | +} |
0 commit comments