From 89a53f70cbb6082a7e39386a01cd6543ae26e79b Mon Sep 17 00:00:00 2001 From: Christian Kadluba <10721825+ckadluba@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:40:17 +0200 Subject: [PATCH] Improved SqlBulkBatchWriter benchmark --- .../Platform/SqlBulkBatchWriterBenchmarks.cs | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/test/Serilog.Sinks.MSSqlServer.PerformanceTests/Sinks/MSSqlServer/Platform/SqlBulkBatchWriterBenchmarks.cs b/test/Serilog.Sinks.MSSqlServer.PerformanceTests/Sinks/MSSqlServer/Platform/SqlBulkBatchWriterBenchmarks.cs index 97735971..c2c7a756 100644 --- a/test/Serilog.Sinks.MSSqlServer.PerformanceTests/Sinks/MSSqlServer/Platform/SqlBulkBatchWriterBenchmarks.cs +++ b/test/Serilog.Sinks.MSSqlServer.PerformanceTests/Sinks/MSSqlServer/Platform/SqlBulkBatchWriterBenchmarks.cs @@ -13,15 +13,17 @@ namespace Serilog.Sinks.MSSqlServer.PerformanceTests.Platform; [MemoryDiagnoser] -[MaxIterationCount(20)] -public class SqlBulkBatchWriterBenchmarks +[MaxIterationCount(16)] +public class SqlBulkBatchWriterBenchmarks : IDisposable { private const string _tableName = "TestTableName"; private const string _schemaName = "TestSchemaName"; + private readonly DataTable _dataTable = new(_tableName); private Mock _sqlConnectionFactoryMock; private Mock _logEventDataGeneratorMock; private Mock _sqlConnectionWrapperMock; private Mock _sqlBulkCopyWrapper; + private List _logEvents; private SqlBulkBatchWriter _sut; [GlobalSetup] @@ -36,6 +38,8 @@ public void Setup() _sqlConnectionWrapperMock.Setup(c => c.CreateSqlBulkCopy(It.IsAny(), It.IsAny())) .Returns(_sqlBulkCopyWrapper.Object); + CreateLogEvents(); + _sut = new SqlBulkBatchWriter(_tableName, _schemaName, false, _sqlConnectionFactoryMock.Object, _logEventDataGeneratorMock.Object); } @@ -43,15 +47,7 @@ public void Setup() [Benchmark] public async Task WriteBatch() { - var logEvents = CreateLogEvents(); - using var dataTable = new DataTable(_tableName); - await _sut.WriteBatch(logEvents, dataTable); - } - - private static List CreateLogEvents() - { - var logEvents = new List { CreateLogEvent(), CreateLogEvent() }; - return logEvents; + await _sut.WriteBatch(_logEvents, _dataTable); } private static LogEvent CreateLogEvent() @@ -61,4 +57,20 @@ private static LogEvent CreateLogEvent() LogEventLevel.Debug, null, new MessageTemplate(new List()), new List()); } + + private void CreateLogEvents() + { + _logEvents = new List(); + var eventCount = 500_000; + while (eventCount-- > 0) + { + _logEvents.Add(CreateLogEvent()); + } + } + + public void Dispose() + { + GC.SuppressFinalize(this); + _dataTable.Dispose(); + } }