Application.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Configuration;
  3. using System.Threading;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7. namespace Benchmarks.AspNet
  8. {
  9. public class Application : IHttpModule
  10. {
  11. private static volatile bool started = false;
  12. private static object locker = new object();
  13. public void Init(HttpApplication context)
  14. {
  15. if (!started)
  16. {
  17. lock (locker)
  18. {
  19. Start();
  20. started = true;
  21. }
  22. }
  23. }
  24. private void Start()
  25. {
  26. Routes();
  27. Views();
  28. Threads();
  29. }
  30. private void Routes()
  31. {
  32. RouteTable.Routes.Clear();
  33. RouteTable.Routes.MapRoute(
  34. name: "JSON",
  35. url: "json",
  36. defaults: new { controller = "Json", action = "Index" }
  37. );
  38. RouteTable.Routes.MapRoute(
  39. name: "WithProviders",
  40. url: "{controller}/{providerName}/{action}",
  41. defaults: new { action = "Index" },
  42. constraints: new { controller = "ado|entityframework", providerName = "mysql|postgresql" }
  43. );
  44. RouteTable.Routes.MapRoute(
  45. name: "Default",
  46. url: "{controller}/{action}",
  47. defaults: new { controller = "Home", action = "Index" }
  48. );
  49. }
  50. private void Views()
  51. {
  52. ViewEngines.Engines.Clear();
  53. ViewEngines.Engines.Add(new RazorViewEngine { ViewLocationFormats = new[] { "~/Views/{0}.cshtml" } });
  54. }
  55. private void Threads()
  56. {
  57. // To improve CPU utilization, increase the number of threads that the .NET thread pool expands by when
  58. // a burst of requests come in. We could do this by editing machine.config/system.web/processModel/minWorkerThreads,
  59. // but that seems too global a change, so we do it in code for just our AppPool. More info:
  60. //
  61. // http://support.microsoft.com/kb/821268
  62. // http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx
  63. // 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
  64. int newMinWorkerThreads = Convert.ToInt32(ConfigurationManager.AppSettings["minWorkerThreadsPerLogicalProcessor"]);
  65. if (newMinWorkerThreads > 0)
  66. {
  67. int minWorkerThreads, minCompletionPortThreads;
  68. ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads);
  69. ThreadPool.SetMinThreads(Environment.ProcessorCount * newMinWorkerThreads, minCompletionPortThreads);
  70. }
  71. }
  72. public void Dispose()
  73. {
  74. }
  75. }
  76. }