Skip to content

Commit dfe7be7

Browse files
authored
Migrate from WebHost to Host in test and perf websites (grpc#566)
1 parent 89cf633 commit dfe7be7

File tree

6 files changed

+124
-105
lines changed

6 files changed

+124
-105
lines changed

perf/benchmarkapps/GrpcAspNetCoreServer/Program.cs

+39-36
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public class Program
3333
{
3434
public static void Main(string[] args)
3535
{
36-
CreateWebHostBuilder(args).Build().Run();
36+
CreateHostBuilder(args).Build().Run();
3737
}
3838

39-
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
39+
public static IHostBuilder CreateHostBuilder(string[] args)
4040
{
4141
Console.WriteLine();
4242
Console.WriteLine("ASP.NET Core gRPC Benchmarks");
@@ -51,11 +51,42 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args)
5151
.AddCommandLine(args)
5252
.Build();
5353

54-
var webHostBuilder =
55-
WebHost.CreateDefaultBuilder(args)
56-
.UseContentRoot(Directory.GetCurrentDirectory())
57-
.UseConfiguration(config)
58-
.UseStartup<Startup>()
54+
var hostBuilder = Host.CreateDefaultBuilder(args)
55+
.ConfigureWebHostDefaults(webBuilder =>
56+
{
57+
webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
58+
webBuilder.UseConfiguration(config);
59+
webBuilder.UseStartup<Startup>();
60+
61+
webBuilder.ConfigureKestrel((context, options) =>
62+
{
63+
var endPoint = config.CreateIPEndPoint();
64+
65+
options.Listen(endPoint, listenOptions =>
66+
{
67+
var protocol = config["protocol"] ?? "";
68+
69+
Console.WriteLine($"Protocol: {protocol}");
70+
71+
if (protocol.Equals("h2", StringComparison.OrdinalIgnoreCase))
72+
{
73+
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
74+
75+
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
76+
var certPath = Path.Combine(basePath!, "Certs/testCert.pfx");
77+
listenOptions.UseHttps(certPath, "testPassword");
78+
}
79+
else if (protocol.Equals("h2c", StringComparison.OrdinalIgnoreCase))
80+
{
81+
listenOptions.Protocols = HttpProtocols.Http2;
82+
}
83+
else
84+
{
85+
throw new InvalidOperationException($"Unexpected protocol: {protocol}");
86+
}
87+
});
88+
});
89+
})
5990
.ConfigureLogging(loggerFactory =>
6091
{
6192
loggerFactory.ClearProviders();
@@ -69,37 +100,9 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args)
69100
.UseDefaultServiceProvider((context, options) =>
70101
{
71102
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
72-
})
73-
.ConfigureKestrel((context, options) =>
74-
{
75-
var endPoint = config.CreateIPEndPoint();
76-
77-
options.Listen(endPoint, listenOptions =>
78-
{
79-
var protocol = config["protocol"] ?? "";
80-
81-
Console.WriteLine($"Protocol: {protocol}");
82-
83-
if (protocol.Equals("h2", StringComparison.OrdinalIgnoreCase))
84-
{
85-
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
86-
87-
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
88-
var certPath = Path.Combine(basePath!, "Certs/testCert.pfx");
89-
listenOptions.UseHttps(certPath, "testPassword");
90-
}
91-
else if (protocol.Equals("h2c", StringComparison.OrdinalIgnoreCase))
92-
{
93-
listenOptions.Protocols = HttpProtocols.Http2;
94-
}
95-
else
96-
{
97-
throw new InvalidOperationException($"Unexpected protocol: {protocol}");
98-
}
99-
});
100103
});
101104

102-
return webHostBuilder;
105+
return hostBuilder;
103106
}
104107
}
105108
}

test/FunctionalTests/Client/MaxMessageSizeTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class MaxMessageSizeTests : FunctionalTestBase
3232
[Test]
3333
public async Task ReceivedMessageExceedsDefaultSize_ThrowError()
3434
{
35-
Task<HelloReply> UnaryDeadlineExceeded(HelloRequest request, ServerCallContext context)
35+
Task<HelloReply> ReturnLargeMessage(HelloRequest request, ServerCallContext context)
3636
{
3737
// Return message is 4 MB + 1 B. Default receive size is 4 MB
3838
return Task.FromResult(new HelloReply { Message = new string('!', (1024 * 1024 * 4) + 1) });
@@ -58,7 +58,7 @@ Task<HelloReply> UnaryDeadlineExceeded(HelloRequest request, ServerCallContext c
5858
return false;
5959
});
6060

61-
var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>(UnaryDeadlineExceeded);
61+
var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>(ReturnLargeMessage);
6262

6363
var channel = CreateChannel();
6464
channel.DisableClientDeadlineTimer = true;

testassets/BenchmarkWorkerWebsite/Program.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.AspNetCore.Hosting;
2323
using Microsoft.AspNetCore.Server.Kestrel.Core;
2424
using Microsoft.Extensions.Configuration;
25+
using Microsoft.Extensions.Hosting;
2526

2627
namespace BenchmarkWorkerWebsite
2728
{
@@ -30,26 +31,29 @@ public class Program
3031
static readonly CancellationTokenSource QuitWorkerCts = new CancellationTokenSource();
3132
public static async Task Main(string[] args)
3233
{
33-
await CreateWebHostBuilder(args).Build().RunAsync(QuitWorkerCts.Token);
34+
await CreateHostBuilder(args).Build().RunAsync(QuitWorkerCts.Token);
3435
}
3536

3637
public static void QuitWorker()
3738
{
3839
QuitWorkerCts.Cancel();
3940
}
4041

41-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
42-
WebHost.CreateDefaultBuilder(args)
43-
.ConfigureKestrel((context, options) =>
42+
public static IHostBuilder CreateHostBuilder(string[] args) =>
43+
Host.CreateDefaultBuilder(args)
44+
.ConfigureWebHostDefaults(webBuilder =>
4445
{
45-
// Port on which to listen to commands from QpsDriver
46-
int driverPort = context.Configuration.GetValue<int>("driver_port", 50053);
47-
48-
options.ListenAnyIP(driverPort, listenOptions =>
46+
webBuilder.ConfigureKestrel((context, options) =>
4947
{
50-
listenOptions.Protocols = HttpProtocols.Http2;
48+
// Port on which to listen to commands from QpsDriver
49+
int driverPort = context.Configuration.GetValue<int>("driver_port", 50053);
50+
51+
options.ListenAnyIP(driverPort, listenOptions =>
52+
{
53+
listenOptions.Protocols = HttpProtocols.Http2;
54+
});
5155
});
52-
})
53-
.UseStartup<Startup>();
56+
webBuilder.UseStartup<Startup>();
57+
});
5458
}
5559
}

testassets/BenchmarkWorkerWebsite/ServerRunners.cs

+32-28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Microsoft.AspNetCore.Hosting;
2727
using Microsoft.AspNetCore.Server.Kestrel.Core;
2828
using Microsoft.Extensions.DependencyInjection;
29+
using Microsoft.Extensions.Hosting;
2930
using Microsoft.Extensions.Logging;
3031

3132
namespace BenchmarkWorkerWebsite
@@ -42,7 +43,7 @@ public static IServerRunner CreateStarted(ServerConfig config, ILogger logger)
4243
{
4344
logger.LogInformation("ServerConfig: {0}", config);
4445

45-
var webHostBuilder = WebHost.CreateDefaultBuilder();
46+
var hostBuilder = Host.CreateDefaultBuilder();
4647

4748
if (config.AsyncServerThreads != 0)
4849
{
@@ -69,36 +70,39 @@ public static IServerRunner CreateStarted(ServerConfig config, ILogger logger)
6970
logger.LogWarning("Grpc.AspNetCore server doesn't support autoselecting of listening port. Setting port explictly to " + port);
7071
}
7172

72-
webHostBuilder.ConfigureKestrel((context, options) =>
73+
hostBuilder.ConfigureWebHostDefaults(webHostBuilder =>
7374
{
74-
options.ListenAnyIP(port, listenOptions =>
75+
webHostBuilder.ConfigureKestrel((context, options) =>
7576
{
76-
// TODO(jtattermusch): use TLS if config.SecurityParams != null
77-
listenOptions.Protocols = HttpProtocols.Http2;
77+
options.ListenAnyIP(port, listenOptions =>
78+
{
79+
// TODO(jtattermusch): use TLS if config.SecurityParams != null
80+
listenOptions.Protocols = HttpProtocols.Http2;
81+
});
7882
});
79-
});
8083

81-
if (config.ServerType == ServerType.AsyncServer)
82-
{
83-
GrpcPreconditions.CheckArgument(config.PayloadConfig == null,
84-
"ServerConfig.PayloadConfig shouldn't be set for BenchmarkService based server.");
85-
webHostBuilder.UseStartup<BenchmarkServiceStartup>();
86-
}
87-
else if (config.ServerType == ServerType.AsyncGenericServer)
88-
{
89-
var genericService = new GenericServiceImpl(config.PayloadConfig.BytebufParams.RespSize);
90-
// TODO(jtattermusch): use startup with given generic service
91-
throw new ArgumentException("Generice service is not yet implemented.");
92-
}
93-
else
94-
{
95-
throw new ArgumentException("Unsupported ServerType");
96-
}
84+
if (config.ServerType == ServerType.AsyncServer)
85+
{
86+
GrpcPreconditions.CheckArgument(config.PayloadConfig == null,
87+
"ServerConfig.PayloadConfig shouldn't be set for BenchmarkService based server.");
88+
webHostBuilder.UseStartup<BenchmarkServiceStartup>();
89+
}
90+
else if (config.ServerType == ServerType.AsyncGenericServer)
91+
{
92+
var genericService = new GenericServiceImpl(config.PayloadConfig.BytebufParams.RespSize);
93+
// TODO(jtattermusch): use startup with given generic service
94+
throw new ArgumentException("Generice service is not yet implemented.");
95+
}
96+
else
97+
{
98+
throw new ArgumentException("Unsupported ServerType");
99+
}
100+
});
97101

98102
// Don't log requests handled by the benchmarking service
99-
webHostBuilder.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning));
103+
hostBuilder.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning));
100104

101-
var webHost = webHostBuilder.Build();
105+
var webHost = hostBuilder.Build();
102106
webHost.Start();
103107
return new ServerRunnerImpl(webHost, logger, port);
104108
}
@@ -150,14 +154,14 @@ await requestStream.ForEachAsync(async request =>
150154
/// </summary>
151155
public class ServerRunnerImpl : IServerRunner
152156
{
153-
readonly IWebHost webHost;
157+
readonly IHost host;
154158
readonly ILogger logger;
155159
readonly int boundPort;
156160
readonly TimeStats timeStats = new TimeStats();
157161

158-
public ServerRunnerImpl(IWebHost webHost, ILogger logger, int boundPort)
162+
public ServerRunnerImpl(IHost host, ILogger logger, int boundPort)
159163
{
160-
this.webHost = webHost;
164+
this.host = host;
161165
this.logger = logger;
162166
this.boundPort = boundPort;
163167
}
@@ -189,7 +193,7 @@ public ServerStats GetStats(bool reset)
189193
/// <returns>Task that finishes when server has shutdown.</returns>
190194
public Task StopAsync()
191195
{
192-
return webHost.StopAsync();
196+
return host.StopAsync();
193197
}
194198
}
195199
}

testassets/FunctionalTestsWebsite/Program.cs

+12-8
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,29 @@
1919
using Microsoft.AspNetCore;
2020
using Microsoft.AspNetCore.Hosting;
2121
using Microsoft.AspNetCore.Server.Kestrel.Core;
22+
using Microsoft.Extensions.Hosting;
2223

2324
namespace FunctionalTestsWebsite
2425
{
2526
public class Program
2627
{
2728
public static void Main(string[] args)
2829
{
29-
CreateWebHostBuilder(args).Build().Run();
30+
CreateHostBuilder(args).Build().Run();
3031
}
3132

32-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
33-
WebHost.CreateDefaultBuilder(args)
34-
.ConfigureKestrel(options =>
33+
public static IHostBuilder CreateHostBuilder(string[] args) =>
34+
Host.CreateDefaultBuilder(args)
35+
.ConfigureWebHostDefaults(webBuilder =>
3536
{
36-
options.ListenLocalhost(50051, listenOptions =>
37+
webBuilder.ConfigureKestrel(options =>
3738
{
38-
listenOptions.Protocols = HttpProtocols.Http2;
39+
options.ListenLocalhost(50051, listenOptions =>
40+
{
41+
listenOptions.Protocols = HttpProtocols.Http2;
42+
});
3943
});
40-
})
41-
.UseStartup<Startup>();
44+
webBuilder.UseStartup<Startup>();
45+
});
4246
}
4347
}

testassets/InteropTestsWebsite/Program.cs

+24-20
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,44 @@
2222
using Microsoft.AspNetCore.Hosting;
2323
using Microsoft.AspNetCore.Server.Kestrel.Core;
2424
using Microsoft.Extensions.Configuration;
25+
using Microsoft.Extensions.Hosting;
2526

2627
namespace InteropTestsWebsite
2728
{
2829
public class Program
2930
{
3031
public static void Main(string[] args)
3132
{
32-
CreateWebHostBuilder(args).Build().Run();
33+
CreateHostBuilder(args).Build().Run();
3334
}
3435

35-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
36-
WebHost.CreateDefaultBuilder(args)
37-
.ConfigureKestrel((context, options) =>
36+
public static IHostBuilder CreateHostBuilder(string[] args) =>
37+
Host.CreateDefaultBuilder(args)
38+
.ConfigureWebHostDefaults(webBuilder =>
3839
{
39-
// Support --port and --use_tls cmdline arguments normally supported
40-
// by gRPC interop servers.
41-
int port = context.Configuration.GetValue<int>("port", 50052);
42-
bool useTls = context.Configuration.GetValue<bool>("use_tls", false);
43-
44-
options.Limits.MinRequestBodyDataRate = null;
45-
options.ListenAnyIP(port, listenOptions =>
40+
webBuilder.ConfigureKestrel((context, options) =>
4641
{
47-
Console.WriteLine($"Enabling connection encryption: {useTls}");
42+
// Support --port and --use_tls cmdline arguments normally supported
43+
// by gRPC interop servers.
44+
int port = context.Configuration.GetValue<int>("port", 50052);
45+
bool useTls = context.Configuration.GetValue<bool>("use_tls", false);
4846

49-
if (useTls)
47+
options.Limits.MinRequestBodyDataRate = null;
48+
options.ListenAnyIP(port, listenOptions =>
5049
{
51-
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
52-
var certPath = Path.Combine(basePath!, "Certs", "server1.pfx");
50+
Console.WriteLine($"Enabling connection encryption: {useTls}");
51+
52+
if (useTls)
53+
{
54+
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
55+
var certPath = Path.Combine(basePath!, "Certs", "server1.pfx");
5356

54-
listenOptions.UseHttps(certPath, "1111");
55-
}
56-
listenOptions.Protocols = HttpProtocols.Http2;
57+
listenOptions.UseHttps(certPath, "1111");
58+
}
59+
listenOptions.Protocols = HttpProtocols.Http2;
60+
});
5761
});
58-
})
59-
.UseStartup<Startup>();
62+
webBuilder.UseStartup<Startup>();
63+
});
6064
}
6165
}

0 commit comments

Comments
 (0)