Application.cs 3.0 KB

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