From e07ce8effb5f1096d62848118763214fbd5b097f Mon Sep 17 00:00:00 2001 From: Christian Kadluba <10721825+ckadluba@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:38:34 +0200 Subject: [PATCH 1/3] Fixed namespace of unit test --- .../MSSqlServer/Output/AdditionalColumnDataGeneratorTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Output/AdditionalColumnDataGeneratorTests.cs b/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Output/AdditionalColumnDataGeneratorTests.cs index 0b08096e..e7143479 100644 --- a/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Output/AdditionalColumnDataGeneratorTests.cs +++ b/test/Serilog.Sinks.MSSqlServer.Tests/Sinks/MSSqlServer/Output/AdditionalColumnDataGeneratorTests.cs @@ -8,7 +8,7 @@ using Serilog.Sinks.MSSqlServer.Tests.TestUtils; using Xunit; -namespace Serilog.Tests.Output +namespace Serilog.Sinks.MSSqlServer.Tests.Output { [Trait(TestCategory.TraitName, TestCategory.Unit)] public class AdditionalColumnDataGeneratorTests From 4506de75db5079059a13d4306812cd63155be174 Mon Sep 17 00:00:00 2001 From: Christian Kadluba <10721825+ckadluba@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:39:15 +0200 Subject: [PATCH 2/3] Added SqlInsertStatementWriter benchmark --- .../SqlInsertStatementWriterBenchmarks.cs | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 test/Serilog.Sinks.MSSqlServer.PerformanceTests/Sinks/MSSqlServer/Platform/SqlInsertStatementWriterBenchmarks.cs diff --git a/test/Serilog.Sinks.MSSqlServer.PerformanceTests/Sinks/MSSqlServer/Platform/SqlInsertStatementWriterBenchmarks.cs b/test/Serilog.Sinks.MSSqlServer.PerformanceTests/Sinks/MSSqlServer/Platform/SqlInsertStatementWriterBenchmarks.cs new file mode 100644 index 00000000..2c2f76f4 --- /dev/null +++ b/test/Serilog.Sinks.MSSqlServer.PerformanceTests/Sinks/MSSqlServer/Platform/SqlInsertStatementWriterBenchmarks.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; +using Moq; +using Serilog.Events; +using Serilog.Parsing; +using Serilog.Sinks.MSSqlServer.Output; +using Serilog.Sinks.MSSqlServer.Platform; +using Serilog.Sinks.MSSqlServer.Platform.SqlClient; + +namespace Serilog.Sinks.MSSqlServer.PerformanceTests.Platform; + +[MemoryDiagnoser] +[MaxIterationCount(16)] +public class SqlInsertStatementWriterBenchmarks : 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 _sqlCommandWrapperMock; + private List _logEvents; + private SqlInsertStatementWriter _sut; + + [GlobalSetup] + public void Setup() + { + _sqlConnectionFactoryMock = new Mock(); + _logEventDataGeneratorMock = new Mock(); + _sqlConnectionWrapperMock = new Mock(); + _sqlCommandWrapperMock = new Mock(); + + _sqlConnectionFactoryMock.Setup(f => f.Create()).Returns(_sqlConnectionWrapperMock.Object); + _sqlConnectionWrapperMock.Setup(f => f.CreateCommand()).Returns(_sqlCommandWrapperMock.Object); + + CreateLogEvents(); + + _sut = new SqlInsertStatementWriter(_tableName, _schemaName, _sqlConnectionFactoryMock.Object, + _logEventDataGeneratorMock.Object); + } + + [Benchmark] + public async Task WriteBatch() + { + await _sut.WriteBatch(_logEvents, _dataTable); + } + + private static LogEvent CreateLogEvent() + { + return new LogEvent( + new DateTimeOffset(2020, 1, 1, 0, 0, 0, 0, TimeSpan.Zero), + LogEventLevel.Debug, null, new MessageTemplate(new List()), + new List()); + } + + private void CreateLogEvents() + { + _logEvents = new List(); + var eventCount = 200_000; + while (eventCount-- > 0) + { + _logEvents.Add(CreateLogEvent()); + } + } + + public void Dispose() + { + GC.SuppressFinalize(this); + _dataTable.Dispose(); + } +} 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 3/3] 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(); + } }