-
Notifications
You must be signed in to change notification settings - Fork 857
[api] Optimise TraceContextPropagator.Extract
#5749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CodeBlanch
merged 5 commits into
open-telemetry:main
from
stevejgordon:perf/tracecontextpropagator
Aug 12, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d01fa96
Optimise TraceContextPropagator.Extract
stevejgordon 538592e
Update changelog
stevejgordon a24b5a9
Reduce initial stackalloc buffer size and prefer try/finally
stevejgordon 35af52d
Fix typo
stevejgordon d43e26f
Merge from main.
CodeBlanch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
test/Benchmarks/Context/Propagation/TraceContextPropagatorBenchmarks.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
| using OpenTelemetry.Context.Propagation; | ||
|
|
||
| namespace Benchmarks.Context.Propagation; | ||
|
|
||
| public class TraceContextPropagatorBenchmarks | ||
| { | ||
| private const string TraceParent = "traceparent"; | ||
| private const string TraceState = "tracestate"; | ||
| private const string TraceId = "0af7651916cd43dd8448eb211c80319c"; | ||
| private const string SpanId = "b9c7c989f97918e1"; | ||
|
|
||
| private static readonly Random Random = new(455946); | ||
| private static readonly TraceContextPropagator TraceContextPropagator = new(); | ||
|
|
||
| private static readonly Func<IReadOnlyDictionary<string, string>, string, IEnumerable<string>> Getter = (headers, name) => | ||
| { | ||
| if (headers.TryGetValue(name, out var value)) | ||
| { | ||
| return [value]; | ||
| } | ||
|
|
||
| return []; | ||
| }; | ||
|
|
||
| private Dictionary<string, string> headers; | ||
|
|
||
| [Params(true, false)] | ||
| public bool LongListMember { get; set; } | ||
|
|
||
| [Params(0, 4, 32)] | ||
| public int MembersCount { get; set; } | ||
|
|
||
| public Dictionary<string, string> Headers => this.headers; | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| var length = this.LongListMember ? 256 : 20; | ||
|
|
||
| var value = new string('a', length); | ||
|
|
||
| Span<char> keyBuffer = stackalloc char[length - 2]; | ||
|
|
||
| string traceState = string.Empty; | ||
| for (var i = 0; i < this.MembersCount; i++) | ||
| { | ||
| // We want a unique key for each member | ||
| for (var j = 0; j < length - 2; j++) | ||
| { | ||
| keyBuffer[j] = (char)('a' + Random.Next(0, 26)); | ||
| } | ||
|
|
||
| var key = keyBuffer.ToString(); | ||
|
|
||
| var listMember = $"{key}{i:00}={value}"; | ||
| traceState += i < this.MembersCount - 1 ? $"{listMember}," : listMember; | ||
| } | ||
|
|
||
| this.headers = new Dictionary<string, string> | ||
| { | ||
| { TraceParent, $"00-{TraceId}-{SpanId}-01" }, | ||
| { TraceState, traceState }, | ||
| }; | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public void Extract() => _ = TraceContextPropagator!.Extract(default, this.headers, Getter); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.