Skip to content

Commit 4acaef9

Browse files
committed
Fixed the benchmarks
- Use the new hosting API - Found a CoreCLR bug with intercept = true on *nix
1 parent 1f74312 commit 4acaef9

File tree

3 files changed

+37
-73
lines changed

3 files changed

+37
-73
lines changed

src/Benchmarks/Program.cs

Lines changed: 30 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
using System;
55
using System.Threading;
66
using Microsoft.AspNet.Hosting;
7-
using Microsoft.AspNet.Http.Features;
8-
using Microsoft.AspNet.Server.Features;
9-
using Microsoft.Extensions.Configuration;
107
using Microsoft.Extensions.DependencyInjection;
118

129
namespace Benchmarks
@@ -15,81 +12,43 @@ public class Program
1512
{
1613
public static void Main(string[] args)
1714
{
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(() =>
3123
{
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");
3526

36-
var serverAddresses = app.ServerFeatures.Get<IServerAddressesFeature>();
37-
if (serverAddresses != null)
27+
while (true)
3828
{
39-
foreach (var address in serverAddresses.Addresses)
29+
var key = Console.ReadKey(intercept: true);
30+
31+
if (key.Key == ConsoleKey.C)
4032
{
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!");
4239
}
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
5741
{
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)}");
7545
}
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();
9352
}
9453

9554
private static string GetAllocatedMemory(bool forceFullCollection = false)

src/Benchmarks/hosting.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"server": "Microsoft.AspNet.Server.Kestrel",
3-
"server.urls": "http://*:5001"
3+
"server.urls": "http://*:5001",
4+
"captureStartupErrors": true
45
}

src/Benchmarks/project.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626

2727
"frameworks": {
2828
"dnx451": { },
29-
"dnxcore50": { }
29+
"dnxcore50": {
30+
"dependencies": {
31+
"System.Runtime.Serialization.Primitives": "4.1.0-*"
32+
}
33+
}
3034
},
3135

3236
"publishExclude": [

0 commit comments

Comments
 (0)