Skip to content

Commit

Permalink
Turn off a bunch of optional features in the benchmark (#2474)
Browse files Browse the repository at this point in the history
* Use a minimal set of ASP.NET features in the benchmark

* Disable UseDefaultForwarders
  • Loading branch information
MihaZupan committed Apr 23, 2024
1 parent ed1cee2 commit f84b7d7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 54 deletions.
113 changes: 82 additions & 31 deletions testassets/BenchmarkApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,109 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Generic;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Crank.EventSources;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Crank.EventSources;
using Microsoft.Extensions.Logging;
using Yarp.ReverseProxy.Forwarder;
using Yarp.ReverseProxy.Transforms.Builder;

BenchmarksEventSource.MeasureAspNetVersion();
BenchmarksEventSource.MeasureNetCoreAppVersion();

var config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.AddCommandLine(args)
.AddJsonFile("appsettings.json", optional: true)
.Build();

var builder = new WebHostBuilder()
.ConfigureLogging(loggerFactory =>
{
if (Enum.TryParse(config["LogLevel"], out LogLevel logLevel))
{
Console.WriteLine($"Console Logging enabled with level '{logLevel}'");
loggerFactory.AddConsole().SetMinimumLevel(logLevel);
}
})
.UseKestrel((context, kestrelOptions) =>
{
kestrelOptions.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.ServerCertificate = new X509Certificate2(Path.Combine(context.HostingEnvironment.ContentRootPath, "testCert.pfx"), "testPassword");
});
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseConfiguration(config)
.ConfigureServices(services =>
{
services.AddHttpForwarder();
})
;

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel((context, kestrelOptions) =>
builder.Configure(app =>
{
kestrelOptions.ConfigureHttpsDefaults(httpsOptions =>
var forwarder = app.ApplicationServices.GetRequiredService<IHttpForwarder>();
var clusterUrl = GetClusterUrl();
var httpClient = new HttpMessageInvoker(CreateHandler());
var transformer = CreateHttpTransformer(app);
app.Run(async context =>
{
httpsOptions.ServerCertificate = new X509Certificate2(Path.Combine(context.HostingEnvironment.ContentRootPath, "testCert.pfx"), "testPassword");
await forwarder.SendAsync(context, clusterUrl, httpClient, ForwarderRequestConfig.Empty, transformer);
});
});

var clusterUrls = builder.Configuration["clusterUrls"];
builder.Build().Run();

if (string.IsNullOrWhiteSpace(clusterUrls))
string GetClusterUrl()
{
throw new ArgumentException("--clusterUrls is required");
}
var clusterUrls = config["clusterUrls"];

var configDictionary = new Dictionary<string, string>
{
{ "Routes:route:ClusterId", "cluster" },
{ "Routes:route:Match:Path", "/{**catchall}" },
{ "Clusters:cluster:HttpClient:DangerousAcceptAnyServerCertificate", "true" },
};
if (string.IsNullOrWhiteSpace(clusterUrls))
{
throw new ArgumentException("--clusterUrls is required");
}

var clusterCount = 0;
foreach (var clusterUrl in clusterUrls.Split(';'))
{
configDictionary.Add($"Clusters:cluster:Destinations:destination{clusterCount++}:Address", clusterUrl);
}
var clusterUrl = clusterUrls.Split(';')[0];

var proxyConfig = new ConfigurationBuilder().AddInMemoryCollection(configDictionary).Build();
Console.WriteLine($"ClusterUrl: {clusterUrl}");

builder.Services.AddReverseProxy().LoadFromConfig(proxyConfig);
return clusterUrl;
}

var app = builder.Build();
static SocketsHttpHandler CreateHandler()
{
var handler = new SocketsHttpHandler
{
UseProxy = false,
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.None,
UseCookies = false,
EnableMultipleHttp2Connections = true,
ActivityHeadersPropagator = new ReverseProxyPropagator(DistributedContextPropagator.Current),
ConnectTimeout = TimeSpan.FromSeconds(15),
};

BenchmarksEventSource.MeasureAspNetVersion();
BenchmarksEventSource.MeasureNetCoreAppVersion();
handler.SslOptions.RemoteCertificateValidationCallback = delegate { return true; };

// Register the reverse proxy routes
app.MapReverseProxy(builder =>
return handler;
}

static HttpTransformer CreateHttpTransformer(IApplicationBuilder app)
{
// Skip SessionAffinity, LoadBalancing and PassiveHealthChecks
});
var transformBuilder = app.ApplicationServices.GetRequiredService<ITransformBuilder>();

app.Run();
return transformBuilder.Create(context =>
{
context.UseDefaultForwarders = false;
});
}
2 changes: 1 addition & 1 deletion testassets/BenchmarkApp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"BenchmarkApp": {
"commandName": "Project",
"commandLineArgs": "--urls http://localhost:5000 --clusterUrls http://localhost:10010",
"commandLineArgs": "--urls http://localhost:5000 --clusterUrls http://httpbin.org",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
9 changes: 0 additions & 9 deletions testassets/BenchmarkApp/appsettings.Development.json

This file was deleted.

15 changes: 2 additions & 13 deletions testassets/BenchmarkApp/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"Microsoft.AspNetCore.Hosting.Diagnostics": "None"
},
"EventLog": {
"LogLevel": {
"Default": "None"
}
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"LogLevel": ""
}

0 comments on commit f84b7d7

Please sign in to comment.