HttpClient benchmark updates (#4269)

This commit is contained in:
Cijo Thomas 2023-03-04 22:54:53 -08:00 committed by GitHub
parent 474ddcb217
commit 8cd0ef5642
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 118 additions and 145 deletions

View File

@ -0,0 +1,118 @@
// <copyright file="HttpClientInstrumentationBenchmarks.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
#if !NETFRAMEWORK
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
/*
// * Summary *
BenchmarkDotNet=v0.13.3, OS=Windows 10 (10.0.19045.2604)
Intel Core i7-4790 CPU 3.60GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET SDK=7.0.103
[Host] : .NET 7.0.3 (7.0.323.6910), X64 RyuJIT AVX2
Job=InProcess Toolchain=InProcessEmitToolchain
| Method | Mean | Error | StdDev | Gen0 | Allocated |
|------------------------- |---------:|--------:|--------:|-------:|----------:|
| UninstrumentedHttpClient | 153.3 us | 2.95 us | 3.83 us | 0.4883 | 2.54 KB |
| InstrumentedHttpClient | 170.4 us | 3.37 us | 4.14 us | 0.9766 | 4.51 KB |
*/
namespace Benchmarks.Instrumentation
{
[InProcess]
public class HttpClientInstrumentationBenchmarks
{
private HttpClient httpClient;
private WebApplication app;
private TracerProvider tracerProvider;
private MeterProvider meterProvider;
[GlobalSetup(Target = nameof(UninstrumentedHttpClient))]
public void UninstrumentedSetup()
{
this.StartWebApplication();
this.httpClient = new HttpClient();
}
[GlobalSetup(Target = nameof(InstrumentedHttpClient))]
public void InstrumentedSetup()
{
this.StartWebApplication();
this.httpClient = new HttpClient();
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.Build();
var exportedItems = new List<Metric>();
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.AddHttpClientInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();
}
[GlobalCleanup(Target = nameof(UninstrumentedHttpClient))]
public async Task UninstrumentedCleanupAsync()
{
this.httpClient.Dispose();
await this.app.DisposeAsync().ConfigureAwait(false);
}
[GlobalCleanup(Target = nameof(InstrumentedHttpClient))]
public async Task InstrumentedCleanupAsync()
{
this.httpClient.Dispose();
await this.app.DisposeAsync().ConfigureAwait(false);
this.tracerProvider.Dispose();
this.meterProvider.Dispose();
}
[Benchmark]
public async Task UninstrumentedHttpClient()
{
var httpResponse = await this.httpClient.GetAsync("http://localhost:5000").ConfigureAwait(false);
httpResponse.EnsureSuccessStatusCode();
}
[Benchmark]
public async Task InstrumentedHttpClient()
{
var httpResponse = await this.httpClient.GetAsync("http://localhost:5000").ConfigureAwait(false);
httpResponse.EnsureSuccessStatusCode();
}
private void StartWebApplication()
{
var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();
var app = builder.Build();
app.MapGet("/", () => $"Hello World!");
app.RunAsync();
this.app = app;
}
}
}
#endif

View File

@ -1,86 +0,0 @@
// <copyright file="InstrumentedHttpClientBenchmark.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Diagnostics;
using System.Net.Http;
using BenchmarkDotNet.Attributes;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
namespace Benchmarks.Instrumentation
{
public class InstrumentedHttpClientBenchmark
{
private const string ActivityName = "incoming request";
private const string ServiceName = "http-service-example";
private const string SourceName = "http-client-test";
private HttpClient httpClient;
private TracerProvider tracerProvider;
private IDisposable serverLifeTime;
private ActivitySource source;
private string url;
[GlobalSetup]
public void GlobalSetup()
{
this.serverLifeTime = TestHttpServer.RunServer(
(ctx) =>
{
ctx.Response.StatusCode = 200;
ctx.Response.OutputStream.Close();
},
out var host,
out var port);
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.ConfigureResource(r => r.AddService(ServiceName))
.AddSource(SourceName)
.Build();
this.url = $"http://{host}:{port}/";
this.httpClient = new HttpClient();
this.source = new ActivitySource(SourceName);
}
[GlobalCleanup]
public void GlobalCleanup()
{
this.httpClient.Dispose();
this.tracerProvider.Dispose();
this.serverLifeTime.Dispose();
this.source.Dispose();
}
[Benchmark]
public async Task InstrumentedHttpClient()
{
var httpResponse = await this.httpClient.GetAsync(this.url).ConfigureAwait(false);
httpResponse.EnsureSuccessStatusCode();
}
[Benchmark]
public async Task InstrumentedHttpClientWithParentActivity()
{
using var parent = this.source.StartActivity(ActivityName, ActivityKind.Server);
var httpResponse = await this.httpClient.GetAsync(this.url).ConfigureAwait(false);
httpResponse.EnsureSuccessStatusCode();
}
}
}

View File

@ -1,59 +0,0 @@
// <copyright file="UninstrumentedHttpClientBenchmark.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Net.Http;
using BenchmarkDotNet.Attributes;
using OpenTelemetry.Tests;
namespace Benchmarks.Instrumentation
{
public class UninstrumentedHttpClientBenchmark
{
private IDisposable serverLifeTime;
private string url;
private HttpClient httpClient;
[GlobalSetup]
public void GlobalSetup()
{
this.serverLifeTime = TestHttpServer.RunServer(
(ctx) =>
{
ctx.Response.StatusCode = 200;
ctx.Response.OutputStream.Close();
},
out var host,
out var port);
this.url = $"http://{host}:{port}/";
this.httpClient = new HttpClient();
}
[GlobalCleanup]
public void GlobalCleanup()
{
this.serverLifeTime.Dispose();
this.httpClient.Dispose();
}
[Benchmark]
public async Task SimpleHttpClient()
{
var httpResponse = await this.httpClient.GetAsync(this.url).ConfigureAwait(false);
httpResponse.EnsureSuccessStatusCode();
}
}
}