Skip to content

Commit

Permalink
Add Logger Optimizations Benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminMichaelis committed Nov 28, 2023
1 parent 6f1a248 commit 36b8213
Show file tree
Hide file tree
Showing 9 changed files with 726 additions and 0 deletions.
480 changes: 480 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions Benchmarks.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34310.174
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmarks", "Benchmarks\Benchmarks.csproj", "{B6370DCB-BAA7-4D24-B08B-0F1A265775E0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C8E97198-3B25-4EAA-BD06-0D5016AD3D3B}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
NuGet.config = NuGet.config
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B6370DCB-BAA7-4D24-B08B-0F1A265775E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6370DCB-BAA7-4D24-B08B-0F1A265775E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6370DCB-BAA7-4D24-B08B-0F1A265775E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6370DCB-BAA7-4D24-B08B-0F1A265775E0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {60018FCA-7458-420B-BDB1-7F7AB75E4B88}
EndGlobalSection
EndGlobal
103 changes: 103 additions & 0 deletions Benchmarks/BenchmarkClasses/LoggerVsLoggerMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.Logging.Abstractions.Internal;
using Microsoft.Extensions.Logging;

namespace BenjaminMichaelis.Benchmarks;

[MemoryDiagnoser]
public class LoggerVsLoggerMessage
{
private readonly LoggerFactory _factory;
private readonly ILogger _logger;
private readonly Random _random;
private readonly Action<ILogger, Guid, int, double, Exception> _logMessage;

public LoggerVsLoggerMessage()
{
_factory = new LoggerFactory();
_factory.AddProvider(new TestLogProvider());
_logger = _factory.CreateLogger("benchmark");

_logMessage
= LoggerMessage.Define<Guid, int, double>(LogLevel.Information, new EventId(0),
"CorrelationID {CorrelationId}, Arg1: {Arg1}, Arg2: {Arg2}");

_random = new Random();
}

[Benchmark]
public void LogDirectly()
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.Log(LogLevel.Information, "CorrelationID {firstGuid}, Arg1: {randomNext}, Arg2: {randomDouble}", Guid.NewGuid(), _random.Next(), _random.NextDouble());
}
}

[Benchmark]
public void LogFormat()
{
if (_logger.IsEnabled(LogLevel.Information))
{
string msg =
$"CorrelationID {Guid.NewGuid()}, Arg1: {_random.Next()}, Arg2: {_random.NextDouble()}";
_logger.Log(LogLevel.Information, 0, msg, null, Format);
}

string? Format(object state, Exception ex)
{
return state.ToString();
}
}

[Benchmark]
public void LogLoggerMessage()
{
_logMessage(_logger, Guid.NewGuid(), _random.Next(), _random.NextDouble(), null);
}

[Benchmark]
public void LogLoggerInterpolatedStringMessage()
{
_logger.LogInformation($"CorrelationID {Guid.NewGuid()}, Arg1: {_random.Next()}, Arg2: {_random.NextDouble()}, Arg3: {null}");
}

[Benchmark]
public void LogLoggerInterpolatedStringMessageDirectly()
{
_logger.LogInformation("CorrelationID {firstGuid}, Arg1: {randomNext}, Arg2: {randomDouble}, Arg3: {null}", Guid.NewGuid(), _random.Next(), _random.NextDouble(), null);
}
}

internal class TestLogProvider : ILoggerProvider
{
public void Dispose()
{
}

public ILogger CreateLogger(string categoryName)
{
return new TestLogger();
}

public class TestLogger : ILogger
{
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var message = formatter(state, exception);

if (!string.IsNullOrEmpty(message) && exception != null)
{
throw new NotImplementedException();
}
}

public bool IsEnabled(LogLevel logLevel)
{
return true;
}

public IDisposable BeginScope<TState>(TState state) => NullScope.Instance;
}

}
24 changes: 24 additions & 0 deletions Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<Configuration>Release</Configuration>
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="BenchmarkDotNet.Annotations" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" />
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;

namespace BenjaminMichaelis.Benchmarks;

public class Program
{
public static void Main(string[] args)
{
var config = DefaultConfig.Instance;
var summary = BenchmarkRunner.Run<LoggerVsLoggerMessage>(config, args);

// Use this to select benchmarks from the console:
// var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
}
9 changes: 9 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
</PropertyGroup>

</Project>
23 changes: 23 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.13.10" />
<PackageVersion Include="BenchmarkDotNet.Annotations" Version="0.13.10" />
<PackageVersion Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.10"/>
<PackageVersion Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageVersion>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Moq" Version="4.20.69" />
<PackageVersion Include="Moq.AutoMock" Version="3.5.0" />
<PackageVersion Include="xunit" Version="2.6.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageVersion>
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>

<packageSourceMapping>
<packageSource key="nuget.org">
<package pattern="*" />
</packageSource>
</packageSourceMapping>
</configuration>
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Benchmark app template

This template creates a [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet) solution, along with a console project with unit tests.

The point of this is so that you can easily create a benchmark project to test out different algorithms, or to test out different ways of doing things.

## Startup
- To run the benchmarks, run `dotnet run -c Release` in the `BenchmarkApp` folder.

## Template

Create a new app in your current directory by running.

```cli
dotnet new bmichaelis.quickstart.benchmarkconsole
```

### Parameters

[Default template options](https://learn.microsoft.com/dotnet/core/tools/dotnet-new#options)

## Key Features

- Should just work out of the box. Not meant to be the perfect project setup to start with, but rather a quick way to get started with benchmarking and testing out different algorithms or such.

0 comments on commit 36b8213

Please sign in to comment.