Skip to content

Commit 89a53f7

Browse files
committed
Improved SqlBulkBatchWriter benchmark
1 parent 4506de7 commit 89a53f7

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
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
}

0 commit comments

Comments
 (0)