Browse Source

Updated servicestack to make the ThreadPool setting a configurable option.

Kevin Howard 12 years ago
parent
commit
d8c99ddcf7

+ 23 - 0
servicestack/src/AppHostConfigHelper.cs

@@ -3,6 +3,7 @@ using System.Data;
 using System.Linq;
 using System.Configuration;
 using System.Collections.Generic;
+using System.Threading;
 using ServiceStack.ServiceHost;
 using ServiceStackBenchmark.Model;
 
@@ -136,5 +137,27 @@ namespace ServiceStackBenchmark
             }
 
         }
+
+        /// <summary>
+        /// Method to config the Minimum and Maximum number of Worker Threads per Logical Processor Count.
+        /// </summary>
+        /// <remarks>the Completion Port Threads are set to their defaults as there is no IO concerrency in our app</remarks>
+        public static void ConfigThreadPool()
+        {
+            int sysMinWorkerThreads, sysMinCompletionPortThreads;
+            ThreadPool.GetMinThreads(out sysMinWorkerThreads, out sysMinCompletionPortThreads);
+
+            int sysMaxWorkerThreads, sysMaxCompletionPortThreads;
+            ThreadPool.GetMaxThreads(out sysMaxWorkerThreads, out sysMaxCompletionPortThreads);
+
+            int newMinWorkerThreadsPerCPU = Math.Max(1, Convert.ToInt32(ConfigurationManager.AppSettings["minWorkerThreadsPerLogicalProcessor"] ?? "1"));
+            int newMaxWorkerThreadsPerCPU = Math.Max(newMinWorkerThreadsPerCPU, Convert.ToInt32(ConfigurationManager.AppSettings["maxWorkerThreadsPerLogicalProcessor"] ?? "100"));
+
+            var minWorkerThreads = Environment.ProcessorCount * newMinWorkerThreadsPerCPU;
+            ThreadPool.SetMinThreads(minWorkerThreads, sysMinCompletionPortThreads);
+
+            var maxWorkerThreads = Environment.ProcessorCount * newMaxWorkerThreadsPerCPU;
+            ThreadPool.SetMaxThreads(maxWorkerThreads, sysMaxCompletionPortThreads);
+        }
     }
 }

+ 1 - 3
servicestack/src/Global.asax.cs

@@ -8,9 +8,7 @@ namespace ServiceStackBenchmark
     {
         protected void Application_Start(object sender, EventArgs e)
         {
-            var threads = 40 * Environment.ProcessorCount;
-            ThreadPool.SetMaxThreads(threads, threads);
-            ThreadPool.SetMinThreads(threads, threads);
+            AppHostConfigHelper.ConfigThreadPool();
 
             new AppHost().Init();
         }

+ 7 - 0
servicestack/src/SelfHost/App.config

@@ -10,6 +10,13 @@
     <add name="SQLServer" connectionString="server=localhost; user id=benchmarkdbuser; password=B3nchmarkDBPass; database=hello_world; max pool size=32767" providerName="System.Data.SqlClient" />
   </connectionStrings>
   <appSettings>
+    <!-- To fully saturate the CPUs, we need to allow the .NET thread pool to create many threads
+         when a large burst of requests come in. We do this by boosting the minWorkerThreads value
+         from the default of 1 per logical processor to 40 per logical processor. This seems to be
+         a little conservative as http://support.microsoft.com/kb/821268 recommends 50.-->
+    <add key="minWorkerThreadsPerLogicalProcessor" value="40" />
+    <add key="maxWorkerThreadsPerLogicalProcessor" value="100" />
+    <!-- Disable ServiceStack features -->
     <add key="DisabledFeatures" value="MetaData, Soap, Soap11, Soap12" />
   </appSettings>
   <runtime>

+ 10 - 1
servicestack/src/Web.config

@@ -11,8 +11,17 @@
     <add name="SQLServer" connectionString="server=localhost; user id=benchmarkdbuser; password=B3nchmarkDBPass; database=hello_world; max pool size=32767" providerName="System.Data.SqlClient" />
   </connectionStrings>
   <appSettings>
+    <!-- To fully saturate the CPUs, we need to allow the .NET thread pool to create many threads
+         when a large burst of requests come in. We do this by boosting the minWorkerThreads value
+         from the default of 1 per logical processor to 40 per logical processor. This seems to be
+         a little conservative as http://support.microsoft.com/kb/821268 recommends 50.-->
+    <add key="minWorkerThreadsPerLogicalProcessor" value="40" />
+    <add key="maxWorkerThreadsPerLogicalProcessor" value="100" />
+    <!-- Disable ServiceStack features -->
     <add key="DisabledFeatures" value="MetaData, Soap, Soap11, Soap12" />
-    <add key="webPages:Enabled" value="false" />
+    <!-- Disable support for directly accessing *.cshtml/*.vbhtml files because that is a perf killer
+         and because we don't use such functionality. -->
+    <add key="webpages:Enabled" value="false" />
   </appSettings>
   <system.web>
     <customErrors mode="Off" />