|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | + |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | +using BenchmarkDotNet.Columns; |
| 7 | +using BenchmarkDotNet.Order; |
| 8 | + |
| 9 | +using Serilog.Events; |
| 10 | + |
| 11 | +namespace Serilog.Sinks.SqlServer.Benchmark; |
| 12 | + |
| 13 | +[MemoryDiagnoser] |
| 14 | +[RankColumn] |
| 15 | +[Orderer(SummaryOrderPolicy.FastestToSlowest)] |
| 16 | +[HideColumns(Column.Error, Column.StdDev)] |
| 17 | +public class SinkBenchmark |
| 18 | +{ |
| 19 | + private const string SqlConnectionString = "Data Source=(local);Initial Catalog=SerilogBenchmark;Integrated Security=True;TrustServerCertificate=True;"; |
| 20 | + private const int BatchSize = 100; |
| 21 | + |
| 22 | + private MSSqlServer.MSSqlServerSink _msSqlServerSink; |
| 23 | + private SqlServerSink _sqlServerSink; |
| 24 | + |
| 25 | + private LogEvent[] _logEvents; |
| 26 | + |
| 27 | + [GlobalSetup] |
| 28 | + public void Setup() |
| 29 | + { |
| 30 | + // Create a sample exception to include in log events |
| 31 | + var exception = new InvalidOperationException("Benchmark exception"); |
| 32 | + |
| 33 | + // Create log events to batch |
| 34 | + _logEvents = new LogEvent[BatchSize]; |
| 35 | + for (int i = 0; i < BatchSize; i++) |
| 36 | + { |
| 37 | + var template = new Parsing.MessageTemplateParser() |
| 38 | + .Parse("Batch message {Counter} user {UserId} action {Action}"); |
| 39 | + |
| 40 | + var properties = new List<LogEventProperty> |
| 41 | + { |
| 42 | + new("Counter", new ScalarValue(i)), |
| 43 | + new("UserId", new ScalarValue($"user{i}")), |
| 44 | + new("Action", new ScalarValue("BatchTest")), |
| 45 | + new("SourceContext", new ScalarValue("Serilog.Sinks.SqlServer.Benchmark.SinkBenchmark")) |
| 46 | + }; |
| 47 | + |
| 48 | + _logEvents[i] = new LogEvent( |
| 49 | + DateTimeOffset.UtcNow, |
| 50 | + LogEventLevel.Information, |
| 51 | + exception, |
| 52 | + template, |
| 53 | + properties); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + // Serilog.Sinks.MSSqlServer |
| 58 | + _msSqlServerSink = new MSSqlServer.MSSqlServerSink( |
| 59 | + connectionString: SqlConnectionString, |
| 60 | + sinkOptions: new MSSqlServer.MSSqlServerSinkOptions |
| 61 | + { |
| 62 | + TableName = "LogsMSSqlServerBatch", |
| 63 | + AutoCreateSqlTable = false, |
| 64 | + BatchPostingLimit = BatchSize, |
| 65 | + BatchPeriod = TimeSpan.FromSeconds(2), |
| 66 | + UseSqlBulkCopy = true |
| 67 | + }); |
| 68 | + |
| 69 | + // Serilog.Sinks.SqlServer |
| 70 | + var options = new SqlServerSinkOptions |
| 71 | + { |
| 72 | + ConnectionString = SqlConnectionString, |
| 73 | + TableName = "LogsSqlServerBatch", |
| 74 | + BatchSizeLimit = BatchSize, |
| 75 | + BufferingTimeLimit = TimeSpan.FromSeconds(2) |
| 76 | + }; |
| 77 | + |
| 78 | + // Exclude SourceContext column for fair comparison |
| 79 | + options.Mappings.RemoveAll(m => m.ColumnName == "SourceContext"); |
| 80 | + |
| 81 | + _sqlServerSink = new SqlServerSink(options); |
| 82 | + } |
| 83 | + |
| 84 | + [GlobalCleanup] |
| 85 | + public void Cleanup() |
| 86 | + { |
| 87 | + (_msSqlServerSink as IDisposable)?.Dispose(); |
| 88 | + (_sqlServerSink as IDisposable)?.Dispose(); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + [Benchmark] |
| 93 | + public async Task MSSqlServerSink() |
| 94 | + { |
| 95 | + await _msSqlServerSink.EmitBatchAsync(_logEvents); |
| 96 | + } |
| 97 | + |
| 98 | + [Benchmark] |
| 99 | + public async Task SqlServerSink() |
| 100 | + { |
| 101 | + await _sqlServerSink.EmitBatchAsync(_logEvents); |
| 102 | + } |
| 103 | +} |
0 commit comments