| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelgrpc |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"net" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
metricnoop "go.opentelemetry.io/otel/metric/noop" |
| 13 |
"go.opentelemetry.io/otel/sdk/metric" |
| 14 |
"go.opentelemetry.io/otel/sdk/trace" |
| 15 |
tracenoop "go.opentelemetry.io/otel/trace/noop" |
| 16 |
"google.golang.org/grpc/peer" |
| 17 |
"google.golang.org/grpc/stats" |
| 18 |
) |
| 19 |
|
| 20 |
func benchmarkStatsHandlerHandleRPC(b *testing.B, ctx context.Context, handler stats.Handler, stat stats.RPCStats) { |
| 21 |
b.ReportAllocs() |
| 22 |
b.ResetTimer() |
| 23 |
for range b.N { |
| 24 |
handler.HandleRPC(ctx, stat) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func benchmarkServerHandlerHandleRPC(b *testing.B, stat stats.RPCStats) { |
| 29 |
handler := NewServerHandler( |
| 30 |
WithTracerProvider(trace.NewTracerProvider( |
| 31 |
trace.WithSampler(trace.AlwaysSample()), |
| 32 |
)), |
| 33 |
WithMeterProvider(metric.NewMeterProvider()), |
| 34 |
WithMessageEvents(ReceivedEvents, SentEvents), |
| 35 |
) |
| 36 |
ctx := b.Context() |
| 37 |
ctx = handler.TagRPC(ctx, &stats.RPCTagInfo{ |
| 38 |
FullMethodName: "/package.service/method", |
| 39 |
}) |
| 40 |
ctx = peer.NewContext(ctx, &peer.Peer{ |
| 41 |
Addr: &net.TCPAddr{ |
| 42 |
IP: net.IPv4(127, 0, 0, 1), |
| 43 |
Port: 1234, |
| 44 |
}, |
| 45 |
}) |
| 46 |
benchmarkStatsHandlerHandleRPC(b, ctx, handler, stat) |
| 47 |
} |
| 48 |
|
| 49 |
func BenchmarkServerHandler_HandleRPC_Begin(b *testing.B) { |
| 50 |
benchmarkServerHandlerHandleRPC(b, &stats.Begin{ |
| 51 |
BeginTime: time.Now(), |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
func BenchmarkServerHandler_HandleRPC_InPayload(b *testing.B) { |
| 56 |
benchmarkServerHandlerHandleRPC(b, &stats.InPayload{ |
| 57 |
Length: 1024, |
| 58 |
CompressedLength: 512, |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
func BenchmarkServerHandler_HandleRPC_OutPayload(b *testing.B) { |
| 63 |
benchmarkServerHandlerHandleRPC(b, &stats.OutPayload{ |
| 64 |
Length: 1024, |
| 65 |
CompressedLength: 512, |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
func BenchmarkServerHandler_HandleRPC_OutTrailer(b *testing.B) { |
| 70 |
benchmarkServerHandlerHandleRPC(b, &stats.OutTrailer{}) |
| 71 |
} |
| 72 |
|
| 73 |
func BenchmarkServerHandler_HandleRPC_OutHeader(b *testing.B) { |
| 74 |
benchmarkServerHandlerHandleRPC(b, &stats.OutHeader{}) |
| 75 |
} |
| 76 |
|
| 77 |
func BenchmarkServerHandler_HandleRPC_End(b *testing.B) { |
| 78 |
benchmarkServerHandlerHandleRPC(b, &stats.End{ |
| 79 |
EndTime: time.Now(), |
| 80 |
}) |
| 81 |
} |
| 82 |
|
| 83 |
func BenchmarkServerHandler_HandleRPC_Nil(b *testing.B) { |
| 84 |
benchmarkServerHandlerHandleRPC(b, nil) |
| 85 |
} |
| 86 |
|
| 87 |
func benchmarkServerHandlerHandleRPCNoOp(b *testing.B, stat stats.RPCStats) { |
| 88 |
handler := NewServerHandler( |
| 89 |
WithTracerProvider(tracenoop.NewTracerProvider()), |
| 90 |
WithMeterProvider(metricnoop.NewMeterProvider()), |
| 91 |
WithMessageEvents(ReceivedEvents, SentEvents), |
| 92 |
) |
| 93 |
ctx := b.Context() |
| 94 |
ctx = handler.TagRPC(ctx, &stats.RPCTagInfo{ |
| 95 |
FullMethodName: "/package.service/method", |
| 96 |
}) |
| 97 |
ctx = peer.NewContext(ctx, &peer.Peer{ |
| 98 |
Addr: &net.TCPAddr{ |
| 99 |
IP: net.IPv4(127, 0, 0, 1), |
| 100 |
Port: 1234, |
| 101 |
}, |
| 102 |
}) |
| 103 |
|
| 104 |
benchmarkStatsHandlerHandleRPC(b, ctx, handler, stat) |
| 105 |
} |
| 106 |
|
| 107 |
func BenchmarkServerHandler_HandleRPC_NoOp_Begin(b *testing.B) { |
| 108 |
benchmarkServerHandlerHandleRPCNoOp(b, &stats.Begin{ |
| 109 |
BeginTime: time.Now(), |
| 110 |
}) |
| 111 |
} |
| 112 |
|
| 113 |
func BenchmarkServerHandler_HandleRPC_NoOp_InPayload(b *testing.B) { |
| 114 |
benchmarkServerHandlerHandleRPCNoOp(b, &stats.InPayload{ |
| 115 |
Length: 1024, |
| 116 |
CompressedLength: 512, |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
func BenchmarkServerHandler_HandleRPC_NoOp_OutPayload(b *testing.B) { |
| 121 |
benchmarkServerHandlerHandleRPCNoOp(b, &stats.OutPayload{ |
| 122 |
Length: 1024, |
| 123 |
CompressedLength: 512, |
| 124 |
}) |
| 125 |
} |
| 126 |
|
| 127 |
func BenchmarkServerHandler_HandleRPC_NoOp_OutTrailer(b *testing.B) { |
| 128 |
benchmarkServerHandlerHandleRPCNoOp(b, &stats.OutTrailer{}) |
| 129 |
} |
| 130 |
|
| 131 |
func BenchmarkServerHandler_HandleRPC_NoOp_OutHeader(b *testing.B) { |
| 132 |
benchmarkServerHandlerHandleRPCNoOp(b, &stats.OutHeader{}) |
| 133 |
} |
| 134 |
|
| 135 |
func BenchmarkServerHandler_HandleRPC_NoOp_End(b *testing.B) { |
| 136 |
benchmarkServerHandlerHandleRPCNoOp(b, &stats.End{ |
| 137 |
EndTime: time.Now(), |
| 138 |
}) |
| 139 |
} |
| 140 |
|
| 141 |
func BenchmarkServerHandler_HandleRPC_NoOp_Nil(b *testing.B) { |
| 142 |
benchmarkServerHandlerHandleRPCNoOp(b, nil) |
| 143 |
} |
| 144 |
|
| 145 |
func BenchmarkServerHandler_TagRPCNoOp(b *testing.B) { |
| 146 |
handler := NewServerHandler( |
| 147 |
WithTracerProvider(tracenoop.NewTracerProvider()), |
| 148 |
WithMeterProvider(metricnoop.NewMeterProvider()), |
| 149 |
WithMessageEvents(ReceivedEvents, SentEvents), |
| 150 |
) |
| 151 |
ctx := b.Context() |
| 152 |
|
| 153 |
tagInfo := &stats.RPCTagInfo{ |
| 154 |
FullMethodName: "/package.service/method", |
| 155 |
} |
| 156 |
|
| 157 |
b.ReportAllocs() |
| 158 |
for b.Loop() { |
| 159 |
_ = handler.TagRPC(ctx, tagInfo) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func BenchmarkClientHandler_TagRPCNoOp(b *testing.B) { |
| 164 |
handler := NewClientHandler( |
| 165 |
WithTracerProvider(tracenoop.NewTracerProvider()), |
| 166 |
WithMeterProvider(metricnoop.NewMeterProvider()), |
| 167 |
WithMessageEvents(ReceivedEvents, SentEvents), |
| 168 |
) |
| 169 |
ctx := b.Context() |
| 170 |
|
| 171 |
tagInfo := &stats.RPCTagInfo{ |
| 172 |
FullMethodName: "/package.service/method", |
| 173 |
} |
| 174 |
|
| 175 |
b.ReportAllocs() |
| 176 |
for b.Loop() { |
| 177 |
_ = handler.TagRPC(ctx, tagInfo) |
| 178 |
} |
| 179 |
} |