From 141b637d20718aba5aed03b69dc10256f39c7078 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Fri, 25 Nov 2016 15:54:52 -0800 Subject: [PATCH] Bump `MemoryPoolHttpResponseStreamWriter` buffer size up to 16K `char`s - #3516 - fix tests that relied on otherwise-unused `HttpResponseStreamWriter.DefaultBufferSize` --- ...moryPoolHttpResponseStreamWriterFactory.cs | 12 +++++++++-- .../ContentResultTest.cs | 4 ++-- .../ViewFeatures/ViewExecutorTest.cs | 21 +++++++++++++++---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Internal/MemoryPoolHttpResponseStreamWriterFactory.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/MemoryPoolHttpResponseStreamWriterFactory.cs index 95e0615613..e63c471e3e 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Internal/MemoryPoolHttpResponseStreamWriterFactory.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/MemoryPoolHttpResponseStreamWriterFactory.cs @@ -15,9 +15,17 @@ namespace Microsoft.AspNetCore.Mvc.Internal public class MemoryPoolHttpResponseStreamWriterFactory : IHttpResponseStreamWriterFactory { /// - /// The default size of created char buffers. + /// The default size of buffers s will allocate. /// - public static readonly int DefaultBufferSize = 1024; // 1KB - results in a 4KB byte array for UTF8. + /// + /// 16K causes each to allocate one 16K + /// array and one 32K (for UTF8) array. + /// + /// + /// maintains s + /// for these arrays. + /// + public static readonly int DefaultBufferSize = 16 * 1024; private readonly ArrayPool _bytePool; private readonly ArrayPool _charPool; diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/ContentResultTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/ContentResultTest.cs index 55c33b5fa6..fc3b2fdaea 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/ContentResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/ContentResultTest.cs @@ -11,7 +11,6 @@ using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.TestCommon; using Microsoft.AspNetCore.Mvc.ViewComponents; using Microsoft.AspNetCore.Routing; -using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -23,7 +22,8 @@ namespace Microsoft.AspNetCore.Mvc { public class ContentResultTest { - private const int DefaultCharacterChunkSize = HttpResponseStreamWriter.DefaultBufferSize; + private static readonly int DefaultCharacterChunkSize = + MemoryPoolHttpResponseStreamWriterFactory.DefaultBufferSize; [Fact] public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding() diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/ViewExecutorTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/ViewExecutorTest.cs index e607623625..88ce632162 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/ViewExecutorTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewFeatures/ViewExecutorTest.cs @@ -9,6 +9,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.TestCommon; @@ -297,10 +298,21 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures Assert.Equal(expectedLength, memoryStream.Length); } + public static TheoryData WriteLengthData + { + get + { + return new TheoryData + { + MemoryPoolHttpResponseStreamWriterFactory.DefaultBufferSize - 1, + MemoryPoolHttpResponseStreamWriterFactory.DefaultBufferSize + 1, + 2 * MemoryPoolHttpResponseStreamWriterFactory.DefaultBufferSize + 4, + }; + } + } + [Theory] - [InlineData(HttpResponseStreamWriter.DefaultBufferSize - 1)] - [InlineData(HttpResponseStreamWriter.DefaultBufferSize + 1)] - [InlineData(2 * HttpResponseStreamWriter.DefaultBufferSize + 4)] + [MemberData(nameof(WriteLengthData))] public async Task ExecuteAsync_AsynchronouslyFlushesToTheResponseStream_PriorToDispose(int writeLength) { // Arrange @@ -310,7 +322,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures await v.Writer.WriteAsync(text); }); - var expectedWriteCallCount = Math.Ceiling((double)writeLength / HttpResponseStreamWriter.DefaultBufferSize); + var expectedWriteCallCount = + Math.Ceiling((double)writeLength / MemoryPoolHttpResponseStreamWriterFactory.DefaultBufferSize); var context = new DefaultHttpContext(); var stream = new Mock();