2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Linq ;
5
6
using System . Threading ;
6
7
using Microsoft . AspNetCore . Hosting ;
7
8
using Microsoft . Extensions . Configuration ;
9
+ using Microsoft . Extensions . DependencyInjection ;
8
10
9
11
namespace Benchmarks
10
12
{
11
13
public class Program
12
14
{
15
+ public static string [ ] Args ;
16
+
13
17
public static void Main ( string [ ] args )
14
18
{
15
- var host = new WebHostBuilder ( )
16
- . UseDefaultConfiguration ( args )
17
- . UseStartup < Startup > ( )
18
- . Build ( ) ;
19
+ Args = args ;
20
+
19
21
Console . WriteLine ( ) ;
20
- Console . WriteLine ( "ASP.NET 5 Benchmarks" ) ;
21
- Console . WriteLine ( "--------------------" ) ;
22
+ Console . WriteLine ( "ASP.NET Core Benchmarks" ) ;
23
+ Console . WriteLine ( "----------------------- " ) ;
22
24
23
- var config = new ConfigurationBuilder ( )
24
- . AddCommandLine ( args )
25
+ var scenarios = LoadScenarios ( args ) ;
26
+
27
+ StartInteractiveConsoleThread ( ) ;
28
+
29
+ var webHost = new WebHostBuilder ( )
30
+ . UseServer ( "Microsoft.AspNetCore.Server.Kestrel" )
31
+ . UseCaptureStartupErrors ( false )
32
+ . UseDefaultConfiguration ( args )
33
+ . UseStartup < Startup > ( )
34
+ . ConfigureServices ( services => services . AddSingleton ( scenarios ) )
25
35
. Build ( ) ;
26
36
27
- if ( string . IsNullOrEmpty ( config [ "scenarios" ] ) )
37
+ webHost . Run ( ) ;
38
+ }
39
+
40
+ private static Scenarios LoadScenarios ( string [ ] args )
41
+ {
42
+ var scenarioConfig = new ConfigurationBuilder ( )
43
+ . AddJsonFile ( "scenarios.json" , optional : true )
44
+ . AddCommandLine ( args )
45
+ . Build ( )
46
+ . GetChildren ( )
47
+ . ToList ( ) ;
48
+
49
+ var scenarios = new Scenarios ( ) ;
50
+
51
+ if ( scenarioConfig . Count > 0 )
52
+ {
53
+ Console . WriteLine ( "Scenario configuration found in scenarios.json and/or command line args" ) ;
54
+ foreach ( var scenario in scenarioConfig )
55
+ {
56
+ scenarios . Enable ( scenario . Value ) ;
57
+ }
58
+ }
59
+ else
28
60
{
29
61
Console . WriteLine ( "Which scenarios would you like to enable?:" ) ;
30
- Console . WriteLine ( " 1: Raw middleware" ) ;
31
- Console . WriteLine ( " 2: All MV" ) ;
32
- Console . Write ( "1, 2, 3, A(ll)> " ) ;
62
+ Console . WriteLine ( ) ;
63
+ foreach ( var scenario in scenarios . GetNames ( ) )
64
+ {
65
+ Console . WriteLine ( " " + scenario ) ;
66
+ }
67
+ Console . WriteLine ( ) ;
68
+ Console . WriteLine ( "Type full or partial scenario names separated by commas and hit [Enter]" ) ;
69
+ Console . Write ( "> " ) ;
70
+
71
+ var choices = Console . ReadLine ( ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
72
+
73
+ if ( choices . Length == 0 )
74
+ {
75
+ Console . WriteLine ( ) ;
76
+ Console . WriteLine ( "No choice entered, enabling default scenarios" ) ;
77
+ scenarios . EnableDefault ( ) ;
78
+ }
79
+ else
80
+ {
81
+ var count = 0 ;
82
+
83
+ foreach ( var choice in choices )
84
+ {
85
+ count += scenarios . Enable ( choice ) ;
86
+ }
87
+
88
+ if ( count == 0 )
89
+ {
90
+ Console . WriteLine ( ) ;
91
+ Console . WriteLine ( "No matching scenarios found, enabling defaults" ) ;
92
+ scenarios . EnableDefault ( ) ;
93
+ }
94
+ }
95
+ }
96
+
97
+ Console . WriteLine ( ) ;
98
+ Console . WriteLine ( "The following scenarios were enabled:" ) ;
99
+ foreach ( var scenario in scenarios . GetEnabled ( ) )
100
+ {
101
+ Console . WriteLine ( " " + scenario ) ;
33
102
}
103
+ Console . WriteLine ( ) ;
104
+
105
+ return scenarios ;
106
+ }
34
107
108
+ private static void StartInteractiveConsoleThread ( )
109
+ {
35
110
// Run the interaction on a separate thread as we don't have Console.KeyAvailable on .NET Core so can't
36
111
// do a pre-emptive check before we call Console.ReadKey (which blocks, hard)
112
+
113
+ var started = new ManualResetEvent ( false ) ;
114
+
37
115
var interactiveThread = new Thread ( ( ) =>
38
116
{
39
- Console . WriteLine ( ) ;
40
117
Console . WriteLine ( "Press 'C' to force GC or any other key to display GC stats" ) ;
118
+ Console . WriteLine ( ) ;
119
+
120
+ started . Set ( ) ;
41
121
42
122
while ( true )
43
123
{
@@ -60,10 +140,11 @@ public static void Main(string[] args)
60
140
}
61
141
}
62
142
} ) ;
143
+
63
144
interactiveThread . IsBackground = true ;
64
145
interactiveThread . Start ( ) ;
65
146
66
- host . Run ( ) ;
147
+ started . WaitOne ( ) ;
67
148
}
68
149
69
150
private static string GetAllocatedMemory ( bool forceFullCollection = false )
0 commit comments