13
13
namespace Serilog . Sinks . MSSqlServer . PerformanceTests . Platform ;
14
14
15
15
[ MemoryDiagnoser ]
16
- [ MaxIterationCount ( 20 ) ]
17
- public class SqlBulkBatchWriterBenchmarks
16
+ [ MaxIterationCount ( 16 ) ]
17
+ public class SqlBulkBatchWriterBenchmarks : IDisposable
18
18
{
19
19
private const string _tableName = "TestTableName" ;
20
20
private const string _schemaName = "TestSchemaName" ;
21
+ private readonly DataTable _dataTable = new ( _tableName ) ;
21
22
private Mock < ISqlConnectionFactory > _sqlConnectionFactoryMock ;
22
23
private Mock < ILogEventDataGenerator > _logEventDataGeneratorMock ;
23
24
private Mock < ISqlConnectionWrapper > _sqlConnectionWrapperMock ;
24
25
private Mock < ISqlBulkCopyWrapper > _sqlBulkCopyWrapper ;
26
+ private List < LogEvent > _logEvents ;
25
27
private SqlBulkBatchWriter _sut ;
26
28
27
29
[ GlobalSetup ]
@@ -36,22 +38,16 @@ public void Setup()
36
38
_sqlConnectionWrapperMock . Setup ( c => c . CreateSqlBulkCopy ( It . IsAny < bool > ( ) , It . IsAny < string > ( ) ) )
37
39
. Returns ( _sqlBulkCopyWrapper . Object ) ;
38
40
41
+ CreateLogEvents ( ) ;
42
+
39
43
_sut = new SqlBulkBatchWriter ( _tableName , _schemaName , false , _sqlConnectionFactoryMock . Object ,
40
44
_logEventDataGeneratorMock . Object ) ;
41
45
}
42
46
43
47
[ Benchmark ]
44
48
public async Task WriteBatch ( )
45
49
{
46
- var logEvents = CreateLogEvents ( ) ;
47
- using var dataTable = new DataTable ( _tableName ) ;
48
- await _sut . WriteBatch ( logEvents , dataTable ) ;
49
- }
50
-
51
- private static List < LogEvent > CreateLogEvents ( )
52
- {
53
- var logEvents = new List < LogEvent > { CreateLogEvent ( ) , CreateLogEvent ( ) } ;
54
- return logEvents ;
50
+ await _sut . WriteBatch ( _logEvents , _dataTable ) ;
55
51
}
56
52
57
53
private static LogEvent CreateLogEvent ( )
@@ -61,4 +57,20 @@ private static LogEvent CreateLogEvent()
61
57
LogEventLevel . Debug , null , new MessageTemplate ( new List < MessageTemplateToken > ( ) ) ,
62
58
new List < LogEventProperty > ( ) ) ;
63
59
}
60
+
61
+ private void CreateLogEvents ( )
62
+ {
63
+ _logEvents = new List < LogEvent > ( ) ;
64
+ var eventCount = 500_000 ;
65
+ while ( eventCount -- > 0 )
66
+ {
67
+ _logEvents . Add ( CreateLogEvent ( ) ) ;
68
+ }
69
+ }
70
+
71
+ public void Dispose ( )
72
+ {
73
+ GC . SuppressFinalize ( this ) ;
74
+ _dataTable . Dispose ( ) ;
75
+ }
64
76
}
0 commit comments