Skip to content

Commit

Permalink
Improved SqlBulkBatchWriter benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ckadluba committed Oct 7, 2024
1 parent 4506de7 commit 89a53f7
Showing 1 changed file with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISqlConnectionFactory> _sqlConnectionFactoryMock;
private Mock<ILogEventDataGenerator> _logEventDataGeneratorMock;
private Mock<ISqlConnectionWrapper> _sqlConnectionWrapperMock;
private Mock<ISqlBulkCopyWrapper> _sqlBulkCopyWrapper;
private List<LogEvent> _logEvents;
private SqlBulkBatchWriter _sut;

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

CreateLogEvents();

_sut = new SqlBulkBatchWriter(_tableName, _schemaName, false, _sqlConnectionFactoryMock.Object,
_logEventDataGeneratorMock.Object);
}

[Benchmark]
public async Task WriteBatch()
{
var logEvents = CreateLogEvents();
using var dataTable = new DataTable(_tableName);
await _sut.WriteBatch(logEvents, dataTable);
}

private static List<LogEvent> CreateLogEvents()
{
var logEvents = new List<LogEvent> { CreateLogEvent(), CreateLogEvent() };
return logEvents;
await _sut.WriteBatch(_logEvents, _dataTable);
}

private static LogEvent CreateLogEvent()
Expand All @@ -61,4 +57,20 @@ private static LogEvent CreateLogEvent()
LogEventLevel.Debug, null, new MessageTemplate(new List<MessageTemplateToken>()),
new List<LogEventProperty>());
}

private void CreateLogEvents()
{
_logEvents = new List<LogEvent>();
var eventCount = 500_000;
while (eventCount-- > 0)
{
_logEvents.Add(CreateLogEvent());
}
}

public void Dispose()
{
GC.SuppressFinalize(this);
_dataTable.Dispose();
}
}

0 comments on commit 89a53f7

Please sign in to comment.