|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using BenchmarkDotNet.Attributes; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Sentry.Extensibility; |
| 6 | +using Sentry.Extensions.Logging; |
| 7 | +using Sentry.Internal; |
| 8 | +using Sentry.Testing; |
| 9 | + |
| 10 | +namespace Sentry.Benchmarks.Extensions.Logging; |
| 11 | + |
| 12 | +public class SentryStructuredLoggerBenchmarks |
| 13 | +{ |
| 14 | + private Hub _hub = null!; |
| 15 | + private Sentry.Extensions.Logging.SentryStructuredLogger _logger = null!; |
| 16 | + private LogRecord _logRecord = null!; |
| 17 | + private SentryLog? _lastLog; |
| 18 | + |
| 19 | + [GlobalSetup] |
| 20 | + public void Setup() |
| 21 | + { |
| 22 | + SentryLoggingOptions options = new() |
| 23 | + { |
| 24 | + Dsn = DsnSamples.ValidDsn, |
| 25 | + Experimental = |
| 26 | + { |
| 27 | + EnableLogs = true, |
| 28 | + }, |
| 29 | + ExperimentalLogging = |
| 30 | + { |
| 31 | + MinimumLogLevel = LogLevel.Information, |
| 32 | + } |
| 33 | + }; |
| 34 | + options.Experimental.SetBeforeSendLog((SentryLog log) => |
| 35 | + { |
| 36 | + _lastLog = log; |
| 37 | + return null; |
| 38 | + }); |
| 39 | + |
| 40 | + MockClock clock = new(new DateTimeOffset(2025, 04, 22, 14, 51, 00, 789, TimeSpan.FromHours(2))); |
| 41 | + SdkVersion sdk = new() |
| 42 | + { |
| 43 | + Name = "SDK Name", |
| 44 | + Version = "SDK Version", |
| 45 | + }; |
| 46 | + |
| 47 | + _hub = new Hub(options, DisabledHub.Instance); |
| 48 | + _logger = new Sentry.Extensions.Logging.SentryStructuredLogger("CategoryName", options, _hub, clock, sdk); |
| 49 | + _logRecord = new LogRecord(LogLevel.Information, new EventId(2025, "EventName"), new InvalidOperationException("exception-message"), "Number={Number}, Text={Text}", 2018, "message"); |
| 50 | + } |
| 51 | + |
| 52 | + [Benchmark] |
| 53 | + public void Log() |
| 54 | + { |
| 55 | + _logger.Log(_logRecord.LogLevel, _logRecord.EventId, _logRecord.Exception, _logRecord.Message, _logRecord.Args); |
| 56 | + } |
| 57 | + |
| 58 | + [GlobalCleanup] |
| 59 | + public void Cleanup() |
| 60 | + { |
| 61 | + _hub.Dispose(); |
| 62 | + |
| 63 | + if (_lastLog is null) |
| 64 | + { |
| 65 | + throw new InvalidOperationException("Last Log is null"); |
| 66 | + } |
| 67 | + if (_lastLog.Message != "Number=2018, Text=message") |
| 68 | + { |
| 69 | + throw new InvalidOperationException($"Last Log with Message: '{_lastLog.Message}'"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private sealed class LogRecord |
| 74 | + { |
| 75 | + public LogRecord(LogLevel logLevel, EventId eventId, Exception? exception, string? message, params object?[] args) |
| 76 | + { |
| 77 | + LogLevel = logLevel; |
| 78 | + EventId = eventId; |
| 79 | + Exception = exception; |
| 80 | + Message = message; |
| 81 | + Args = args; |
| 82 | + } |
| 83 | + |
| 84 | + public LogLevel LogLevel { get; } |
| 85 | + public EventId EventId { get; } |
| 86 | + public Exception? Exception { get; } |
| 87 | + public string? Message { get; } |
| 88 | + public object?[] Args { get; } |
| 89 | + } |
| 90 | +} |
0 commit comments