Adding benchmark for Aspnetcore instrumentation (#1223)

* Adding benchmark for Aspnetcore instrumentation

* removing extra space

* renaming methods and removing resource

* updating to localserver

* removing unused code

* updating reference

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
Eddy Nakamura 2020-09-10 03:02:22 -03:00 committed by GitHub
parent 4ae9104ec5
commit e78dcae1e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 206 additions and 0 deletions

View File

@ -18,7 +18,18 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Jaeger\OpenTelemetry.Exporter.Jaeger.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Zipkin\OpenTelemetry.Exporter.Zipkin.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.AspNetCore\OpenTelemetry.Instrumentation.AspNetCore.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.Http\OpenTelemetry.Instrumentation.Http.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.8" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,83 @@
// <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
}
}
private class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
}
}
#endif

View File

@ -0,0 +1,56 @@
// <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]
[MemoryDiagnoser]
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);
httpResponse.EnsureSuccessStatusCode();
}
}
}
#endif

View File

@ -0,0 +1,56 @@
// <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]
[MemoryDiagnoser]
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);
httpResponse.EnsureSuccessStatusCode();
}
}
}
#endif