Skip to content

Commit 91fa58a

Browse files
authored
Merge pull request #585 from ckadluba/benchmarks-added
Benchmarks added and improved
2 parents c8d98bd + 89a53f7 commit 91fa58a

File tree

3 files changed

+99
-12
lines changed

3 files changed

+99
-12
lines changed

test/Serilog.Sinks.MSSqlServer.PerformanceTests/Sinks/MSSqlServer/Platform/SqlBulkBatchWriterBenchmarks.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
namespace Serilog.Sinks.MSSqlServer.PerformanceTests.Platform;
1414

1515
[MemoryDiagnoser]
16-
[MaxIterationCount(20)]
17-
public class SqlBulkBatchWriterBenchmarks
16+
[MaxIterationCount(16)]
17+
public class SqlBulkBatchWriterBenchmarks : IDisposable
1818
{
1919
private const string _tableName = "TestTableName";
2020
private const string _schemaName = "TestSchemaName";
21+
private readonly DataTable _dataTable = new(_tableName);
2122
private Mock<ISqlConnectionFactory> _sqlConnectionFactoryMock;
2223
private Mock<ILogEventDataGenerator> _logEventDataGeneratorMock;
2324
private Mock<ISqlConnectionWrapper> _sqlConnectionWrapperMock;
2425
private Mock<ISqlBulkCopyWrapper> _sqlBulkCopyWrapper;
26+
private List<LogEvent> _logEvents;
2527
private SqlBulkBatchWriter _sut;
2628

2729
[GlobalSetup]
@@ -36,22 +38,16 @@ public void Setup()
3638
_sqlConnectionWrapperMock.Setup(c => c.CreateSqlBulkCopy(It.IsAny<bool>(), It.IsAny<string>()))
3739
.Returns(_sqlBulkCopyWrapper.Object);
3840

41+
CreateLogEvents();
42+
3943
_sut = new SqlBulkBatchWriter(_tableName, _schemaName, false, _sqlConnectionFactoryMock.Object,
4044
_logEventDataGeneratorMock.Object);
4145
}
4246

4347
[Benchmark]
4448
public async Task WriteBatch()
4549
{
46-
var logEvents = CreateLogEvents();
47-
using var dataTable = new DataTable(_tableName);
48-
await _sut.WriteBatch(logEvents, dataTable);
49-
}
50-
51-
private static List<LogEvent> CreateLogEvents()
52-
{
53-
var logEvents = new List<LogEvent> { CreateLogEvent(), CreateLogEvent() };
54-
return logEvents;
50+
await _sut.WriteBatch(_logEvents, _dataTable);
5551
}
5652

5753
private static LogEvent CreateLogEvent()
@@ -61,4 +57,20 @@ private static LogEvent CreateLogEvent()
6157
LogEventLevel.Debug, null, new MessageTemplate(new List<MessageTemplateToken>()),
6258
new List<LogEventProperty>());
6359
}
60+
61+
private void CreateLogEvents()
62+
{
63+
_logEvents = new List<LogEvent>();
64+
var eventCount = 500_000;
65+
while (eventCount-- > 0)
66+
{
67+
_logEvents.Add(CreateLogEvent());
68+
}
69+
}
70+
71+
public void Dispose()
72+
{
73+
GC.SuppressFinalize(this);
74+
_dataTable.Dispose();
75+
}
6476
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Threading.Tasks;
5+
using BenchmarkDotNet.Attributes;
6+
using Moq;
7+
using Serilog.Events;
8+
using Serilog.Parsing;
9+
using Serilog.Sinks.MSSqlServer.Output;
10+
using Serilog.Sinks.MSSqlServer.Platform;
11+
using Serilog.Sinks.MSSqlServer.Platform.SqlClient;
12+
13+
namespace Serilog.Sinks.MSSqlServer.PerformanceTests.Platform;
14+
15+
[MemoryDiagnoser]
16+
[MaxIterationCount(16)]
17+
public class SqlInsertStatementWriterBenchmarks : IDisposable
18+
{
19+
private const string _tableName = "TestTableName";
20+
private const string _schemaName = "TestSchemaName";
21+
private readonly DataTable _dataTable = new(_tableName);
22+
private Mock<ISqlConnectionFactory> _sqlConnectionFactoryMock;
23+
private Mock<ILogEventDataGenerator> _logEventDataGeneratorMock;
24+
private Mock<ISqlConnectionWrapper> _sqlConnectionWrapperMock;
25+
private Mock<ISqlCommandWrapper> _sqlCommandWrapperMock;
26+
private List<LogEvent> _logEvents;
27+
private SqlInsertStatementWriter _sut;
28+
29+
[GlobalSetup]
30+
public void Setup()
31+
{
32+
_sqlConnectionFactoryMock = new Mock<ISqlConnectionFactory>();
33+
_logEventDataGeneratorMock = new Mock<ILogEventDataGenerator>();
34+
_sqlConnectionWrapperMock = new Mock<ISqlConnectionWrapper>();
35+
_sqlCommandWrapperMock = new Mock<ISqlCommandWrapper>();
36+
37+
_sqlConnectionFactoryMock.Setup(f => f.Create()).Returns(_sqlConnectionWrapperMock.Object);
38+
_sqlConnectionWrapperMock.Setup(f => f.CreateCommand()).Returns(_sqlCommandWrapperMock.Object);
39+
40+
CreateLogEvents();
41+
42+
_sut = new SqlInsertStatementWriter(_tableName, _schemaName, _sqlConnectionFactoryMock.Object,
43+
_logEventDataGeneratorMock.Object);
44+
}
45+
46+
[Benchmark]
47+
public async Task WriteBatch()
48+
{
49+
await _sut.WriteBatch(_logEvents, _dataTable);
50+
}
51+
52+
private static LogEvent CreateLogEvent()
53+
{
54+
return new LogEvent(
55+
new DateTimeOffset(2020, 1, 1, 0, 0, 0, 0, TimeSpan.Zero),
56+
LogEventLevel.Debug, null, new MessageTemplate(new List<MessageTemplateToken>()),
57+
new List<LogEventProperty>());
58+
}
59+
60+
private void CreateLogEvents()
61+
{
62+
_logEvents = new List<LogEvent>();
63+
var eventCount = 200_000;
64+
while (eventCount-- > 0)
65+
{
66+
_logEvents.Add(CreateLogEvent());
67+
}
68+
}
69+
70+
public void Dispose()
71+
{
72+
GC.SuppressFinalize(this);
73+
_dataTable.Dispose();
74+
}
75+
}

test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Output/AdditionalColumnDataGeneratorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
99
using Xunit;
1010

11-
namespace Serilog.Tests.Output
11+
namespace Serilog.Sinks.MSSqlServer.Tests.Output
1212
{
1313
[Trait(TestCategory.TraitName, TestCategory.Unit)]
1414
public class AdditionalColumnDataGeneratorTests

0 commit comments

Comments
 (0)