|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Columns; |
| 3 | +using BenchmarkDotNet.Configs; |
| 4 | +using BenchmarkDotNet.Diagnosers; |
| 5 | +using BenchmarkDotNet.Exporters; |
| 6 | +using BenchmarkDotNet.Jobs; |
| 7 | +using BenchmarkDotNet.Loggers; |
| 8 | +using BenchmarkDotNet.Order; |
| 9 | +using BenchmarkDotNet.Reports; |
| 10 | +using Perfolizer.Horology; |
| 11 | + |
| 12 | +namespace BenchmarkDotNet; |
| 13 | + |
| 14 | +public class BaseBenchmarkConfig : ManualConfig |
| 15 | +{ |
| 16 | + public BaseBenchmarkConfig() |
| 17 | + { |
| 18 | + WithSummaryStyle(SummaryStyle.Default.WithMaxParameterColumnWidth(40)); // Default: 20 chars |
| 19 | + WithBuildTimeout(TimeSpan.FromMinutes(10)); // Default: 120 seconds |
| 20 | + |
| 21 | + WithOrderer(new DefaultOrderer()); |
| 22 | + WithUnionRule(ConfigUnionRule.Union); |
| 23 | + WithArtifactsPath(DefaultConfig.Instance.ArtifactsPath); |
| 24 | + |
| 25 | +#if DEBUG |
| 26 | + // Allow benchmarks for debug build. |
| 27 | + WithOptions(ConfigOptions.DisableOptimizationsValidator); |
| 28 | +#endif |
| 29 | + |
| 30 | + // Enable following settings for debugging |
| 31 | + // WithOptions(ConfigOptions.StopOnFirstError); |
| 32 | + // WithOptions(ConfigOptions.KeepBenchmarkFiles); |
| 33 | + // WithOptions(ConfigOptions.GenerateMSBuildBinLog); |
| 34 | + } |
| 35 | + |
| 36 | + // Use ShortRun based settings (LaunchCount=1 IterationCount=3 WarmupCount = 3) |
| 37 | + // And use RecommendedConfig setting that used by `dotnet/performance` repository. |
| 38 | + // https://github.com/dotnet/performance/blob/main/src/harness/BenchmarkDotNet.Extensions/RecommendedConfig.cs |
| 39 | + protected virtual Job GetBaseJobConfig() => |
| 40 | + Job.Default |
| 41 | + .WithLaunchCount(1) |
| 42 | + .WithWarmupCount(3) |
| 43 | + .WithIterationTime(TimeInterval.FromMilliseconds(250)) // Default: 500 [ms] |
| 44 | + .WithMinIterationCount(15) // Default: 15 |
| 45 | + .WithMaxIterationCount(20); // Default: 100 |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Add configurations. |
| 49 | + /// </summary> |
| 50 | + protected void AddConfigurations() |
| 51 | + { |
| 52 | + AddAnalyzers(); |
| 53 | + AddColumnHidingRules(); |
| 54 | + AddColumnProviders(); |
| 55 | + AddDiagnosers(); |
| 56 | + AddEventProcessors(); |
| 57 | + AddExporters(); |
| 58 | + AddFilters(); |
| 59 | + AddHardwareCounters(); |
| 60 | + AddLoggers(); |
| 61 | + AddLogicalGroupRules(); |
| 62 | + AddValidators(); |
| 63 | + } |
| 64 | + |
| 65 | + protected virtual void AddAnalyzers() |
| 66 | + { |
| 67 | + AddAnalyser(DefaultConfig.Instance.GetAnalysers().ToArray()); |
| 68 | + } |
| 69 | + |
| 70 | + protected virtual void AddColumnHidingRules() |
| 71 | + { |
| 72 | + } |
| 73 | + |
| 74 | + protected virtual void AddColumnProviders() |
| 75 | + { |
| 76 | + AddColumnProvider(DefaultColumnProviders.Instance); |
| 77 | + } |
| 78 | + |
| 79 | + protected virtual void AddDiagnosers() |
| 80 | + { |
| 81 | + AddDiagnoser(MemoryDiagnoser.Default); |
| 82 | + AddDiagnoser(new ExceptionDiagnoser(new ExceptionDiagnoserConfig(displayExceptionsIfZeroValue: false))); |
| 83 | + |
| 84 | +#if NETCOREAPP3_0_OR_GREATER |
| 85 | + AddDiagnoser(new ThreadingDiagnoser(new ThreadingDiagnoserConfig(displayCompletedWorkItemCountWhenZero: false, displayLockContentionWhenZero: false))); |
| 86 | +#endif |
| 87 | + } |
| 88 | + |
| 89 | + protected virtual void AddExporters() |
| 90 | + { |
| 91 | + // Use ConsoleMarkdownExporter to disable group higligting with `**`. |
| 92 | + AddExporter(MarkdownExporter.Console); |
| 93 | + } |
| 94 | + |
| 95 | + protected virtual void AddEventProcessors() |
| 96 | + { |
| 97 | + } |
| 98 | + |
| 99 | + protected virtual void AddFilters() |
| 100 | + { |
| 101 | + AddFilter(TargetFrameworkFilter.Instance); |
| 102 | + } |
| 103 | + |
| 104 | + protected virtual void AddHardwareCounters() |
| 105 | + { |
| 106 | + } |
| 107 | + |
| 108 | + protected virtual void AddLoggers() |
| 109 | + { |
| 110 | + AddLogger(ConsoleLogger.Default); |
| 111 | + } |
| 112 | + |
| 113 | + protected virtual void AddLogicalGroupRules() |
| 114 | + { |
| 115 | + } |
| 116 | + |
| 117 | + protected virtual void AddValidators() |
| 118 | + { |
| 119 | + AddValidator(DefaultConfig.Instance.GetValidators().ToArray()); |
| 120 | + } |
| 121 | +} |
0 commit comments