Skip to content

Commit 6cd5ff7

Browse files
committed
Quick-and-dirty latency benchmark.
1 parent 2173150 commit 6cd5ff7

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

src/Serilog.Sinks.Async.PerformanceTests/Benchmarks.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class Benchmarks
1010
public void Benchmark()
1111
{
1212
BenchmarkRunner.Run<ThroughputBenchmark>();
13+
BenchmarkRunner.Run<LatencyBenchmark>();
1314
}
1415
}
1516
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using Serilog.Core;
4+
using Serilog.Events;
5+
using Serilog.Parsing;
6+
7+
namespace Serilog.Sinks.Async.PerformanceTests
8+
{
9+
public class LatencyBenchmark
10+
{
11+
private const int Count = 10000;
12+
13+
private readonly LogEvent _evt = new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null,
14+
new MessageTemplate(new[] {new TextToken("Hello")}), new LogEventProperty[0]);
15+
16+
private Logger _syncLogger, _asyncLogger, _async2Logger;
17+
18+
[Setup]
19+
public void Reset()
20+
{
21+
_syncLogger?.Dispose();
22+
_asyncLogger?.Dispose();
23+
_async2Logger?.Dispose();
24+
25+
_syncLogger = new LoggerConfiguration()
26+
.WriteTo.Sink(new SilentSink())
27+
.CreateLogger();
28+
29+
_asyncLogger = new LoggerConfiguration()
30+
.WriteTo.Async(a => a.Sink(new SilentSink()))
31+
.CreateLogger();
32+
33+
_async2Logger = new LoggerConfiguration()
34+
.WriteTo.Async2(a => a.Sink(new SilentSink()))
35+
.CreateLogger();
36+
}
37+
38+
[Benchmark(Baseline = true)]
39+
public void Sync()
40+
{
41+
for (var i = 0; i < Count; ++i)
42+
{
43+
_syncLogger.Write(_evt);
44+
}
45+
}
46+
47+
[Benchmark]
48+
public void Async()
49+
{
50+
for (var i = 0; i < Count; ++i)
51+
{
52+
_asyncLogger.Write(_evt);
53+
}
54+
}
55+
56+
[Benchmark]
57+
public void Async2()
58+
{
59+
for (var i = 0; i < Count; ++i)
60+
{
61+
_async2Logger.Write(_evt);
62+
}
63+
}
64+
}
65+
}

src/Serilog.Sinks.Async.PerformanceTests/Serilog.Sinks.Async.PerformanceTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
<Compile Include="Benchmarks.cs" />
8686
<Compile Include="Properties\AssemblyInfo.cs" />
8787
<Compile Include="SignallingSink.cs" />
88+
<Compile Include="LatencyBenchmark.cs" />
89+
<Compile Include="SilentSink.cs" />
8890
<Compile Include="ThroughputBenchmark.cs" />
8991
</ItemGroup>
9092
<ItemGroup>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Serilog.Core;
2+
using Serilog.Events;
3+
4+
namespace Serilog.Sinks.Async.PerformanceTests
5+
{
6+
public class SilentSink : ILogEventSink
7+
{
8+
public void Emit(LogEvent logEvent)
9+
{
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)