Added benchmark and test for dispatcher

This commit is contained in:
Kiran Challa 2018-06-01 11:59:45 -07:00
parent dc4be30d88
commit 2fed6710a9
8 changed files with 158 additions and 11 deletions

View File

@ -5,13 +5,15 @@
<TargetFramework Condition="'$(BenchmarksTargetFramework)' != ''">$(BenchmarksTargetFramework)</TargetFramework>
</PropertyGroup>
<!-- These references are used when running locally -->
<!--These references are used when running locally-->
<ItemGroup Condition="'$(BenchmarksTargetFramework)' == ''">
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Routing\Microsoft.AspNetCore.Routing.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(MicrosoftAspNetCoreServerKestrelPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="$(MicrosoftExtensionsConfigurationCommandLinePackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion)" />
</ItemGroup>
<!-- These references are used when running on the Benchmarks Server -->
<!--These references are used when running on the Benchmarks Server-->
<ItemGroup Condition="'$(BenchmarksTargetFramework)' != ''">
<PackageReference Include="Microsoft.AspNetCore.App" Version="$(MicrosoftAspNetCoreAppPackageVersion)" />
</ItemGroup>

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
@ -16,15 +17,37 @@ namespace Benchmarks
public static IWebHostBuilder GetWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.AddEnvironmentVariables(prefix: "RoutingBenchmarks_")
.Build();
// Consoler logger has a major impact on perf results, so do not use
// default builder.
return new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseStartup<Startup>();
var webHostBuilder = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel();
var scenario = config["scenarios"]?.ToLower();
if (scenario == "plaintextdispatcher")
{
webHostBuilder.UseStartup<StartupUsingDispatcher>();
// for testing
webHostBuilder.UseSetting("Startup", nameof(StartupUsingDispatcher));
}
else if (scenario == "plaintextrouting")
{
webHostBuilder.UseStartup<StartupUsingRouting>();
// for testing
webHostBuilder.UseSetting("Startup", nameof(StartupUsingRouting));
}
else
{
throw new InvalidOperationException(
$"Invalid scenario '{scenario}'. Allowed scenarios are PlaintextDispatcher and PlaintextRouting");
}
return webHostBuilder;
}
}
}

View File

@ -0,0 +1,50 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Matchers;
using Microsoft.Extensions.DependencyInjection;
namespace Benchmarks
{
public class StartupUsingDispatcher
{
private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
services.AddDispatcher(options =>
{
options.DataSources.Add(new DefaultEndpointDataSource(new[]
{
new MatcherEndpoint(
invoker: (next) => (httpContext) =>
{
var response = httpContext.Response;
var payloadLength = _helloWorldPayload.Length;
response.StatusCode = 200;
response.ContentType = "text/plain";
response.ContentLength = payloadLength;
return response.Body.WriteAsync(_helloWorldPayload, 0, payloadLength);
},
template: "/plaintext",
values: new { },
order: 0,
metadata: EndpointMetadataCollection.Empty,
displayName: "Plaintext"),
}));
});
}
public void Configure(IApplicationBuilder app)
{
app.UseDispatcher();
app.UseEndpoint();
}
}
}

View File

@ -8,7 +8,7 @@ using System.Text;
namespace Benchmarks
{
public class Startup
public class StartupUsingRouting
{
private static readonly byte[] _helloWorldPayload = Encoding.UTF8.GetBytes("Hello, World!");

View File

@ -15,5 +15,8 @@
},
"PlaintextRouting": {
"Path": "/plaintext"
},
"PlaintextDispatcher": {
"Path": "/plaintext"
}
}
}

View File

@ -17,6 +17,8 @@
<MicrosoftAspNetCoreTestHostPackageVersion>2.2.0-preview1-34326</MicrosoftAspNetCoreTestHostPackageVersion>
<MicrosoftAspNetCoreTestingPackageVersion>2.2.0-preview1-34326</MicrosoftAspNetCoreTestingPackageVersion>
<MicrosoftExtensionsConfigurationPackageVersion>2.2.0-preview1-34326</MicrosoftExtensionsConfigurationPackageVersion>
<MicrosoftExtensionsConfigurationCommandLinePackageVersion>2.2.0-preview1-34326</MicrosoftExtensionsConfigurationCommandLinePackageVersion>
<MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>2.2.0-preview1-34326</MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>
<MicrosoftExtensionsDependencyInjectionAbstractionsPackageVersion>2.2.0-preview1-34326</MicrosoftExtensionsDependencyInjectionAbstractionsPackageVersion>
<MicrosoftExtensionsDependencyInjectionPackageVersion>2.2.0-preview1-34326</MicrosoftExtensionsDependencyInjectionPackageVersion>
<MicrosoftExtensionsHashCodeCombinerSourcesPackageVersion>2.2.0-preview1-34326</MicrosoftExtensionsHashCodeCombinerSourcesPackageVersion>

View File

@ -0,0 +1,60 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#if NETCOREAPP2_2
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.TestHost;
using Xunit;
namespace Microsoft.AspNetCore.Routing.FunctionalTests
{
public class DispatcherTest : IDisposable
{
private readonly HttpClient _client;
private readonly TestServer _testServer;
public DispatcherTest()
{
// This switch and value are set by benchmark server when running the app for profiling.
var args = new[] { "--scenarios", "PlaintextDispatcher" };
var webHostBuilder = Benchmarks.Program.GetWebHostBuilder(args);
// Make sure we are using the right startup
var startupName = webHostBuilder.GetSetting("Startup");
Assert.Equal(nameof(Benchmarks.StartupUsingDispatcher), startupName);
_testServer = new TestServer(webHostBuilder);
_client = _testServer.CreateClient();
_client.BaseAddress = new Uri("http://localhost");
}
[Fact]
public async Task RouteEndpoint_ReturnsPlaintextResponse()
{
// Arrange
var expectedContentType = "text/plain";
var expectedContent = "Hello, World!";
// Act
var response = await _client.GetAsync("/plaintext");
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
Assert.NotNull(response.Content.Headers.ContentType);
Assert.Equal(expectedContentType, response.Content.Headers.ContentType.MediaType);
var actualContent = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedContent, actualContent);
}
public void Dispose()
{
_testServer.Dispose();
_client.Dispose();
}
}
}
#endif

View File

@ -11,14 +11,21 @@ using Xunit;
namespace Microsoft.AspNetCore.Routing.FunctionalTests
{
public class BenchmarksTest : IDisposable
public class RoutingTest : IDisposable
{
private readonly HttpClient _client;
private readonly TestServer _testServer;
public BenchmarksTest()
public RoutingTest()
{
var webHostBuilder = Benchmarks.Program.GetWebHostBuilder(args: null);
// This switch and value are set by benchmark server when running the app for profiling.
var args = new[] { "--scenarios", "PlaintextRouting" };
var webHostBuilder = Benchmarks.Program.GetWebHostBuilder(args);
// Make sure we are using the right startup
var startupName = webHostBuilder.GetSetting("Startup");
Assert.Equal(nameof(Benchmarks.StartupUsingRouting), startupName);
_testServer = new TestServer(webHostBuilder);
_client = _testServer.CreateClient();
_client.BaseAddress = new Uri("http://localhost");