Skip to content

Commit f43aa66

Browse files
authored
Add scenarios for route handler filters (#1727)
* Add scenarios for route handler filters * Add new test scenarios * Update code samples
1 parent 0055ec0 commit f43aa66

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

build/trend-scenarios.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@ parameters:
3838
arguments: --scenario mapaction $(plaintextJobs) --property scenario=PlaintextMapAction --property protocol=http
3939
- displayName: Plaintext Endpoint
4040
arguments: --scenario endpoint $(plaintextJobs) --property scenario=PlaintextEndpoint --property protocol=http
41+
- displayName: Plaintext (No Filters)
42+
arguments: --scenario plainTextSansFilter $(plaintextJobs) --property scenario=PlaintextWithParametersNoFilter --property protocol=http
43+
- displayName: Plaintext (Empty Filters)
44+
arguments: --scenario plainTextEmptyFilter $(plaintextJobs) --property scenario=PlaintextWithParametersEmptyFilter --property protocol=http
45+
- displayName: Plaintext (Empty Filters, No Parameters)
46+
arguments: --scenario plainTextNoParamsEmptyFilter $(plaintextJobs) --property scenario=PlaintextNoParametersEmptyFilter --property protocol=http
47+
- displayName: Plaintext (With Filters)
48+
arguments: --scenario plainTextWithFilter $(plaintextJobs) --property scenario=PlaintextWithParameterstWithFilter --property protocol=http
4149
- displayName: Plaintext Connection Close
4250
arguments: --scenario connectionclose $(plaintextJobs) --property scenario=ConnectionClose --property protocol=http --variable connections=32 --property connections=32
4351
- displayName: Plaintext Connection Close Https
4452
arguments: --scenario connectionclosehttps $(plaintextJobs) --property scenario=ConnectionCloseHttps --property protocol=https --variable connections=32 --property connections=32
45-
- displayName: Plaintext Connection Close Https HttpSys
4653
arguments: --scenario connectionclosehttps $(plaintextJobs) --property scenario=ConnectionCloseHttpsHttpSys --property protocol=https --variable connections=32 --property connections=32 --variable server=HttpSys --application.options.requiredOperatingSystem windows
4754

4855
# Json

scenarios/plaintext.benchmarks.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,46 @@ scenarios:
9292
presetHeaders: plaintext
9393
path: /plaintext
9494
pipeline: 16
95+
96+
plainTextSansFilter:
97+
application:
98+
job: mapaction
99+
load:
100+
job: wrk
101+
variables:
102+
presetHeaders: plaintext
103+
path: /hello/TesterName/42/Internet
104+
pipeline: 16
105+
106+
plainTextEmptyFilter:
107+
application:
108+
job: mapaction
109+
load:
110+
job: wrk
111+
variables:
112+
presetHeaders: plaintext
113+
path: /helloEmptyFilter/TesterName/42/Internet
114+
pipeline: 16
115+
116+
plainTextNoParamsEmptyFilter:
117+
application:
118+
job: mapaction
119+
load:
120+
job: wrk
121+
variables:
122+
presetHeaders: plaintext
123+
path: /plaintextNoParamsWithFilter
124+
pipeline: 16
125+
126+
plainTextWithFilter:
127+
application:
128+
job: mapaction
129+
load:
130+
job: wrk
131+
variables:
132+
presetHeaders: plaintext
133+
path: /helloFiltered/TesterName/42/Internet
134+
pipeline: 16
95135

96136
connectionclose:
97137
application:

src/BenchmarksApps/MapAction/Program.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Reflection;
23
using Microsoft.AspNetCore.Builder;
34
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.Http;
46
using Microsoft.AspNetCore.Mvc;
57
using Microsoft.Extensions.Hosting;
68
using Microsoft.Extensions.Logging;
@@ -23,6 +25,32 @@
2325

2426
object Json() => new { message = "Hello, World!" };
2527
endpoints.MapGet("/json", (Func<object>)Json);
28+
29+
// Parameterized plain-text endpoint
30+
string SayHello(string name, int age, string location) => $"Hello, {name}! You're {age} years old and based in {location}.";
31+
// With no filters
32+
endpoints.MapGet("/hello/{name}/{age}/{location}", SayHello);
33+
// With a filter that no-ops
34+
endpoints.MapGet("/helloEmptyFilter/{name}/{age}/{location}", SayHello)
35+
.AddFilter((context, next) => next(context));
36+
// With filter on endpoint with no parameters
37+
endpoints.MapGet("/plaintextNoParamsWithFilter", Plaintext)
38+
.AddFilter((context, next) => next(context));
39+
endpoints
40+
.MapGet("/helloFiltered/{name}/{age}/{location}", SayHello)
41+
.AddFilter((routeHandlerContext, next) => {
42+
var hasParams = routeHandlerContext.MethodInfo.GetParameters().Length >= 1;
43+
var isStringParam = routeHandlerContext.MethodInfo.GetParameters()[0].ParameterType == typeof(string);
44+
45+
return (RouteHandlerFilterDelegate)((context) =>
46+
{
47+
if (hasParams && isStringParam)
48+
{
49+
context.Parameters[0] = ((string)context.Parameters[0]).ToUpperInvariant();
50+
}
51+
return next(context);
52+
});
53+
});
2654
});
2755

2856
});

0 commit comments

Comments
 (0)