Skip to content

Commit

Permalink
Add ZLogger to latency benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Jan 7, 2024
1 parent eff85fe commit 94af52a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
84 changes: 74 additions & 10 deletions src/ZeroLog.Benchmarks/LatencyTests/LatencyBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
using System;
using System.IO;
using BenchmarkDotNet.Attributes;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Serilog.Events;
using ZeroLog;
using ZeroLog.Benchmarks.Tools;
using ZeroLog.Configuration;
using ZeroLog.Tests;
using ZLogger;

namespace ZeroLog.Benchmarks.LatencyTests;
// ReSharper disable once CheckNamespace
namespace Benchmarks;

[MemoryDiagnoser]
[AllStatisticsColumn]
[SimpleJob(iterationCount: 100, invocationCount: 8 * 1024)]
public class LatencyBenchmarks
[SimpleJob(iterationCount: 64, invocationCount: 128)]
public partial class LatencyBenchmarks
{
private const int _operationCount = 8 * 1024;

private static readonly string _text = "dude";
private static readonly DateTime _date = DateTime.UtcNow;
private static readonly int _number = 42;
Expand All @@ -20,24 +28,30 @@ public class LatencyBenchmarks
private TestAppender _zeroLogTestAppender;
private Log _zeroLogLogger;

// ZLogger
private ILoggerFactory _zLoggerFactory;
private ILogger _zLoggerLogger;

// Serilog
private SerilogTestSink _serilogTestSink;
private Serilog.Core.Logger _serilogLogger;

[ParamsAllValues]
public bool Enabled { get; set; }
public bool Enabled { get; [UsedImplicitly] set; }

[GlobalSetup]
public void Setup()
{
SetupZeroLog();
SetupZLogger();
SetupSerilog();
}

[GlobalCleanup]
public void Cleanup()
{
TearDownZeroLog();
TearDownZLogger();
TearDownSerilog();
}

Expand All @@ -51,29 +65,73 @@ private void SetupZeroLog()

LogManager.Initialize(new ZeroLogConfiguration
{
LogMessagePoolSize = 32 * 1024,
LogMessagePoolSize = _operationCount,
RootLogger =
{
Level = Enabled ? LogLevel.Info : LogLevel.Warn,
Level = Enabled ? global::ZeroLog.LogLevel.Info : global::ZeroLog.LogLevel.Warn,
LogMessagePoolExhaustionStrategy = LogMessagePoolExhaustionStrategy.WaitUntilAvailable,
Appenders = { _zeroLogTestAppender }
}
});

_zeroLogLogger = LogManager.GetLogger(nameof(ZeroLog));

if (_zeroLogLogger.IsInfoEnabled != Enabled)
throw new InvalidOperationException();
}

private void TearDownZeroLog()
=> LogManager.Shutdown();

[Benchmark(Baseline = true)]
[Benchmark(Baseline = true, OperationsPerInvoke = _operationCount)]
public void ZeroLog()
=> _zeroLogLogger.Info($"Hi {_text} ! It's {_date:HH:mm:ss}, and the message is #{_number}");
{
for (var i = 0; i < _operationCount; ++i)
_zeroLogLogger.Info($"Hi {_text}! It's {_date:HH:mm:ss}, and the message is #{_number}");
}

[IterationCleanup(Target = nameof(ZeroLog))]
public void CleanupZeroLogIteration()
=> LogManager.Flush();

//
// ZLogger
//

private void SetupZLogger()
{
_zLoggerFactory = LoggerFactory.Create(logging =>
{
logging.AddZLoggerStream(Stream.Null);
logging.SetMinimumLevel(Enabled ? Microsoft.Extensions.Logging.LogLevel.Information : Microsoft.Extensions.Logging.LogLevel.Warning);
});

_zLoggerLogger = _zLoggerFactory.CreateLogger(nameof(ZLogger));

if (_zLoggerLogger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information) != Enabled)
throw new InvalidOperationException();
}

private void TearDownZLogger()
=> _zLoggerFactory.Dispose();

[Benchmark(OperationsPerInvoke = _operationCount)]
public void ZLogger()
{
for (var i = 0; i < _operationCount; ++i)
_zLoggerLogger.ZLogInformation($"Hi {_text}! It's {_date:HH:mm:ss}, and the message is #{_number}");
}

[Benchmark(OperationsPerInvoke = _operationCount)]
public void ZLoggerGenerated()
{
for (var i = 0; i < _operationCount; ++i)
ZLoggerGenerated(_zLoggerLogger, _text, _date, _number);
}

[ZLoggerMessage(Microsoft.Extensions.Logging.LogLevel.Information, "Hi {name}! It's {hour:HH:mm:ss}, and the message is #{number}")]
private static partial void ZLoggerGenerated(ILogger logger, string name, DateTime hour, int number);

//
// Serilog
//
Expand All @@ -86,12 +144,18 @@ private void SetupSerilog()
.WriteTo.Sink(_serilogTestSink)
.MinimumLevel.Is(Enabled ? LogEventLevel.Information : LogEventLevel.Warning)
.CreateLogger();

if (_serilogLogger.IsEnabled(LogEventLevel.Information) != Enabled)
throw new InvalidOperationException();
}

private void TearDownSerilog()
=> _serilogLogger.Dispose();

[Benchmark]
[Benchmark(OperationsPerInvoke = _operationCount)]
public void Serilog()
=> _serilogLogger.Information("Hi {name} ! It's {hour:HH:mm:ss}, and the message is #{number}", _text, _date, _number);
{
for (var i = 0; i < _operationCount; ++i)
_serilogLogger.Information("Hi {name}! It's {hour:HH:mm:ss}, and the message is #{number}", _text, _date, _number);
}
}
1 change: 1 addition & 0 deletions src/ZeroLog.Benchmarks/ZeroLog.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="System.Net.Sockets" Version="4.3.0" />
<PackageReference Include="ZLogger" Version="2.1.0" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/ZeroLog.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_BEFORE_ARROW_WITH_EXPRESSIONS/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Constants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
Expand Down

0 comments on commit 94af52a

Please sign in to comment.