[Perf] High Allocator LoggingScope when no loggers #367
Description
Using works with null references, so this is simpler than outlined below; added change to logging to improve efficiency.
Creating a scope is expensive (at benchmarking speeds) and the techempower benchmarks specify in general requirements item 2:
All test implementations must disable all disk logging. For many reasons, we expect all tests will run without writing logs to disk. Most importantly, the volume of requests is sufficiently high to fill up disks even with only a single line written to disk per request. Please disable all forms of disk logging. We recommend but do not require disabling console logging as well.
For fair comparison, only create the logging scope if logging is enabled:
Add LogLevel
of None = 7
?
Change the following from:
var logger = _applicationServices.GetRequiredService<ILogger<HostingEngine>>();
...
var server = ServerFactory.Start(_serverInstance,
async features =>
{
try
{
using (logger.BeginScope("Request Id: {RequestId}", requestIdentifier))
{
contextAccessor.HttpContext = httpContext;
await application(httpContext);
}
}
catch (Exception ex)
{
if (telemetrySource.IsEnabled("Microsoft.AspNet.Hosting.UnhandledException"))
{
telemetrySource.WriteTelemetry("Microsoft.AspNet.Hosting.UnhandledException", new { httpContext = httpContext, exception = ex });
}
throw;
}
To something similar to:
var logger = _applicationServices.GetRequiredService<ILogger<HostingEngine>>();
...
var isLogging = logger.IsEnabled(LogLevel.Critical); // ? not sure about how not logging is checked
var server = ServerFactory.Start(_serverInstance,
async features =>
{
ILogger loggerScope;
try
{
if (isLogging)
{
loggerScope = logger.BeginScope("Request Id: {RequestId}", requestIdentifier);
}
contextAccessor.HttpContext = httpContext;
await application(httpContext);
}
catch (Exception ex)
{
if (telemetrySource.IsEnabled("Microsoft.AspNet.Hosting.UnhandledException"))
{
telemetrySource.WriteTelemetry("Microsoft.AspNet.Hosting.UnhandledException", new { httpContext = httpContext, exception = ex });
}
throw;
}
finally
{
if (loggerScope != null) loggerScope.Dispose();
}
Ensure having no local logging scope doesn't cause issues.