Skip to content

Commit 44f0303

Browse files
committed
More JSON variants
1 parent 60c7ac1 commit 44f0303

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/BenchmarksApps/TechEmpower/Kestrel/BenchmarkApp.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Buffers;
2+
using System.Text;
23
using System.Text.Json;
34
using Microsoft.AspNetCore.Hosting.Server;
45
using Microsoft.AspNetCore.Http.Features;
@@ -22,6 +23,8 @@ public Task ProcessRequestAsync(IFeatureCollection features)
2223
{
2324
"/plaintext" => Plaintext(res, features),
2425
"/json" => Json(res, features),
26+
"/json-string" => JsonString(res, features),
27+
"/json-utf8bytes" => JsonUtf8Bytes(res, features),
2528
"/json-chunked" => JsonChunked(res, features),
2629
"/" => Index(res, features),
2730
_ => NotFound(res, features),
@@ -78,6 +81,40 @@ private static async Task JsonChunked(IHttpResponseFeature res, IFeatureCollecti
7881
await body.Writer.FlushAsync();
7982
}
8083

84+
private static async Task JsonString(IHttpResponseFeature res, IFeatureCollection features)
85+
{
86+
res.StatusCode = StatusCodes.Status200OK;
87+
res.Headers.ContentType = "application/json; charset=utf-8";
88+
89+
var message = JsonSerializer.Serialize(new JsonMessage { message = "Hello, World!" }, _jsonSerializerOptions);
90+
res.Headers.ContentLength = Encoding.UTF8.GetByteCount(message);
91+
92+
var body = features.GetResponseBodyFeature();
93+
await body.StartAsync();
94+
95+
Span<byte> buffer = stackalloc byte[256];
96+
var length = Encoding.UTF8.GetBytes(message, buffer);
97+
body.Writer.Write(buffer[..length]);
98+
99+
await body.Writer.FlushAsync();
100+
}
101+
102+
private static async Task JsonUtf8Bytes(IHttpResponseFeature res, IFeatureCollection features)
103+
{
104+
res.StatusCode = StatusCodes.Status200OK;
105+
res.Headers.ContentType = "application/json; charset=utf-8";
106+
107+
var messageBytes = JsonSerializer.SerializeToUtf8Bytes(new JsonMessage { message = "Hello, World!" }, _jsonSerializerOptions);
108+
res.Headers.ContentLength = messageBytes.Length;
109+
110+
var body = features.GetResponseBodyFeature();
111+
await body.StartAsync();
112+
113+
body.Writer.Write(messageBytes);
114+
115+
await body.Writer.FlushAsync();
116+
}
117+
81118
private static readonly ObjectPoolProvider _objectPoolProvider = new DefaultObjectPoolProvider();
82119
private static readonly ObjectPool<ArrayBufferWriter<byte>> _bufferWriterPool = _objectPoolProvider.Create<ArrayBufferWriter<byte>>();
83120
private static readonly ObjectPool<Utf8JsonWriter> _jsonWriterPool = _objectPoolProvider.Create(new Utf8JsonWriterPooledObjectPolicy());

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

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

39+
json-string:
40+
application:
41+
job: kestrel
42+
load:
43+
job: wrk
44+
variables:
45+
presetHeaders: json
46+
path: /json-string
47+
48+
json-utf8bytes:
49+
application:
50+
job: kestrel
51+
load:
52+
job: wrk
53+
variables:
54+
presetHeaders: json
55+
path: /json-utf8bytes
56+
3957
json-chunked:
4058
application:
4159
job: kestrel

0 commit comments

Comments
 (0)