-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6b7b27
commit f0d626c
Showing
5 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
test/Serilog.Extensions.Logging.Benchmarks/LogEventConstructionBenchmark.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2019 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Running; | ||
using Microsoft.Extensions.Logging; | ||
using IMelLogger = Microsoft.Extensions.Logging.ILogger; | ||
using Serilog.Events; | ||
using Serilog.Extensions.Logging.Benchmarks.Support; | ||
using Xunit; | ||
|
||
namespace Serilog.Extensions.Logging.Benchmarks | ||
{ | ||
[MemoryDiagnoser] | ||
public class LogEventConstructionBenchmark | ||
{ | ||
readonly IMelLogger _melLogger; | ||
readonly ILogger _serilogContextualLogger; | ||
readonly CapturingSink _sink; | ||
const int LowId = 10, HighId = 101; | ||
const string Template = "This is an event"; | ||
|
||
public LogEventConstructionBenchmark() | ||
{ | ||
_sink = new CapturingSink(); | ||
var underlyingLogger = new LoggerConfiguration().WriteTo.Sink(_sink).CreateLogger(); | ||
_serilogContextualLogger = underlyingLogger.ForContext<LogEventConstructionBenchmark>(); | ||
_melLogger = new SerilogLoggerProvider(underlyingLogger).CreateLogger(GetType().FullName); | ||
} | ||
|
||
static void VerifyEventId(LogEvent evt, int? expectedId) | ||
{ | ||
if (evt == null) throw new ArgumentNullException(nameof(evt)); | ||
if (expectedId == null) | ||
{ | ||
Assert.False(evt.Properties.TryGetValue("EventId", out _)); | ||
} | ||
else | ||
{ | ||
Assert.True(evt.Properties.TryGetValue("EventId", out var eventIdValue)); | ||
var structure = Assert.IsType<StructureValue>(eventIdValue); | ||
var idValue = Assert.Single(structure.Properties, p => p.Name == "Id")?.Value; | ||
var scalar = Assert.IsType<ScalarValue>(idValue); | ||
Assert.Equal(expectedId.Value, scalar.Value); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Verify() | ||
{ | ||
VerifyEventId(Native(), null); | ||
VerifyEventId(NoId(), null); | ||
VerifyEventId(LowNumbered(), LowId); | ||
VerifyEventId(HighNumbered(), HighId); | ||
} | ||
|
||
[Fact] | ||
public void Benchmark() | ||
{ | ||
BenchmarkRunner.Run<LogEventConstructionBenchmark>(); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public LogEvent Native() | ||
{ | ||
_serilogContextualLogger.Information(Template); | ||
return _sink.Collect(); | ||
} | ||
|
||
[Benchmark] | ||
public LogEvent NoId() | ||
{ | ||
_melLogger.LogInformation(Template); | ||
return _sink.Collect(); | ||
} | ||
|
||
[Benchmark] | ||
public LogEvent LowNumbered() | ||
{ | ||
_melLogger.LogInformation(LowId, Template); | ||
return _sink.Collect(); | ||
} | ||
|
||
[Benchmark] | ||
public LogEvent HighNumbered() | ||
{ | ||
_melLogger.LogInformation(HighId, Template); | ||
return _sink.Collect(); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
test/Serilog.Extensions.Logging.Benchmarks/Serilog.Extensions.Logging.Benchmarks.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.2</TargetFrameworks> | ||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Serilog.Extensions.Logging\Serilog.Extensions.Logging.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
|
||
</Project> |
36 changes: 36 additions & 0 deletions
36
test/Serilog.Extensions.Logging.Benchmarks/Support/CapturingSink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2019 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Extensions.Logging.Benchmarks.Support | ||
{ | ||
class CapturingSink : ILogEventSink | ||
{ | ||
LogEvent _emitted; | ||
|
||
public void Emit(LogEvent logEvent) | ||
{ | ||
_emitted = logEvent; | ||
} | ||
|
||
public LogEvent Collect() | ||
{ | ||
var collected = _emitted; | ||
_emitted = null; | ||
return collected; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters