-
Notifications
You must be signed in to change notification settings - Fork 378
[AspNetCore] Simplify benchmarks and add gRPC #4091
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
Open
martincostello
wants to merge
3
commits into
open-telemetry:main
Choose a base branch
from
martincostello:simplify-aspnetcode-benchmarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
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
143 changes: 143 additions & 0 deletions
143
test/OpenTelemetry.Instrumentation.AspNetCore.Benchmarks/AspNetCoreBenchmarks.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,143 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
| using Greet; | ||
| using Grpc.Core; | ||
| using Grpc.Net.Client; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using OpenTelemetry.Metrics; | ||
| using OpenTelemetry.Trace; | ||
|
|
||
| namespace OpenTelemetry.Instrumentation.AspNetCore.Benchmarks; | ||
|
|
||
| [MemoryDiagnoser] | ||
| public class AspNetCoreBenchmarks | ||
| { | ||
| private static readonly Uri BaseAddress = new("/", UriKind.Relative); | ||
|
|
||
| private readonly HelloRequest helloRequest = new(); | ||
| private HttpClient? httpClient; | ||
| private Greeter.GreeterClient? grpcClient; | ||
| private WebApplication? app; | ||
| private TracerProvider? tracerProvider; | ||
| private MeterProvider? meterProvider; | ||
|
|
||
| [Flags] | ||
| public enum EnableInstrumentationOption | ||
| { | ||
| /// <summary> | ||
| /// Instrumentation is not enabled for any signal. | ||
| /// </summary> | ||
| None = 0, | ||
|
|
||
| /// <summary> | ||
| /// Instrumentation is enabled only for Traces. | ||
| /// </summary> | ||
| Traces = 1, | ||
|
|
||
| /// <summary> | ||
| /// Instrumentation is enabled only for Metrics. | ||
| /// </summary> | ||
| Metrics = 2, | ||
| } | ||
|
|
||
| [Params(EnableInstrumentationOption.None, EnableInstrumentationOption.Traces, EnableInstrumentationOption.Metrics, EnableInstrumentationOption.Traces | EnableInstrumentationOption.Metrics)] | ||
| public EnableInstrumentationOption EnableInstrumentation { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public async Task StartServer() | ||
| { | ||
| await this.StartWebApplicationAsync(); | ||
|
|
||
| KeyValuePair<string, string?>[] config = | ||
| [ | ||
| new("OTEL_DOTNET_EXPERIMENTAL_ASPNETCORE_ENABLE_GRPC_INSTRUMENTATION", "true"), | ||
| ]; | ||
|
|
||
| IConfiguration configuration = new ConfigurationBuilder() | ||
| .AddInMemoryCollection(config) | ||
| .Build(); | ||
|
|
||
| if (this.EnableInstrumentation.HasFlag(EnableInstrumentationOption.Traces)) | ||
| { | ||
| this.tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
| .ConfigureServices((services) => services.AddSingleton(configuration)) | ||
| .AddAspNetCoreInstrumentation() | ||
| .Build(); | ||
| } | ||
|
|
||
| if (this.EnableInstrumentation.HasFlag(EnableInstrumentationOption.Metrics)) | ||
| { | ||
| this.meterProvider = Sdk.CreateMeterProviderBuilder() | ||
| .ConfigureServices((services) => services.AddSingleton(configuration)) | ||
| .AddAspNetCoreInstrumentation() | ||
| .Build(); | ||
| } | ||
| } | ||
|
|
||
| [GlobalCleanup] | ||
| public async Task StopServer() | ||
| { | ||
| this.httpClient?.Dispose(); | ||
|
|
||
| if (this.app != null) | ||
| { | ||
| await this.app.StopAsync(); | ||
| await this.app.DisposeAsync(); | ||
| } | ||
|
|
||
| this.tracerProvider?.Dispose(); | ||
| this.meterProvider?.Dispose(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task HttpGet() | ||
| { | ||
| using var httpResponse = await this.httpClient!.GetAsync(BaseAddress).ConfigureAwait(false); | ||
| httpResponse.EnsureSuccessStatusCode(); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task<HelloReply> GrpcGet() => | ||
| await this.grpcClient!.SayHelloAsync(this.helloRequest); | ||
|
|
||
| private async Task StartWebApplicationAsync() | ||
| { | ||
| var builder = WebApplication.CreateBuilder(); | ||
|
|
||
| builder.Logging.ClearProviders(); | ||
| builder.WebHost.UseTestServer(); | ||
|
|
||
| builder.Services.AddGrpc(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.MapGet("/", async context => await context.Response.WriteAsync("Hello World!")); | ||
|
|
||
| app.MapGrpcService<GreeterService>(); | ||
|
|
||
| await app.StartAsync(); | ||
|
|
||
| this.app = app; | ||
| this.httpClient = app.GetTestClient(); | ||
|
|
||
| var channel = GrpcChannel.ForAddress("http://localhost", new() | ||
| { | ||
| HttpClient = this.httpClient, | ||
| }); | ||
|
|
||
| this.grpcClient = new Greeter.GreeterClient(channel); | ||
| } | ||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private sealed class GreeterService : Greeter.GreeterBase | ||
| { | ||
| public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) => | ||
| Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); | ||
| } | ||
| } | ||
200 changes: 0 additions & 200 deletions
200
...trumentation.AspNetCore.Benchmarks/Instrumentation/AspNetCoreInstrumentationBenchmarks.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.