Skip to content

Commit

Permalink
Update BenchmarkApp.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Jan 17, 2025
1 parent 32a1460 commit 50f48a4
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/BenchmarksApps/TechEmpower/Kestrel/BenchmarkApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,48 +81,46 @@ private static async Task Index(IHttpResponseFeature res, IFeatureCollection fea

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

private static async Task Plaintext(IHttpResponseFeature res, IFeatureCollection features)
private static Task Plaintext(IHttpResponseFeature res, IFeatureCollection features)
{
res.StatusCode = StatusCodes.Status200OK;
res.Headers.ContentType = TextPlainContentType;
res.Headers.ContentLength = HelloWorldPayload.Length;

var body = features.GetResponseBodyFeature();
await body.StartAsync();

body.Writer.Write(HelloWorldPayload);
await body.Writer.FlushAsync();

return Task.CompletedTask;
}

private static async Task JsonChunked(IHttpResponseFeature res, IFeatureCollection features)
private static Task JsonChunked(IHttpResponseFeature res, IFeatureCollection features)
{
res.StatusCode = StatusCodes.Status200OK;
res.Headers.ContentType = JsonContentTypeWithCharset;

var body = features.GetResponseBodyFeature();
await body.StartAsync();
await JsonSerializer.SerializeAsync(body.Writer, new JsonMessage { message = "Hello, World!" }, SerializerContext.JsonMessage);
await body.Writer.FlushAsync();
return JsonSerializer.SerializeAsync(body.Writer, new JsonMessage { message = "Hello, World!" }, SerializerContext.JsonMessage);
}

private static async Task JsonString(IHttpResponseFeature res, IFeatureCollection features)
private static Task JsonString(IHttpResponseFeature res, IFeatureCollection features)
{
res.StatusCode = StatusCodes.Status200OK;
res.Headers.ContentType = JsonContentTypeWithCharset;

var message = JsonSerializer.Serialize(new JsonMessage { message = "Hello, World!" }, SerializerContext.JsonMessage);
res.Headers.ContentLength = Encoding.UTF8.GetByteCount(message);
Span<byte> buffer = stackalloc byte[64];
var length = Encoding.UTF8.GetBytes(message, buffer);
res.Headers.ContentLength = length;

var body = features.GetResponseBodyFeature();
await body.StartAsync();

Span<byte> buffer = stackalloc byte[256];
var length = Encoding.UTF8.GetBytes(message, buffer);
body.Writer.Write(buffer[..length]);

await body.Writer.FlushAsync();
return Task.CompletedTask;
}

private static async Task JsonUtf8Bytes(IHttpResponseFeature res, IFeatureCollection features)
private static Task JsonUtf8Bytes(IHttpResponseFeature res, IFeatureCollection features)
{
res.StatusCode = StatusCodes.Status200OK;
res.Headers.ContentType = JsonContentTypeWithCharset;
Expand All @@ -131,11 +129,10 @@ private static async Task JsonUtf8Bytes(IHttpResponseFeature res, IFeatureCollec
res.Headers.ContentLength = messageBytes.Length;

var body = features.GetResponseBodyFeature();
await body.StartAsync();

body.Writer.Write(messageBytes);

await body.Writer.FlushAsync();
return Task.CompletedTask;
}

private static Task Json(IHttpResponseFeature res, IFeatureCollection features)
Expand All @@ -150,8 +147,6 @@ private static Task Json(IHttpResponseFeature res, IFeatureCollection features)

body.Writer.Write(messageSpan);

//await body.StartAsync();
//await body.Writer.FlushAsync();
return Task.CompletedTask;
}

Expand Down

0 comments on commit 50f48a4

Please sign in to comment.