|
| 1 | +// Copyright (c) 2026 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package grpc |
| 22 | + |
| 23 | +import ( |
| 24 | + "testing" |
| 25 | + |
| 26 | + "go.uber.org/yarpc/api/transport" |
| 27 | + "google.golang.org/grpc/metadata" |
| 28 | +) |
| 29 | + |
| 30 | +// BenchmarkTransportRequestToMetadata measures outbound header serialization. |
| 31 | +// |
| 32 | +// go test -bench=BenchmarkTransportRequestToMetadata -benchmem ./transport/grpc/ |
| 33 | +func BenchmarkTransportRequestToMetadata(b *testing.B) { |
| 34 | + request := &transport.Request{ |
| 35 | + Caller: "my-caller", |
| 36 | + Service: "my-service", |
| 37 | + Procedure: "MyService::MyMethod", |
| 38 | + Encoding: "proto", |
| 39 | + ShardKey: "shard-123", |
| 40 | + RoutingKey: "routing-456", |
| 41 | + RoutingDelegate: "delegate-789", |
| 42 | + CallerProcedure: "CallerService::CallerMethod", |
| 43 | + Headers: transport.HeadersFromMap(map[string]string{ |
| 44 | + "x-custom-1": "value1", |
| 45 | + "x-custom-2": "value2", |
| 46 | + "x-custom-3": "value3", |
| 47 | + }), |
| 48 | + } |
| 49 | + |
| 50 | + b.ReportAllocs() |
| 51 | + b.ResetTimer() |
| 52 | + for i := 0; i < b.N; i++ { |
| 53 | + _, _ = transportRequestToMetadata(request) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// BenchmarkMetadataToTransportRequest measures inbound header deserialization. |
| 58 | +// |
| 59 | +// go test -bench=BenchmarkMetadataToTransportRequest -benchmem ./transport/grpc/ |
| 60 | +func BenchmarkMetadataToTransportRequest(b *testing.B) { |
| 61 | + md := metadata.New(map[string]string{ |
| 62 | + CallerHeader: "my-caller", |
| 63 | + ServiceHeader: "my-service", |
| 64 | + ShardKeyHeader: "shard-123", |
| 65 | + RoutingKeyHeader: "routing-456", |
| 66 | + RoutingDelegateHeader: "delegate-789", |
| 67 | + EncodingHeader: "proto", |
| 68 | + CallerProcedureHeader: "CallerService::CallerMethod", |
| 69 | + "x-custom-1": "value1", |
| 70 | + "x-custom-2": "value2", |
| 71 | + "x-custom-3": "value3", |
| 72 | + }) |
| 73 | + |
| 74 | + b.ReportAllocs() |
| 75 | + b.ResetTimer() |
| 76 | + for i := 0; i < b.N; i++ { |
| 77 | + _, _ = metadataToTransportRequest(md) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// BenchmarkGetApplicationHeaders measures inbound response header extraction. |
| 82 | +// |
| 83 | +// go test -bench=BenchmarkGetApplicationHeaders -benchmem ./transport/grpc/ |
| 84 | +func BenchmarkGetApplicationHeaders(b *testing.B) { |
| 85 | + md := metadata.New(map[string]string{ |
| 86 | + CallerHeader: "my-caller", |
| 87 | + ServiceHeader: "my-service", |
| 88 | + EncodingHeader: "proto", |
| 89 | + "x-custom-1": "value1", |
| 90 | + "x-custom-2": "value2", |
| 91 | + "x-custom-3": "value3", |
| 92 | + "x-custom-4": "value4", |
| 93 | + "x-custom-5": "value5", |
| 94 | + }) |
| 95 | + |
| 96 | + b.ReportAllocs() |
| 97 | + b.ResetTimer() |
| 98 | + for i := 0; i < b.N; i++ { |
| 99 | + _, _ = getApplicationHeaders(md) |
| 100 | + } |
| 101 | +} |
0 commit comments