|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | +using Moq; |
| 7 | +using Serilog.Events; |
| 8 | +using Serilog.Parsing; |
| 9 | +using Serilog.Sinks.MSSqlServer.Output; |
| 10 | +using Serilog.Sinks.MSSqlServer.Platform; |
| 11 | +using Serilog.Sinks.MSSqlServer.Platform.SqlClient; |
| 12 | + |
| 13 | +namespace Serilog.Sinks.MSSqlServer.PerformanceTests.Platform; |
| 14 | + |
| 15 | +[MemoryDiagnoser] |
| 16 | +[MaxIterationCount(16)] |
| 17 | +public class SqlInsertStatementWriterBenchmarks : IDisposable |
| 18 | +{ |
| 19 | + private const string _tableName = "TestTableName"; |
| 20 | + private const string _schemaName = "TestSchemaName"; |
| 21 | + private readonly DataTable _dataTable = new(_tableName); |
| 22 | + private Mock<ISqlConnectionFactory> _sqlConnectionFactoryMock; |
| 23 | + private Mock<ILogEventDataGenerator> _logEventDataGeneratorMock; |
| 24 | + private Mock<ISqlConnectionWrapper> _sqlConnectionWrapperMock; |
| 25 | + private Mock<ISqlCommandWrapper> _sqlCommandWrapperMock; |
| 26 | + private List<LogEvent> _logEvents; |
| 27 | + private SqlInsertStatementWriter _sut; |
| 28 | + |
| 29 | + [GlobalSetup] |
| 30 | + public void Setup() |
| 31 | + { |
| 32 | + _sqlConnectionFactoryMock = new Mock<ISqlConnectionFactory>(); |
| 33 | + _logEventDataGeneratorMock = new Mock<ILogEventDataGenerator>(); |
| 34 | + _sqlConnectionWrapperMock = new Mock<ISqlConnectionWrapper>(); |
| 35 | + _sqlCommandWrapperMock = new Mock<ISqlCommandWrapper>(); |
| 36 | + |
| 37 | + _sqlConnectionFactoryMock.Setup(f => f.Create()).Returns(_sqlConnectionWrapperMock.Object); |
| 38 | + _sqlConnectionWrapperMock.Setup(f => f.CreateCommand()).Returns(_sqlCommandWrapperMock.Object); |
| 39 | + |
| 40 | + CreateLogEvents(); |
| 41 | + |
| 42 | + _sut = new SqlInsertStatementWriter(_tableName, _schemaName, _sqlConnectionFactoryMock.Object, |
| 43 | + _logEventDataGeneratorMock.Object); |
| 44 | + } |
| 45 | + |
| 46 | + [Benchmark] |
| 47 | + public async Task WriteBatch() |
| 48 | + { |
| 49 | + await _sut.WriteBatch(_logEvents, _dataTable); |
| 50 | + } |
| 51 | + |
| 52 | + private static LogEvent CreateLogEvent() |
| 53 | + { |
| 54 | + return new LogEvent( |
| 55 | + new DateTimeOffset(2020, 1, 1, 0, 0, 0, 0, TimeSpan.Zero), |
| 56 | + LogEventLevel.Debug, null, new MessageTemplate(new List<MessageTemplateToken>()), |
| 57 | + new List<LogEventProperty>()); |
| 58 | + } |
| 59 | + |
| 60 | + private void CreateLogEvents() |
| 61 | + { |
| 62 | + _logEvents = new List<LogEvent>(); |
| 63 | + var eventCount = 200_000; |
| 64 | + while (eventCount-- > 0) |
| 65 | + { |
| 66 | + _logEvents.Add(CreateLogEvent()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public void Dispose() |
| 71 | + { |
| 72 | + GC.SuppressFinalize(this); |
| 73 | + _dataTable.Dispose(); |
| 74 | + } |
| 75 | +} |
0 commit comments