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(); + } } 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(); + } +} 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