Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Original file line number Diff line number Diff line change
@@ -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<ISqlConnectionFactory> _sqlConnectionFactoryMock;
private Mock<ILogEventDataGenerator> _logEventDataGeneratorMock;
private Mock<ISqlConnectionWrapper> _sqlConnectionWrapperMock;
private Mock<ISqlCommandWrapper> _sqlCommandWrapperMock;
private List<LogEvent> _logEvents;
private SqlInsertStatementWriter _sut;

[GlobalSetup]
public void Setup()
{
_sqlConnectionFactoryMock = new Mock<ISqlConnectionFactory>();
_logEventDataGeneratorMock = new Mock<ILogEventDataGenerator>();
_sqlConnectionWrapperMock = new Mock<ISqlConnectionWrapper>();
_sqlCommandWrapperMock = new Mock<ISqlCommandWrapper>();

_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<MessageTemplateToken>()),
new List<LogEventProperty>());
}

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

public void Dispose()
{
GC.SuppressFinalize(this);
_dataTable.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading