diff --git a/src/Benchmarks/Middleware/PlaintextMiddleware.cs b/src/Benchmarks/Middleware/PlaintextMiddleware.cs index 2cd1d23b7..8d50f7daf 100644 --- a/src/Benchmarks/Middleware/PlaintextMiddleware.cs +++ b/src/Benchmarks/Middleware/PlaintextMiddleware.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.IO.Pipelines; using System.Text; using System.Threading.Tasks; using Benchmarks.Configuration; @@ -32,6 +33,33 @@ public Task Invoke(HttpContext httpContext) return _next(httpContext); } +#if NETCOREAPP3_0 + public static Task WriteResponse(HttpResponse response) + { + var payloadLength = _helloWorldPayload.Length; + response.StatusCode = 200; + response.ContentType = "text/plain"; + response.ContentLength = payloadLength; + + var vt = response.BodyPipe.WriteAsync(_helloWorldPayload); + + if (vt.IsCompletedSuccessfully) + { + // Signal consumption to the IValueTaskSource + vt.GetAwaiter().GetResult(); + return Task.CompletedTask; + } + else + { + return AwaitResult(vt); + } + + async Task AwaitResult(ValueTask flushResult) + { + await flushResult; + } + } +#else public static Task WriteResponse(HttpResponse response) { var payloadLength = _helloWorldPayload.Length; @@ -40,6 +68,7 @@ public static Task WriteResponse(HttpResponse response) response.ContentLength = payloadLength; return response.Body.WriteAsync(_helloWorldPayload, 0, payloadLength); } +#endif } public static class PlaintextMiddlewareExtensions