Skip to content

Commit 2b1ad6d

Browse files
authored
Add h2load benchmark scenarios and improve local perf client (#193)
1 parent dab1585 commit 2b1ad6d

19 files changed

+236
-32
lines changed

Grpc.DotNet.sln

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{310E57
8989
perf\benchmarkapps\Shared\ca.crt = perf\benchmarkapps\Shared\ca.crt
9090
perf\benchmarkapps\Shared\client.crt = perf\benchmarkapps\Shared\client.crt
9191
perf\benchmarkapps\Shared\client.key = perf\benchmarkapps\Shared\client.key
92+
perf\benchmarkapps\Shared\data.proto = perf\benchmarkapps\Shared\data.proto
9293
perf\benchmarkapps\Shared\greet.proto = perf\benchmarkapps\Shared\greet.proto
93-
perf\benchmarkapps\Shared\Greeter.cs = perf\benchmarkapps\Shared\Greeter.cs
94+
perf\benchmarkapps\Shared\GreetService.cs = perf\benchmarkapps\Shared\GreetService.cs
9495
perf\benchmarkapps\Shared\server.pfx = perf\benchmarkapps\Shared\server.pfx
9596
EndProjectSection
9697
EndProject

perf/benchmarkapps/BenchmarkClient/BenchmarkClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<Protobuf Include="..\Shared\greet.proto" GrpcServices="Client" Link="Protos\greet.proto" />
11+
<Protobuf Include="..\Shared\data.proto" GrpcServices="Client" Link="Protos\data.proto" />
1112

1213
<Compile Include="$(MSBuildThisFileDirectory)..\..\Shared\*.cs" />
1314

perf/benchmarkapps/BenchmarkClient/Program.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ namespace BenchmarkClient
2727
{
2828
class Program
2929
{
30-
private const int Connections = 1;
31-
private const int DurationSeconds = 5;
30+
private const int Connections = 8;
31+
private const int DurationSeconds = 15;
32+
private const bool UseClientCertificate = false;
3233
// The host name is tied to some certificates
3334
private const string Target = "localhost:50051";
3435
private readonly static bool StopOnError = false;
@@ -39,11 +40,11 @@ static async Task Main(string[] args)
3940

4041
var benchmarkResults = new List<BenchmarkResult>();
4142

42-
benchmarkResults.Add(await ExecuteBenchmark("GrpcHttpClientUnary", id => new GrpcHttpClientUnaryWorker(id, "https://" + Target)));
43-
benchmarkResults.Add(await ExecuteBenchmark("JsonRaw", id => new JsonWorker(id, Target, "/raw/greeter")));
44-
benchmarkResults.Add(await ExecuteBenchmark("JsonMvc", id => new JsonWorker(id, Target, "/api/greeter")));
45-
benchmarkResults.Add(await ExecuteBenchmark("GrpcCoreUnary", id => new GrpcCoreUnaryWorker(id, Target, useClientCertificate: true)));
46-
benchmarkResults.Add(await ExecuteBenchmark("GrpcRawUnary", id => new GrpcRawUnaryWorker(id, Target)));
43+
benchmarkResults.Add(await ExecuteBenchmark("GrpcHttpClientUnary", id => new GrpcHttpClientUnaryWorker(id, Target, UseClientCertificate)));
44+
benchmarkResults.Add(await ExecuteBenchmark("JsonRaw", id => new JsonWorker(id, Target, UseClientCertificate, "/raw/greeter")));
45+
benchmarkResults.Add(await ExecuteBenchmark("JsonMvc", id => new JsonWorker(id, Target, UseClientCertificate, "/api/greeter")));
46+
benchmarkResults.Add(await ExecuteBenchmark("GrpcCoreUnary", id => new GrpcCoreUnaryWorker(id, Target, UseClientCertificate)));
47+
benchmarkResults.Add(await ExecuteBenchmark("GrpcRawUnary", id => new GrpcRawUnaryWorker(id, Target, UseClientCertificate)));
4748

4849
Log($"Results:");
4950

perf/benchmarkapps/BenchmarkClient/Workers/GrpcCoreUnaryWorker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace BenchmarkClient.Workers
2727
public class GrpcCoreUnaryWorker : IWorker
2828
{
2929
private Channel? _channel;
30-
private Greeter.GreeterClient? _client;
30+
private GreetService.GreetServiceClient? _client;
3131
private bool _useClientCertificate;
3232

3333
public GrpcCoreUnaryWorker(int id, string target, bool useClientCertificate)
@@ -53,7 +53,7 @@ public Task ConnectAsync()
5353
: ChannelCredentials.Insecure;
5454

5555
_channel = new Channel(Target, credentials);
56-
_client = new Greeter.GreeterClient(_channel);
56+
_client = new GreetService.GreetServiceClient(_channel);
5757

5858
return _channel.ConnectAsync();
5959
}

perf/benchmarkapps/BenchmarkClient/Workers/GrpcHttpClientUnaryWorker.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,20 @@ namespace BenchmarkClient.Workers
2727
{
2828
public class GrpcHttpClientUnaryWorker : IWorker
2929
{
30+
private readonly bool _useClientCertificate;
3031
private readonly DateTime? _deadline;
31-
private Greeter.GreeterClient? _client;
32+
private GreetService.GreetServiceClient? _client;
3233

33-
public GrpcHttpClientUnaryWorker(int id, string baseUri, DateTime? deadline = null)
34+
public GrpcHttpClientUnaryWorker(int id, string target, bool useClientCertificate, DateTime? deadline = null)
3435
{
3536
Id = id;
36-
BaseUri = baseUri;
37+
Target = target;
38+
_useClientCertificate = useClientCertificate;
3739
_deadline = deadline;
3840
}
3941

4042
public int Id { get; }
41-
public string BaseUri { get; }
43+
public string Target { get; }
4244

4345
public async Task CallAsync()
4446
{
@@ -49,8 +51,11 @@ public async Task CallAsync()
4951

5052
public Task ConnectAsync()
5153
{
52-
var channel = GrpcChannel.ForAddress(new Uri(BaseUri, UriKind.RelativeOrAbsolute));
53-
_client = new Greeter.GreeterClient(channel);
54+
var url = _useClientCertificate ? "https://" : "http://";
55+
url += Target;
56+
57+
var channel = GrpcChannel.ForAddress(new Uri(url, UriKind.RelativeOrAbsolute));
58+
_client = new GreetService.GreetServiceClient(channel);
5459

5560
return Task.CompletedTask;
5661
}

perf/benchmarkapps/BenchmarkClient/Workers/GrpcRawUnaryWorker.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ namespace BenchmarkClient.Workers
3030
{
3131
public class GrpcRawUnaryWorker : IWorker
3232
{
33+
private readonly bool _useClientCertificate;
3334
private readonly DateTime? _deadline;
3435
private HttpClient? _client;
3536

36-
public GrpcRawUnaryWorker(int id, string target, DateTime? deadline = null)
37+
public GrpcRawUnaryWorker(int id, string target, bool useClientCertificate, DateTime? deadline = null)
3738
{
3839
Id = id;
3940
Target = target;
41+
_useClientCertificate = useClientCertificate;
4042
_deadline = deadline;
4143
}
4244

@@ -59,7 +61,10 @@ public async Task CallAsync()
5961
BinaryPrimitives.WriteUInt32BigEndian(data.AsSpan(1, 4), (uint)messageSize);
6062
messageBytes.CopyTo(data.AsSpan(5));
6163

62-
var request = new HttpRequestMessage(HttpMethod.Post, "https://" + Target + "/Greet.Greeter/SayHello");
64+
var url = _useClientCertificate ? "https://" : "http://";
65+
url += Target;
66+
67+
var request = new HttpRequestMessage(HttpMethod.Post, url + "/Greet.GreetService/SayHello");
6368
request.Version = new Version(2, 0);
6469
request.Content = new StreamContent(new MemoryStream(data));
6570
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/grpc");

perf/benchmarkapps/BenchmarkClient/Workers/JsonWorker.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ namespace BenchmarkClient.Workers
2929
{
3030
public class JsonWorker : IWorker
3131
{
32+
private readonly bool _useClientCertificate;
3233
private readonly string _path;
3334
private HttpClient? _client;
3435

35-
public JsonWorker(int id, string target, string path)
36+
public JsonWorker(int id, string target, bool useClientCertificate, string path)
3637
{
3738
Id = id;
3839
Target = target;
40+
_useClientCertificate = useClientCertificate;
3941
_path = path;
4042
}
4143

@@ -52,7 +54,10 @@ public async Task CallAsync()
5254
var json = JsonConvert.SerializeObject(message);
5355
var data = Encoding.UTF8.GetBytes(json);
5456

55-
var request = new HttpRequestMessage(HttpMethod.Post, "https://" + Target + _path);
57+
var url = _useClientCertificate ? "https://" : "http://";
58+
url += Target;
59+
60+
var request = new HttpRequestMessage(HttpMethod.Post, url + _path);
5661
request.Version = new Version(2, 0);
5762
request.Content = new StreamContent(new MemoryStream(data));
5863
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

perf/benchmarkapps/BenchmarkServer/BenchmarkServer.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
<ItemGroup>
1515
<Protobuf Include="..\Shared\greet.proto" GrpcServices="Server" Link="Protos\greet.proto" />
16+
<Protobuf Include="..\Shared\data.proto" GrpcServices="Server" Link="Protos\data.proto" />
1617

1718
<None Include="..\..\..\examples\Certs\*.*" LinkBase="Certs">
1819
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1920
</None>
2021

2122
<Compile Include="..\Shared\BenchmarkConfigurationHelpers.cs" Link="BenchmarkConfigurationHelpers.cs" />
22-
<Compile Include="..\Shared\Greeter.cs" Link="Greeter.cs" />
23+
<Compile Include="..\Shared\GreetService.cs" Link="Services\GreetService.cs" />
24+
<Compile Include="..\Shared\DataService.cs" Link="Services\DataService.cs" />
2325

2426
<Content Update="hosting.json">
2527
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

perf/benchmarkapps/BenchmarkServer/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args)
9797
});
9898
}
9999

100-
listenOptions.Protocols = HttpProtocols.Http2;
100+
var httpProtocols = Enum.Parse<HttpProtocols>(context.Configuration["HttpProtocols"]);
101+
Console.WriteLine($"Protocol: {httpProtocols}");
102+
103+
listenOptions.Protocols = httpProtocols;
101104
});
102105
});
103106

perf/benchmarkapps/BenchmarkServer/Startup.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public void Configure(IApplicationBuilder app)
4545
app.UseEndpoints(endpoints =>
4646
{
4747
endpoints.MapGrpcService<GreeterService>();
48+
endpoints.MapGrpcService<DataService>();
49+
50+
endpoints.MapControllers();
51+
52+
endpoints.MapGet("/", context =>
53+
{
54+
return context.Response.WriteAsync("Benchmark Server");
55+
});
4856

4957
endpoints.MapPost("/raw/greeter", async context =>
5058
{
@@ -66,8 +74,6 @@ public void Configure(IApplicationBuilder app)
6674
ms.Seek(0, SeekOrigin.Begin);
6775
await ms.CopyToAsync(context.Response.Body);
6876
});
69-
70-
endpoints.MapControllers();
7177
});
7278
}
7379
}

0 commit comments

Comments
 (0)