Skip to content

Commit 60c7ac1

Browse files
committed
More tweaks & add JSON auto-chunked
1 parent 66029f3 commit 60c7ac1

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

src/BenchmarksApps/TechEmpower/Kestrel/BenchmarkApp.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,32 @@ public Task ProcessRequestAsync(IFeatureCollection features)
1515

1616
if (req.Method != "GET")
1717
{
18-
res.StatusCode = 405;
19-
var body = features.GetResponseBodyFeature();
20-
return body.StartAsync().ContinueWith(t => body.CompleteAsync());
18+
res.StatusCode = StatusCodes.Status405MethodNotAllowed;
2119
}
2220

2321
return req.Path switch
2422
{
25-
"/plaintext" => Plaintext(req, res, features),
26-
"/json" => Json(req, res, features),
27-
"/" => Index(req, res, features),
28-
_ => NotFound(req, res, features),
23+
"/plaintext" => Plaintext(res, features),
24+
"/json" => Json(res, features),
25+
"/json-chunked" => JsonChunked(res, features),
26+
"/" => Index(res, features),
27+
_ => NotFound(res, features),
2928
};
3029
}
3130

32-
private static async Task NotFound(IHttpRequestFeature req, IHttpResponseFeature res, IFeatureCollection features)
31+
private static Task NotFound(IHttpResponseFeature res, IFeatureCollection features)
3332
{
34-
res.StatusCode = 404;
35-
res.Headers.ContentType = "text/plain";
36-
37-
var body = features.GetResponseBodyFeature();
38-
39-
await body.StartAsync();
40-
await body.CompleteAsync();
33+
res.StatusCode = StatusCodes.Status404NotFound;
34+
return Task.CompletedTask;
4135
}
4236

4337
public void DisposeContext(IFeatureCollection features, Exception? exception) { }
4438

4539
private static ReadOnlySpan<byte> IndexPayload => "Running directly on Kestrel! Navigate to /plaintext and /json to see other endpoints."u8;
4640

47-
private static async Task Index(IHttpRequestFeature req, IHttpResponseFeature res, IFeatureCollection features)
41+
private static async Task Index(IHttpResponseFeature res, IFeatureCollection features)
4842
{
49-
res.StatusCode = 200;
43+
res.StatusCode = StatusCodes.Status200OK;
5044
res.Headers.ContentType = "text/plain";
5145
res.Headers.ContentLength = IndexPayload.Length;
5246

@@ -59,9 +53,9 @@ private static async Task Index(IHttpRequestFeature req, IHttpResponseFeature re
5953

6054
private static ReadOnlySpan<byte> HelloWorldPayload => "Hello, World!"u8;
6155

62-
private static async Task Plaintext(IHttpRequestFeature req, IHttpResponseFeature res, IFeatureCollection features)
56+
private static async Task Plaintext(IHttpResponseFeature res, IFeatureCollection features)
6357
{
64-
res.StatusCode = 200;
58+
res.StatusCode = StatusCodes.Status200OK;
6559
res.Headers.ContentType = "text/plain";
6660
res.Headers.ContentLength = HelloWorldPayload.Length;
6761

@@ -72,13 +66,25 @@ private static async Task Plaintext(IHttpRequestFeature req, IHttpResponseFeatur
7266
}
7367

7468
private static readonly JsonSerializerOptions _jsonSerializerOptions = new(JsonSerializerDefaults.Web);
69+
70+
private static async Task JsonChunked(IHttpResponseFeature res, IFeatureCollection features)
71+
{
72+
res.StatusCode = StatusCodes.Status200OK;
73+
res.Headers.ContentType = "application/json; charset=utf-8";
74+
75+
var body = features.GetResponseBodyFeature();
76+
await body.StartAsync();
77+
await JsonSerializer.SerializeAsync(body.Writer, new JsonMessage { message = "Hello, World!" }, _jsonSerializerOptions);
78+
await body.Writer.FlushAsync();
79+
}
80+
7581
private static readonly ObjectPoolProvider _objectPoolProvider = new DefaultObjectPoolProvider();
7682
private static readonly ObjectPool<ArrayBufferWriter<byte>> _bufferWriterPool = _objectPoolProvider.Create<ArrayBufferWriter<byte>>();
7783
private static readonly ObjectPool<Utf8JsonWriter> _jsonWriterPool = _objectPoolProvider.Create(new Utf8JsonWriterPooledObjectPolicy());
7884

79-
private static async Task Json(IHttpRequestFeature req, IHttpResponseFeature res, IFeatureCollection features)
85+
private static async Task Json(IHttpResponseFeature res, IFeatureCollection features)
8086
{
81-
res.StatusCode = 200;
87+
res.StatusCode = StatusCodes.Status200OK;
8288
res.Headers.ContentType = "application/json; charset=utf-8";
8389

8490
var bufferWriter = _bufferWriterPool.Get();

src/BenchmarksApps/TechEmpower/Kestrel/kestrel.benchmarks.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ scenarios:
3636
presetHeaders: json
3737
path: /json
3838

39+
json-chunked:
40+
application:
41+
job: kestrel
42+
load:
43+
job: wrk
44+
variables:
45+
presetHeaders: json
46+
path: /json-chunked
47+
3948
profiles:
4049
# this profile uses the local folder as the source
4150
# instead of the public repository

0 commit comments

Comments
 (0)