-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turn off a bunch of optional features in the benchmark (#2474)
* Use a minimal set of ASP.NET features in the benchmark * Disable UseDefaultForwarders
- Loading branch information
Showing
4 changed files
with
85 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "" | ||
} |