diff --git a/src/Benchmarks/Middleware/MemoryCachePlaintextMiddleware.cs b/src/Benchmarks/Middleware/MemoryCachePlaintextMiddleware.cs index aec8298a8..62245d40e 100644 --- a/src/Benchmarks/Middleware/MemoryCachePlaintextMiddleware.cs +++ b/src/Benchmarks/Middleware/MemoryCachePlaintextMiddleware.cs @@ -40,12 +40,12 @@ public Task Invoke(HttpContext httpContext) } } - httpContext.Response.StatusCode = 200; - httpContext.Response.ContentType = "text/plain"; - // HACK: Setting the Content-Length header manually avoids the cost of serializing the int to a string. - // This is instead of: httpContext.Response.ContentLength = payload.Length; - httpContext.Response.Headers["Content-Length"] = "13"; - return httpContext.Response.Body.WriteAsync(payload, 0, payload.Length); + var payloadLength = payload.Length; + var response = httpContext.Response; + response.StatusCode = 200; + response.ContentType = "text/plain"; + response.ContentLength = payloadLength; + return response.Body.WriteAsync(payload, 0, payloadLength); } return _next(httpContext); diff --git a/src/Benchmarks/Middleware/MemoryCachePlaintextSetRemoveMiddleware.cs b/src/Benchmarks/Middleware/MemoryCachePlaintextSetRemoveMiddleware.cs index bd2ddf3b9..93d7079c9 100644 --- a/src/Benchmarks/Middleware/MemoryCachePlaintextSetRemoveMiddleware.cs +++ b/src/Benchmarks/Middleware/MemoryCachePlaintextSetRemoveMiddleware.cs @@ -34,12 +34,12 @@ public Task Invoke(HttpContext httpContext) _memoryCache.Set(_key, _helloWorldPayload); _memoryCache.Remove(_key); - httpContext.Response.StatusCode = 200; - httpContext.Response.ContentType = "text/plain"; - // HACK: Setting the Content-Length header manually avoids the cost of serializing the int to a string. - // This is instead of: httpContext.Response.ContentLength = payload.Length; - httpContext.Response.Headers["Content-Length"] = "13"; - return httpContext.Response.Body.WriteAsync(_helloWorldPayload, 0, _helloWorldPayload.Length); + var response = httpContext.Response; + var payloadLength = _helloWorldPayload.Length; + response.StatusCode = 200; + response.ContentType = "text/plain"; + response.ContentLength = payloadLength; + return httpContext.Response.Body.WriteAsync(_helloWorldPayload, 0, payloadLength); } return _next(httpContext); diff --git a/src/Benchmarks/Middleware/PlaintextMiddleware.cs b/src/Benchmarks/Middleware/PlaintextMiddleware.cs index bd3cf991b..6adcb48cd 100644 --- a/src/Benchmarks/Middleware/PlaintextMiddleware.cs +++ b/src/Benchmarks/Middleware/PlaintextMiddleware.cs @@ -34,12 +34,11 @@ public Task Invoke(HttpContext httpContext) public static Task WriteResponse(HttpResponse response) { + var payloadLength = _helloWorldPayload.Length; response.StatusCode = 200; response.ContentType = "text/plain"; - // HACK: Setting the Content-Length header manually avoids the cost of serializing the int to a string. - // This is instead of: httpContext.Response.ContentLength = _helloWorldPayload.Length; - response.Headers["Content-Length"] = "13"; - return response.Body.WriteAsync(_helloWorldPayload, 0, _helloWorldPayload.Length); + response.ContentLength = payloadLength; + return response.Body.WriteAsync(_helloWorldPayload, 0, payloadLength); } }