Skip to content

Commit 938a92b

Browse files
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.
1 parent 24e188b commit 938a92b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

aspnet/src/Application.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System;
2+
using System.Configuration;
3+
using System.Threading;
14
using System.Web;
25
using System.Web.Mvc;
36
using System.Web.Routing;
@@ -25,6 +28,7 @@ private void Start()
2528
{
2629
Routes();
2730
Views();
31+
Threads();
2832
}
2933

3034
private void Routes()
@@ -57,6 +61,25 @@ private void Views()
5761
ViewEngines.Engines.Add(new RazorViewEngine { ViewLocationFormats = new[] { "~/Views/{0}.cshtml" } });
5862
}
5963

64+
private void Threads()
65+
{
66+
// To improve CPU utilization, increase the number of threads that the .NET thread pool expands by when
67+
// a burst of requests come in. We could do this by editing machine.config/system.web/processModel/minWorkerThreads,
68+
// but that seems too global a change, so we do it in code for just our AppPool. More info:
69+
//
70+
// http://support.microsoft.com/kb/821268
71+
// http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx
72+
// 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
73+
74+
int newMinWorkerThreads = Convert.ToInt32(ConfigurationManager.AppSettings["minWorkerThreadsPerLogicalProcessor"]);
75+
if (newMinWorkerThreads > 0)
76+
{
77+
int minWorkerThreads, minCompletionPortThreads;
78+
ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads);
79+
ThreadPool.SetMinThreads(Environment.ProcessorCount * newMinWorkerThreads, minCompletionPortThreads);
80+
}
81+
}
82+
6083
public void Dispose()
6184
{
6285
}

aspnet/src/Web.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<!-- Disable support for directly accessing *.cshtml/*.vbhtml files because that is a perf killer
3030
and because we don't use such functionality. -->
3131
<add key="webpages:Enabled" value="false" />
32+
<!-- To fully saturate the CPUs, we need to allow the .NET thread pool to create many threads
33+
when a large burst of requests come in. We do this by boosting the minWorkerThreads value
34+
from the default of 1 per logical processor to 8 per logical processor. This seems to be
35+
pretty conservative as http://support.microsoft.com/kb/821268 recommends 50.-->
36+
<add key="minWorkerThreadsPerLogicalProcessor" value="8" />
3237
</appSettings>
3338
<system.web>
3439
<!-- Show errors -->

0 commit comments

Comments
 (0)