4
4
using System ;
5
5
using System . Threading ;
6
6
using Microsoft . AspNet . Hosting ;
7
- using Microsoft . AspNet . Http . Features ;
8
- using Microsoft . AspNet . Server . Features ;
9
- using Microsoft . Extensions . Configuration ;
10
7
using Microsoft . Extensions . DependencyInjection ;
11
8
12
9
namespace Benchmarks
@@ -15,81 +12,43 @@ public class Program
15
12
{
16
13
public static void Main ( string [ ] args )
17
14
{
18
- var hostingConfig = new ConfigurationBuilder ( )
19
- . AddJsonFile ( "hosting.json" , optional : true )
20
- . AddEnvironmentVariables ( )
21
- . AddEnvironmentVariables ( prefix : "ASPNET_" )
22
- . AddCommandLine ( args )
23
- . Build ( ) ;
24
-
25
- var hostBuilder = new WebHostBuilder ( hostingConfig , captureStartupErrors : true ) ;
26
- hostBuilder . UseStartup ( typeof ( Startup ) ) ;
27
-
28
- var host = hostBuilder . Build ( ) ;
29
-
30
- using ( var app = host . Start ( ) )
15
+ var app = new WebApplicationBuilder ( )
16
+ . UseConfiguration ( WebApplicationConfiguration . GetDefault ( args ) )
17
+ . UseStartup < Startup > ( )
18
+ . Build ( ) ;
19
+
20
+ // Run the interaction on a separate thread as we don't have Console.KeyAvailable on .NET Core so can't
21
+ // do a pre-emptive check before we call Console.ReadKey (which blocks, hard)
22
+ var interactiveThread = new Thread ( ( ) =>
31
23
{
32
- // Echo out the addresses we're listening on
33
- var hostingEnv = app . Services . GetRequiredService < IHostingEnvironment > ( ) ;
34
- Console . WriteLine ( "Hosting environment: " + hostingEnv . EnvironmentName ) ;
24
+ Console . WriteLine ( ) ;
25
+ Console . WriteLine ( "Press 'C' to force GC or any other key to display GC stats" ) ;
35
26
36
- var serverAddresses = app . ServerFeatures . Get < IServerAddressesFeature > ( ) ;
37
- if ( serverAddresses != null )
27
+ while ( true )
38
28
{
39
- foreach ( var address in serverAddresses . Addresses )
29
+ var key = Console . ReadKey ( intercept : true ) ;
30
+
31
+ if ( key . Key == ConsoleKey . C )
40
32
{
41
- Console . WriteLine ( "Now listening on: " + address ) ;
33
+ Console . WriteLine ( ) ;
34
+ Console . Write ( "Forcing GC..." ) ;
35
+ GC . Collect ( ) ;
36
+ GC . WaitForPendingFinalizers ( ) ;
37
+ GC . Collect ( ) ;
38
+ Console . WriteLine ( " done!" ) ;
42
39
}
43
- }
44
-
45
- Console . WriteLine ( "Application started. Press Ctrl+C to shut down." ) ;
46
-
47
- var appLifetime = app . Services . GetRequiredService < IApplicationLifetime > ( ) ;
48
-
49
- // Run the interaction on a separate thread as we don't have Console.KeyAvailable on .NET Core so can't
50
- // do a pre-emptive check before we call Console.ReadKey (which blocks, hard)
51
- var interactiveThread = new Thread ( ( ) =>
52
- {
53
- Console . WriteLine ( ) ;
54
- Console . WriteLine ( "Press 'C' to force GC or any other key to display GC stats" ) ;
55
-
56
- while ( true )
40
+ else
57
41
{
58
- var key = Console . ReadKey ( intercept : true ) ;
59
-
60
- if ( key . Key == ConsoleKey . C )
61
- {
62
- Console . WriteLine ( ) ;
63
- Console . Write ( "Forcing GC..." ) ;
64
- GC . Collect ( ) ;
65
- GC . WaitForPendingFinalizers ( ) ;
66
- GC . Collect ( ) ;
67
- Console . WriteLine ( " done!" ) ;
68
- }
69
- else
70
- {
71
- Console . WriteLine ( ) ;
72
- Console . WriteLine ( $ "Allocated: { GetAllocatedMemory ( ) } ") ;
73
- Console . WriteLine ( $ "Gen 0: { GC . CollectionCount ( 0 ) } , Gen 1: { GC . CollectionCount ( 1 ) } , Gen 2: { GC . CollectionCount ( 2 ) } ") ;
74
- }
42
+ Console . WriteLine ( ) ;
43
+ Console . WriteLine ( $ "Allocated: { GetAllocatedMemory ( ) } ") ;
44
+ Console . WriteLine ( $ "Gen 0: { GC . CollectionCount ( 0 ) } , Gen 1: { GC . CollectionCount ( 1 ) } , Gen 2: { GC . CollectionCount ( 2 ) } ") ;
75
45
}
76
- } ) ;
77
-
78
- // Handle Ctrl+C in order to gracefully shutdown the web server
79
- Console . CancelKeyPress += ( sender , eventArgs ) =>
80
- {
81
- Console . WriteLine ( ) ;
82
- Console . WriteLine ( "Shutting down application..." ) ;
83
-
84
- appLifetime . StopApplication ( ) ;
85
-
86
- eventArgs . Cancel = true ;
87
- } ;
88
-
89
- interactiveThread . Start ( ) ;
90
-
91
- appLifetime . ApplicationStopping . WaitHandle . WaitOne ( ) ;
92
- }
46
+ }
47
+ } ) ;
48
+ interactiveThread . IsBackground = true ;
49
+ interactiveThread . Start ( ) ;
50
+
51
+ app . Run ( ) ;
93
52
}
94
53
95
54
private static string GetAllocatedMemory ( bool forceFullCollection = false )
0 commit comments