Skip to content

Commit fa502de

Browse files
committed
Ability to enable specific scenarios:
- Updated for rename too - Removed hosting.json
1 parent 47c6d36 commit fa502de

File tree

5 files changed

+197
-92
lines changed

5 files changed

+197
-92
lines changed

src/Benchmarks/MultipleQueriesDapperMiddleware.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Data.Common;
6-
using System.Linq;
76
using System.Threading.Tasks;
87
using Benchmarks.Data;
98
using Dapper;

src/Benchmarks/Program.cs

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,122 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Linq;
56
using System.Threading;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
810

911
namespace Benchmarks
1012
{
1113
public class Program
1214
{
15+
public static string[] Args;
16+
1317
public static void Main(string[] args)
1418
{
15-
var host = new WebHostBuilder()
16-
.UseDefaultConfiguration(args)
17-
.UseStartup<Startup>()
18-
.Build();
19+
Args = args;
20+
1921
Console.WriteLine();
20-
Console.WriteLine("ASP.NET 5 Benchmarks");
21-
Console.WriteLine("--------------------");
22+
Console.WriteLine("ASP.NET Core Benchmarks");
23+
Console.WriteLine("-----------------------");
2224

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))
2535
.Build();
2636

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
2860
{
2961
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);
33102
}
103+
Console.WriteLine();
104+
105+
return scenarios;
106+
}
34107

108+
private static void StartInteractiveConsoleThread()
109+
{
35110
// Run the interaction on a separate thread as we don't have Console.KeyAvailable on .NET Core so can't
36111
// do a pre-emptive check before we call Console.ReadKey (which blocks, hard)
112+
113+
var started = new ManualResetEvent(false);
114+
37115
var interactiveThread = new Thread(() =>
38116
{
39-
Console.WriteLine();
40117
Console.WriteLine("Press 'C' to force GC or any other key to display GC stats");
118+
Console.WriteLine();
119+
120+
started.Set();
41121

42122
while (true)
43123
{
@@ -60,10 +140,11 @@ public static void Main(string[] args)
60140
}
61141
}
62142
});
143+
63144
interactiveThread.IsBackground = true;
64145
interactiveThread.Start();
65146

66-
host.Run();
147+
started.WaitOne();
67148
}
68149

69150
private static string GetAllocatedMemory(bool forceFullCollection = false)

src/Benchmarks/Scenarios.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Linq;
5+
6+
namespace Benchmarks
7+
{
8+
public class Scenarios
9+
{
10+
public bool Plaintext { get; set; }
11+
12+
public bool Json { get; set; }
13+
14+
public bool StaticFiles { get; set; }
15+
16+
public bool MvcApis { get; set; }
17+
18+
public bool MvcViews { get; set; }
19+
20+
public bool DbSingleQueryRaw { get; set; }
21+
22+
public bool DbSingleQueryEf { get; set; }
23+
24+
public bool DbSingleQueryDapper { get; set; }
25+
26+
public bool DbMultiQueryRaw { get; set; }
27+
28+
public bool DbMultiQueryEf { get; set; }
29+
30+
public bool DbMultiQueryDapper { get; set; }
31+
32+
public bool DbFortunesRaw { get; set; }
33+
34+
public bool DbFortunesEf { get; set; }
35+
36+
public bool DbFortunesDapper { get; set; }
37+
38+
public bool Any(string partialName) =>
39+
typeof(Scenarios).GetTypeInfo().DeclaredProperties
40+
.Where(p => p.Name.IndexOf(partialName, StringComparison.Ordinal) >= 0 && (bool)p.GetValue(this))
41+
.Any();
42+
43+
public IEnumerable<string> GetNames() =>
44+
typeof(Scenarios).GetTypeInfo().DeclaredProperties
45+
.Select(p => p.Name);
46+
47+
public IEnumerable<string> GetEnabled() =>
48+
typeof(Scenarios).GetTypeInfo().DeclaredProperties
49+
.Where(p => p.GetValue(this) is bool && (bool)p.GetValue(this))
50+
.Select(p => p.Name);
51+
52+
public int Enable(string partialName)
53+
{
54+
var props = typeof(Scenarios).GetTypeInfo().DeclaredProperties
55+
.Where(p => string.Equals(partialName, "[all]", StringComparison.OrdinalIgnoreCase) || p.Name.IndexOf(partialName, StringComparison.OrdinalIgnoreCase) >= 0)
56+
.ToList();
57+
58+
foreach (var p in props)
59+
{
60+
p.SetValue(this, true);
61+
}
62+
63+
return props.Count;
64+
}
65+
66+
public void EnableDefault()
67+
{
68+
Plaintext = true;
69+
Json = true;
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)