1- using BenchmarkDotNet . Running ;
1+ using BenchmarkDotNet . Configs ;
2+ using BenchmarkDotNet . Engines ;
3+ using BenchmarkDotNet . Jobs ;
4+ using BenchmarkDotNet . Loggers ;
5+ using BenchmarkDotNet . Reports ;
6+ using BenchmarkDotNet . Running ;
7+ using BenchmarkDotNet . Toolchains . InProcess . Emit ;
8+ using System ;
9+ using System . Collections . Generic ;
10+ using System . Linq ;
211
3- namespace BenchmarkDotNet . Samples
12+ namespace BenchmarkDotNet . Samples ;
13+
14+ public class Program
415{
5- public class Program
16+ public static int Main ( string [ ] args )
617 {
7- public static void Main ( string [ ] args ) => BenchmarkSwitcher . FromAssembly ( typeof ( Program ) . Assembly ) . Run ( args ) ;
18+ #if DEBUG
19+ ConsoleLogger . Default . WriteLineWarning ( "Benchmark is executed with DEBUG configuration." ) ;
20+ ConsoleLogger . Default . WriteLine ( ) ;
21+ #endif
22+
23+ if ( args . Length != 0 )
24+ {
25+ ConsoleLogger . Default . WriteLine ( $ "Start benchmarks with args: { string . Join ( " " , args ) } ") ;
26+ ConsoleLogger . Default . WriteLine ( ) ;
27+ }
28+
29+ IConfig config ;
30+ #if DEBUG
31+ config = GetDebugConfig ( ) ;
32+ #else
33+ config = null ; // `DefaultConfig.Instance` is used.
34+ #endif
35+
36+ var summaries = BenchmarkSwitcher . FromAssembly ( typeof ( Program ) . Assembly )
37+ . Run ( args , config )
38+ . ToArray ( ) ;
39+
40+ if ( summaries . HasError ( ) )
41+ return 1 ;
42+
43+ return 0 ;
44+ }
45+
46+ private static ManualConfig GetDebugConfig ( )
47+ {
48+ return DefaultConfig . Instance
49+ . WithOptions ( ConfigOptions . DisableOptimizationsValidator )
50+ . WithOptions ( ConfigOptions . StopOnFirstError )
51+ . AddJob (
52+ Job . Default
53+ . WithId ( "WithDebugConfiguration" )
54+ . WithToolchain ( InProcessEmitToolchain . Instance )
55+ . WithStrategy ( RunStrategy . Monitoring )
56+ ) ;
57+ }
58+ }
59+
60+ file static class ExtensionMethods
61+ {
62+ public static bool HasError ( this Summary [ ] summaries )
63+ {
64+ if ( summaries . Length == 0 )
65+ {
66+ var hashSet = new HashSet < string > ( [ "--help" , "--list" , "--info" , "--version" ] ) ;
67+ return ! Environment . GetCommandLineArgs ( ) . Any ( hashSet . Contains ) ;
68+ }
69+
70+ if ( summaries . Any ( x => x . HasCriticalValidationErrors ) )
71+ return true ;
72+
73+ return summaries . Any ( x => x . Reports . Any ( r => ! r . Success ) ) ;
874 }
9- }
75+ }
0 commit comments