From 938a92b68a738a7686b74bc60ff68f7af5b012f6 Mon Sep 17 00:00:00 2001 From: Malcolm Evershed Date: Tue, 4 Jun 2013 17:28:14 -0700 Subject: [PATCH] Increase .NET minWorkerThreads to improve CPU utilization MinWorkerThreads controls how many threads the .NET thread pool creates when a new burst of requests come in. Increase this to properly size the thread pool early on and improve CPU utilization, increasing throughput. I saw gains of 5-10% at least. --- aspnet/src/Application.cs | 23 +++++++++++++++++++++++ aspnet/src/Web.config | 5 +++++ 2 files changed, 28 insertions(+) diff --git a/aspnet/src/Application.cs b/aspnet/src/Application.cs index 5535b523748..7b7f557b87b 100644 --- a/aspnet/src/Application.cs +++ b/aspnet/src/Application.cs @@ -1,3 +1,6 @@ +using System; +using System.Configuration; +using System.Threading; using System.Web; using System.Web.Mvc; using System.Web.Routing; @@ -25,6 +28,7 @@ private void Start() { Routes(); Views(); + Threads(); } private void Routes() @@ -57,6 +61,25 @@ private void Views() ViewEngines.Engines.Add(new RazorViewEngine { ViewLocationFormats = new[] { "~/Views/{0}.cshtml" } }); } + private void Threads() + { + // To improve CPU utilization, increase the number of threads that the .NET thread pool expands by when + // a burst of requests come in. We could do this by editing machine.config/system.web/processModel/minWorkerThreads, + // but that seems too global a change, so we do it in code for just our AppPool. More info: + // + // http://support.microsoft.com/kb/821268 + // http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx + // http://blogs.msdn.com/b/perfworld/archive/2010/01/13/how-can-i-improve-the-performance-of-asp-net-by-adjusting-the-clr-thread-throttling-properties.aspx + + int newMinWorkerThreads = Convert.ToInt32(ConfigurationManager.AppSettings["minWorkerThreadsPerLogicalProcessor"]); + if (newMinWorkerThreads > 0) + { + int minWorkerThreads, minCompletionPortThreads; + ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads); + ThreadPool.SetMinThreads(Environment.ProcessorCount * newMinWorkerThreads, minCompletionPortThreads); + } + } + public void Dispose() { } diff --git a/aspnet/src/Web.config b/aspnet/src/Web.config index ebd4fea1092..94bfc6ff714 100644 --- a/aspnet/src/Web.config +++ b/aspnet/src/Web.config @@ -29,6 +29,11 @@ + +