Cleanup aspnetcore benchmarks and add metrics (#4266)

This commit is contained in:
Cijo Thomas 2023-03-03 16:44:22 -08:00 committed by GitHub
parent 4425ce52fc
commit 474ddcb217
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 242 deletions

View File

@ -1,85 +0,0 @@
// <copyright file="LocalServer.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 NETCOREAPP
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Trace;
namespace Benchmarks.Helper
{
public class LocalServer : IDisposable
{
private readonly IWebHost host;
private TracerProvider tracerProvider;
public LocalServer(string url, bool enableTracerProvider = false)
{
void ConfigureTestServices(IServiceCollection services)
{
if (enableTracerProvider)
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.Build();
}
}
this.host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.UseUrls(url)
.ConfigureServices(configure => ConfigureTestServices(configure))
.Build();
Task.Run(() => this.host.Run());
}
public void Dispose()
{
try
{
this.tracerProvider.Dispose();
this.host.Dispose();
}
catch (Exception)
{
// ignored, see https://github.com/aspnet/KestrelHttpServer/issues/1513
// Kestrel 2.0.0 should have fix it, but it does not seem important for our tests
}
GC.SuppressFinalize(this);
}
private class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!").ConfigureAwait(false);
});
}
}
}
}
#endif

View File

@ -1,34 +0,0 @@
// <copyright file="ValuesController.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 System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
namespace Benchmark.Helper
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
}
#endif

View File

@ -15,29 +15,27 @@
// </copyright>
#if !NETFRAMEWORK
using System.Net.Http;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
/*
// * Summary *
BenchmarkDotNet=v0.13.2, OS=Windows 11 (10.0.22621.521)
Intel Core i7-8850H CPU 2.60GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET SDK=7.0.100-preview.6.22275.1
[Host] : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT AVX2
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 |
|-------------------------------------------- |---------:|--------:|--------:|-------:|----------:|
| UninstrumentedAspNetCoreApp | 172.3 us | 2.35 us | 2.09 us | 0.9766 | 4.73 KB |
| InstrumentedAspNetCoreAppWithDefaultOptions | 175.2 us | 2.52 us | 2.10 us | 0.9766 | 4.86 KB |
| UninstrumentedAspNetCoreApp | 149.4 us | 2.94 us | 2.75 us | 0.4883 | 2.54 KB |
| InstrumentedAspNetCoreAppWithDefaultOptions | 171.9 us | 2.65 us | 2.48 us | 0.7324 | 3.79 KB |
*/
namespace Benchmarks.Instrumentation
@ -48,6 +46,7 @@ namespace Benchmarks.Instrumentation
private HttpClient httpClient;
private WebApplication app;
private TracerProvider tracerProvider;
private MeterProvider meterProvider;
[GlobalSetup(Target = nameof(UninstrumentedAspNetCoreApp))]
public void UninstrumentedAspNetCoreAppGlobalSetup()
@ -65,6 +64,12 @@ namespace Benchmarks.Instrumentation
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.Build();
var exportedItems = new List<Metric>();
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.AddAspNetCoreInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();
}
[GlobalCleanup(Target = nameof(UninstrumentedAspNetCoreApp))]
@ -80,29 +85,29 @@ namespace Benchmarks.Instrumentation
this.httpClient.Dispose();
await this.app.DisposeAsync().ConfigureAwait(false);
this.tracerProvider.Dispose();
this.meterProvider.Dispose();
}
[Benchmark]
public async Task UninstrumentedAspNetCoreApp()
{
var httpResponse = await this.httpClient.GetAsync("http://localhost:5000/api/values").ConfigureAwait(false);
var httpResponse = await this.httpClient.GetAsync("http://localhost:5000").ConfigureAwait(false);
httpResponse.EnsureSuccessStatusCode();
}
[Benchmark]
public async Task InstrumentedAspNetCoreAppWithDefaultOptions()
{
var httpResponse = await this.httpClient.GetAsync("http://localhost:5000/api/values").ConfigureAwait(false);
var httpResponse = await this.httpClient.GetAsync("http://localhost:5000").ConfigureAwait(false);
httpResponse.EnsureSuccessStatusCode();
}
private void StartWebApplication()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddControllers();
builder.Logging.ClearProviders();
var app = builder.Build();
app.MapControllers();
app.MapGet("/", () => $"Hello World!");
app.RunAsync();
this.app = app;

View File

@ -1,55 +0,0 @@
// <copyright file="InstrumentedAspNetCoreBenchmark.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 System.Net.Http;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Benchmarks.Helper;
namespace Benchmarks.Instrumentation
{
[InProcess]
public class InstrumentedAspNetCoreBenchmark
{
private const string LocalhostUrl = "http://localhost:5050";
private HttpClient client;
private LocalServer localServer;
[GlobalSetup]
public void GlobalSetup()
{
this.localServer = new LocalServer(LocalhostUrl, true);
this.client = new HttpClient();
}
[GlobalCleanup]
public void GlobalCleanup()
{
this.localServer.Dispose();
this.client.Dispose();
}
[Benchmark]
public async Task InstrumentedAspNetCoreGetPage()
{
var httpResponse = await this.client.GetAsync(LocalhostUrl).ConfigureAwait(false);
httpResponse.EnsureSuccessStatusCode();
}
}
}
#endif

View File

@ -1,55 +0,0 @@
// <copyright file="UninstrumentedAspNetCoreBenchmark.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 System.Net.Http;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Benchmarks.Helper;
namespace Benchmarks.Instrumentation
{
[InProcess]
public class UninstrumentedAspNetCoreBenchmark
{
private const string LocalhostUrl = "http://localhost:5050";
private HttpClient client;
private LocalServer localServer;
[GlobalSetup]
public void GlobalSetup()
{
this.localServer = new LocalServer(LocalhostUrl);
this.client = new HttpClient();
}
[GlobalCleanup]
public void GlobalCleanup()
{
this.client.Dispose();
this.localServer.Dispose();
}
[Benchmark]
public async Task SimpleAspNetCoreGetPage()
{
var httpResponse = await this.client.GetAsync(LocalhostUrl).ConfigureAwait(false);
httpResponse.EnsureSuccessStatusCode();
}
}
}
#endif