|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Configs; |
| 3 | +using Microsoft.Extensions.Logging; |
| 4 | + |
| 5 | +[RankColumn] |
| 6 | +[MemoryDiagnoser] |
| 7 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 8 | +public partial class LogScopeBenchmark |
| 9 | +{ |
| 10 | + private ILogger _logger = null!; |
| 11 | + private ConfigurationExample _configuration = null!; |
| 12 | + |
| 13 | + [GlobalSetup] |
| 14 | + public void Setup() |
| 15 | + { |
| 16 | + _logger = CustomNullLogger<LogScopeBenchmark>.Instance; |
| 17 | + _configuration = new ConfigurationExample( |
| 18 | + 0, "Root", new ConfigurationExample( |
| 19 | + 1, "First Level", new ConfigurationExample( |
| 20 | + 2, "Second level", null) |
| 21 | + ) |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | +#if !TELEMETRY |
| 26 | + |
| 27 | + [Benchmark(Baseline = true)] |
| 28 | + [BenchmarkCategory("Without parameters")] |
| 29 | + public void BeginScopeWithoutParameters() |
| 30 | + { |
| 31 | + using var _ = _logger.BeginScope("Hello world!"); |
| 32 | + } |
| 33 | + |
| 34 | +#endif |
| 35 | + |
| 36 | +#if DEFAULT && !TELEMETRY && !AUTO_LOGGER_MESSAGE |
| 37 | + |
| 38 | + [Benchmark] |
| 39 | + [BenchmarkCategory("Without parameters")] |
| 40 | + public void LoggerDefineScopeWithoutParameters() |
| 41 | + { |
| 42 | + using var _ = _loggerDefineScopeWithoutParameters(_logger); |
| 43 | + } |
| 44 | + |
| 45 | + private static readonly Func<ILogger, IDisposable?> _loggerDefineScopeWithoutParameters = |
| 46 | + LoggerMessage.DefineScope("Hello world!"); |
| 47 | + |
| 48 | +#endif |
| 49 | + |
| 50 | +#if !TELEMETRY |
| 51 | + [Benchmark(Baseline = true)] |
| 52 | + [BenchmarkCategory("With 6 Parameters")] |
| 53 | + public void BeginScopeWith6PrimitiveParameters() |
| 54 | + { |
| 55 | + using var _ = _logger.BeginScope("Hello world! {arg1} {arg2} {arg3} {arg4} {arg5} {arg6}", 1, 2, 3, 4, 5, 6); |
| 56 | + } |
| 57 | +#endif |
| 58 | + |
| 59 | +#if DEFAULT && !TELEMETRY && !AUTO_LOGGER_MESSAGE |
| 60 | + |
| 61 | + [Benchmark] |
| 62 | + [BenchmarkCategory("With 6 Parameters")] |
| 63 | + public void LoggerDefineScopeWith6Parameters() |
| 64 | + { |
| 65 | + using var _ = _loggerDefineScopeWith6Parameters(_logger, 1, 2, 3, 4, 5, 6); |
| 66 | + } |
| 67 | + |
| 68 | + private static readonly Func<ILogger, int, int, int, int, int, int, IDisposable?> _loggerDefineScopeWith6Parameters = |
| 69 | + LoggerMessage.DefineScope<int, int, int, int, int, int>("Hello world! {arg1} {arg2} {arg3} {arg4} {arg5} {arg6}"); |
| 70 | + |
| 71 | +#endif |
| 72 | + |
| 73 | +#if !TELEMETRY |
| 74 | + |
| 75 | + [Benchmark(Baseline = true)] |
| 76 | + [BenchmarkCategory("With Complex Types Parameters")] |
| 77 | + public void BeginScopeWithComplexParameters() |
| 78 | + { |
| 79 | + using var _ = _logger.BeginScope("Hello world! {arg1} {arg2} {arg3} {arg4} {arg5} {arg6}", |
| 80 | + _configuration, _configuration, _configuration, _configuration, _configuration, _configuration); |
| 81 | + } |
| 82 | + |
| 83 | +#endif |
| 84 | + |
| 85 | +#if DEFAULT && !TELEMETRY && !AUTO_LOGGER_MESSAGE |
| 86 | + |
| 87 | + [Benchmark] |
| 88 | + [BenchmarkCategory("With Complex Types Parameters")] |
| 89 | + public void LoggerDefineScopeWithComplexParameters() |
| 90 | + { |
| 91 | + using var _ = _loggerDefineScopeWithComplexParameters(_logger, |
| 92 | + _configuration, _configuration, _configuration, |
| 93 | + _configuration, _configuration, _configuration |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + private static readonly Func<ILogger, ConfigurationExample, ConfigurationExample, |
| 98 | + ConfigurationExample, ConfigurationExample, ConfigurationExample, |
| 99 | + ConfigurationExample, IDisposable?> _loggerDefineScopeWithComplexParameters = |
| 100 | + LoggerMessage.DefineScope<ConfigurationExample, ConfigurationExample, |
| 101 | + ConfigurationExample, ConfigurationExample, |
| 102 | + ConfigurationExample, ConfigurationExample>( |
| 103 | + "Hello world! {arg1} {arg2} {arg3} {arg4} {arg5} {arg6}" |
| 104 | + ); |
| 105 | + |
| 106 | +#endif |
| 107 | + |
| 108 | + public record ConfigurationExample(int Id, string Name, ConfigurationExample? NestedConfiguration); |
| 109 | +} |
0 commit comments