Skip to content

Commit 6c393b1

Browse files
author
Stanislav Perekrestov
committed
Adds benchmarks for event batch creation
1 parent 51ca3f7 commit 6c393b1

File tree

6 files changed

+101
-2
lines changed

6 files changed

+101
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Jobs;
3+
using Serilog.Events;
4+
using Serilog.Formatting.Json;
5+
using Serilog.Parsing;
6+
7+
namespace Serilog.Sinks.GoogleCloudLogging.Benchmark;
8+
9+
[MemoryDiagnoser]
10+
[SimpleJob(runtimeMoniker: RuntimeMoniker.Net90, baseline: false)]
11+
[SimpleJob(runtimeMoniker: RuntimeMoniker.Net80, baseline: true)]
12+
public class LogEventEmitBenchmark
13+
{
14+
private readonly GoogleCloudLoggingSink _sink = new("project_test", new JsonFormatter());
15+
16+
[Benchmark]
17+
[ArgumentsSource(nameof(Data))]
18+
public void CreateEventsBatch(IReadOnlyCollection<LogEvent> events)
19+
{
20+
_sink.CreateEventsBatch(events);
21+
}
22+
23+
public IEnumerable<IReadOnlyCollection<LogEvent>> Data()
24+
{
25+
var timeStamp = DateTimeOffset.UtcNow;
26+
var mtParser = new MessageTemplateParser();
27+
var mt = mtParser.Parse("Hello {@World}");
28+
return
29+
[
30+
[
31+
new LogEvent(timestamp: timeStamp,
32+
level: LogEventLevel.Information,
33+
exception: null,
34+
messageTemplate: mt,
35+
properties: [new LogEventProperty("World", new ScalarValue("Hello World!"))])
36+
],
37+
Enumerable.Range(1, 10)
38+
.Select(t => new LogEvent(
39+
timestamp: timeStamp.AddMilliseconds(t),
40+
level: LogEventLevel.Information,
41+
exception: null,
42+
messageTemplate: mt,
43+
properties: [new LogEventProperty("World", new ScalarValue("Hello World!"))]))
44+
.ToArray(),
45+
Enumerable.Range(1, 100)
46+
.Select(t => new LogEvent(
47+
timestamp: timeStamp.AddMilliseconds(t),
48+
level: LogEventLevel.Information,
49+
exception: null,
50+
messageTemplate: mt,
51+
properties: [new LogEventProperty("World", new ScalarValue("Hello World!"))]))
52+
.ToArray()
53+
];
54+
}
55+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using BenchmarkDotNet.Running;
4+
using Serilog.Sinks.GoogleCloudLogging.Benchmark;
5+
6+
_ = BenchmarkRunner.Run<LogEventEmitBenchmark>();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
12+
<PackageReference Include="Bogus" Version="35.6.3" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Serilog.Sinks.GoogleCloudLogging\Serilog.Sinks.GoogleCloudLogging.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

src/Serilog.Sinks.GoogleCloudLogging.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestWeb", "TestWeb\TestWeb.
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.GoogleCloudLogging.Test", "Serilog.Sinks.GoogleCloudLogging.Test\Serilog.Sinks.GoogleCloudLogging.Test.csproj", "{858BBD6D-9FF4-4D78-95A7-7139E69FCC55}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Sinks.GoogleCloudLogging.Benchmark", "Serilog.Sinks.GoogleCloudLogging.Benchmark\Serilog.Sinks.GoogleCloudLogging.Benchmark.csproj", "{FD57E195-5EDD-42B5-A722-4043636AA032}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{858BBD6D-9FF4-4D78-95A7-7139E69FCC55}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{858BBD6D-9FF4-4D78-95A7-7139E69FCC55}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{858BBD6D-9FF4-4D78-95A7-7139E69FCC55}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{FD57E195-5EDD-42B5-A722-4043636AA032}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{FD57E195-5EDD-42B5-A722-4043636AA032}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{FD57E195-5EDD-42B5-A722-4043636AA032}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{FD57E195-5EDD-42B5-A722-4043636AA032}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

src/Serilog.Sinks.GoogleCloudLogging/GoogleCloudLoggingSink.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Serilog.Sinks.GoogleCloudLogging;
1717

18-
public class GoogleCloudLoggingSink : IBatchedLogEventSink
18+
public sealed class GoogleCloudLoggingSink : IBatchedLogEventSink
1919
{
2020
private readonly GoogleCloudLoggingSinkOptions _sinkOptions;
2121
private readonly LoggingServiceV2Client _client;
@@ -67,7 +67,15 @@ public GoogleCloudLoggingSink(GoogleCloudLoggingSinkOptions sinkOptions, ITextFo
6767
: new LoggingServiceV2ClientBuilder { JsonCredentials = _sinkOptions.GoogleCredentialJson }.Build();
6868
}
6969

70-
private List<LogEntry> CreateEventsBatch(IReadOnlyCollection<LogEvent> events)
70+
//For testing and benchmarking purposes
71+
internal GoogleCloudLoggingSink(string projectId, ITextFormatter? textFormatter)
72+
{
73+
_projectId = projectId;
74+
_logFormatter = new LogFormatter(textFormatter);;
75+
_sinkOptions = new GoogleCloudLoggingSinkOptions(projectId, useLogCorrelation: true);
76+
}
77+
78+
internal List<LogEntry> CreateEventsBatch(IReadOnlyCollection<LogEvent> events)
7179
{
7280
//writer is used for message template rendering
7381
using var writer = new StringWriter(_stringBuilder);

src/Serilog.Sinks.GoogleCloudLogging/Serilog.Sinks.GoogleCloudLogging.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@
3838
<PackageReference Condition="'$(TargetFramework)' == 'net9.0'" Include="System.Text.Encodings.Web" Version="9.0.0" />
3939
<PackageReference Condition="'$(TargetFramework)' != 'net9.0'" Include="System.Text.Encodings.Web" Version="8.0.0" />
4040
</ItemGroup>
41+
<ItemGroup>
42+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
43+
<_Parameter1>Serilog.Sinks.GoogleCloudLogging.Benchmark</_Parameter1>
44+
</AssemblyAttribute>
45+
</ItemGroup>
4146

4247
</Project>

0 commit comments

Comments
 (0)