Skip to content

Commit 40a2a2d

Browse files
committed
Add some basic benchmarks
1 parent 8301b4e commit 40a2a2d

File tree

5 files changed

+175
-3
lines changed

5 files changed

+175
-3
lines changed

serilog-extensions-logging.sln

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.10
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29209.62
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}"
77
EndProject
@@ -24,6 +24,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{9C21B9
2424
assets\Serilog.snk = assets\Serilog.snk
2525
EndProjectSection
2626
EndProject
27+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Extensions.Logging.Benchmarks", "test\Serilog.Extensions.Logging.Benchmarks\Serilog.Extensions.Logging.Benchmarks.csproj", "{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}"
28+
EndProject
2729
Global
2830
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2931
Debug|Any CPU = Debug|Any CPU
@@ -42,6 +44,10 @@ Global
4244
{65357FBC-9BC4-466D-B621-1C3A19BC2A78}.Debug|Any CPU.Build.0 = Debug|Any CPU
4345
{65357FBC-9BC4-466D-B621-1C3A19BC2A78}.Release|Any CPU.ActiveCfg = Release|Any CPU
4446
{65357FBC-9BC4-466D-B621-1C3A19BC2A78}.Release|Any CPU.Build.0 = Release|Any CPU
47+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2}.Release|Any CPU.Build.0 = Release|Any CPU
4551
EndGlobalSection
4652
GlobalSection(SolutionProperties) = preSolution
4753
HideSolutionNode = FALSE
@@ -50,6 +56,7 @@ Global
5056
{903CD13A-D54B-4CEC-A55F-E22AE3D93B3B} = {A1893BD1-333D-4DFE-A0F0-DDBB2FE526E0}
5157
{37EADF84-5E41-4224-A194-1E3299DCD0B8} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1}
5258
{65357FBC-9BC4-466D-B621-1C3A19BC2A78} = {F2407211-6043-439C-8E06-3641634332E7}
59+
{6D5986FF-EECD-4E75-8BC6-A5F78AB549B2} = {E30F638E-BBBE-4AD1-93CE-48CC69CFEFE1}
5360
EndGlobalSection
5461
GlobalSection(ExtensibilityGlobals) = postSolution
5562
SolutionGuid = {811E61C5-3871-4633-AFAE-B35B619C8A10}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2019 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using BenchmarkDotNet.Attributes;
17+
using BenchmarkDotNet.Running;
18+
using Microsoft.Extensions.Logging;
19+
using IMelLogger = Microsoft.Extensions.Logging.ILogger;
20+
using Serilog.Events;
21+
using Serilog.Extensions.Logging.Benchmarks.Support;
22+
using Xunit;
23+
24+
namespace Serilog.Extensions.Logging.Benchmarks
25+
{
26+
[MemoryDiagnoser]
27+
public class LogEventConstructionBenchmark
28+
{
29+
readonly IMelLogger _melLogger;
30+
readonly ILogger _serilogContextualLogger;
31+
readonly CapturingSink _sink;
32+
const int LowId = 10, HighId = 101;
33+
const string Template = "This is an event";
34+
35+
public LogEventConstructionBenchmark()
36+
{
37+
_sink = new CapturingSink();
38+
var underlyingLogger = new LoggerConfiguration().WriteTo.Sink(_sink).CreateLogger();
39+
_serilogContextualLogger = underlyingLogger.ForContext<LogEventConstructionBenchmark>();
40+
_melLogger = new SerilogLoggerProvider(underlyingLogger).CreateLogger(GetType().FullName);
41+
}
42+
43+
static void VerifyEventId(LogEvent evt, int? expectedId)
44+
{
45+
if (evt == null) throw new ArgumentNullException(nameof(evt));
46+
if (expectedId == null)
47+
{
48+
Assert.False(evt.Properties.TryGetValue("EventId", out _));
49+
}
50+
else
51+
{
52+
Assert.True(evt.Properties.TryGetValue("EventId", out var eventIdValue));
53+
var structure = Assert.IsType<StructureValue>(eventIdValue);
54+
var idValue = Assert.Single(structure.Properties, p => p.Name == "Id")?.Value;
55+
var scalar = Assert.IsType<ScalarValue>(idValue);
56+
Assert.Equal(expectedId.Value, scalar.Value);
57+
}
58+
}
59+
60+
[Fact]
61+
public void Verify()
62+
{
63+
VerifyEventId(Native(), null);
64+
VerifyEventId(NoId(), null);
65+
VerifyEventId(LowNumbered(), LowId);
66+
VerifyEventId(HighNumbered(), HighId);
67+
}
68+
69+
[Fact]
70+
public void Benchmark()
71+
{
72+
BenchmarkRunner.Run<LogEventConstructionBenchmark>();
73+
}
74+
75+
[Benchmark(Baseline = true)]
76+
public LogEvent Native()
77+
{
78+
_serilogContextualLogger.Information(Template);
79+
return _sink.Collect();
80+
}
81+
82+
[Benchmark]
83+
public LogEvent NoId()
84+
{
85+
_melLogger.LogInformation(Template);
86+
return _sink.Collect();
87+
}
88+
89+
[Benchmark]
90+
public LogEvent LowNumbered()
91+
{
92+
_melLogger.LogInformation(LowId, Template);
93+
return _sink.Collect();
94+
}
95+
96+
[Benchmark]
97+
public LogEvent HighNumbered()
98+
{
99+
_melLogger.LogInformation(HighId, Template);
100+
return _sink.Collect();
101+
}
102+
}
103+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp2.2</TargetFrameworks>
5+
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
15+
<PrivateAssets>all</PrivateAssets>
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
18+
<PackageReference Include="xunit" Version="2.4.1" />
19+
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2019 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Serilog.Core;
16+
using Serilog.Events;
17+
18+
namespace Serilog.Extensions.Logging.Benchmarks.Support
19+
{
20+
class CapturingSink : ILogEventSink
21+
{
22+
LogEvent _emitted;
23+
24+
public void Emit(LogEvent logEvent)
25+
{
26+
_emitted = logEvent;
27+
}
28+
29+
public LogEvent Collect()
30+
{
31+
var collected = _emitted;
32+
_emitted = null;
33+
return collected;
34+
}
35+
}
36+
}

test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static Tuple<SerilogLogger, SerilogSink> SetUp(LogLevel logLevel)
2525

2626
var serilogLogger = new LoggerConfiguration()
2727
.WriteTo.Sink(sink)
28-
.MinimumLevel.Is(LevelMapping.ToSerilogLevel(logLevel))
28+
.MinimumLevel.Is(LevelConvert.ToSerilogLevel(logLevel))
2929
.CreateLogger();
3030

3131
var provider = new SerilogLoggerProvider(serilogLogger);

0 commit comments

Comments
 (0)