Skip to content

Commit 3add750

Browse files
authored
Add failure microbenchmark (#1581)
1 parent a08e737 commit 3add750

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using BenchmarkDotNet.Attributes;
20+
using Grpc.Core;
21+
22+
namespace Grpc.AspNetCore.Microbenchmarks.Server
23+
{
24+
public class FailureStatusUnaryServerCallHandlerBenchmark : UnaryServerCallHandlerBenchmarkBase
25+
{
26+
public FailureStatusUnaryServerCallHandlerBenchmark()
27+
{
28+
Method = (service, request, context) => throw new RpcException(Status.DefaultCancelled);
29+
ExpectedStatus = "1";
30+
}
31+
32+
[Benchmark]
33+
public Task HandleCallAsync()
34+
{
35+
return InvokeUnaryRequestAsync();
36+
}
37+
}
38+
}

perf/Grpc.AspNetCore.Microbenchmarks/Server/UnaryServerCallHandlerBenchmarkBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class UnaryServerCallHandlerBenchmarkBase
5151
protected InterceptorCollection? Interceptors { get; set; }
5252
protected List<ICompressionProvider>? CompressionProviders { get; set; }
5353
protected string? ResponseCompressionAlgorithm { get; set; }
54+
protected Grpc.AspNetCore.Server.Model.UnaryServerMethod<TestService, ChatMessage, ChatMessage>? Method { get; set; }
55+
protected string ExpectedStatus { get; set; } = "0";
5456

5557
[GlobalSetup]
5658
public void GlobalSetup()
@@ -75,7 +77,7 @@ public void GlobalSetup()
7577
var result = Task.FromResult(message);
7678
_callHandler = new UnaryServerCallHandler<TestService, ChatMessage, ChatMessage>(
7779
new UnaryServerMethodInvoker<TestService, ChatMessage, ChatMessage>(
78-
(service, request, context) => result,
80+
Method ?? ((service, request, context) => result),
7981
method,
8082
HttpContextServerCallContextHelper.CreateMethodOptions(
8183
compressionProviders: CompressionProviders,
@@ -135,7 +137,7 @@ protected async Task InvokeUnaryRequestAsync()
135137
StringValues value;
136138
if (_trailers.TryGetValue("grpc-status", out value) || _headers.TryGetValue("grpc-status", out value))
137139
{
138-
if (!value.Equals("0"))
140+
if (!value.Equals(ExpectedStatus))
139141
{
140142
throw new InvalidOperationException("Unexpected grpc-status: " + Enum.Parse<StatusCode>(value));
141143
}

0 commit comments

Comments
 (0)