Skip to content

Commit b8ecf76

Browse files
committed
Tweaks
1 parent 246eef0 commit b8ecf76

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/BenchmarksApps/TechEmpower/Kestrel/BenchmarkApp.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ private static async Task NotFound(IHttpRequestFeature req, IHttpResponseFeature
3333
{
3434
res.StatusCode = 404;
3535
res.Headers.ContentType = "text/plain";
36-
res.Headers.ContentLength = HelloWorldPayload.Length;
3736

3837
var body = features.GetResponseBodyFeature();
3938

4039
await body.StartAsync();
41-
body.Writer.Write(HelloWorldPayload);
4240
await body.CompleteAsync();
4341
}
4442

@@ -56,8 +54,6 @@ private static async Task Index(IHttpRequestFeature req, IHttpResponseFeature re
5654

5755
await body.StartAsync();
5856
body.Writer.Write(IndexPayload);
59-
await body.Writer.FlushAsync();
60-
await body.CompleteAsync();
6157
}
6258

6359
private static ReadOnlySpan<byte> HelloWorldPayload => "Hello, World!"u8;
@@ -69,11 +65,8 @@ private static async Task Plaintext(IHttpRequestFeature req, IHttpResponseFeatur
6965
res.Headers.ContentLength = HelloWorldPayload.Length;
7066

7167
var body = features.GetResponseBodyFeature();
72-
7368
await body.StartAsync();
7469
body.Writer.Write(HelloWorldPayload);
75-
await body.Writer.FlushAsync();
76-
await body.CompleteAsync();
7770
}
7871

7972
private static readonly JsonSerializerOptions _jsonSerializerOptions = new(JsonSerializerDefaults.Web);
@@ -84,31 +77,32 @@ private static async Task Plaintext(IHttpRequestFeature req, IHttpResponseFeatur
8477
private static async Task Json(IHttpRequestFeature req, IHttpResponseFeature res, IFeatureCollection features)
8578
{
8679
res.StatusCode = 200;
87-
res.Headers.ContentType = "application/json";
80+
res.Headers.ContentType = "application/json; charset=utf-8";
8881

89-
//Span<byte> buffer = stackalloc byte[256];
9082
var bufferWriter = _bufferWriterPool.Get();
9183
var jsonWriter = _jsonWriterPool.Get();
9284

9385
bufferWriter.ResetWrittenCount();
9486
jsonWriter.Reset(bufferWriter);
9587

96-
JsonSerializer.Serialize(jsonWriter, new { message = "Hello, World!" }, _jsonSerializerOptions);
88+
JsonSerializer.Serialize(jsonWriter, new JsonMessage { message = "Hello, World!" }, _jsonSerializerOptions);
9789

9890
res.Headers.ContentLength = bufferWriter.WrittenCount;
9991

10092
var body = features.GetResponseBodyFeature();
10193

10294
await body.StartAsync();
103-
bufferWriter.WrittenSpan.CopyTo(body.Writer.GetSpan(bufferWriter.WrittenCount));
104-
body.Writer.Advance(bufferWriter.WrittenCount);
105-
await body.Writer.FlushAsync();
106-
await body.CompleteAsync();
95+
body.Writer.Write(bufferWriter.WrittenSpan);
10796

10897
_jsonWriterPool.Return(jsonWriter);
10998
_bufferWriterPool.Return(bufferWriter);
11099
}
111100

101+
private struct JsonMessage
102+
{
103+
public required string message { get; set; }
104+
}
105+
112106
private class Utf8JsonWriterPooledObjectPolicy : IPooledObjectPolicy<Utf8JsonWriter>
113107
{
114108
private static readonly ArrayBufferWriter<byte> _dummyBufferWriter = new(256);

0 commit comments

Comments
 (0)