ASP.NET Routing performance compare to HttpHandler
Hi few days ago I was experimenting with some new ASP.NET features and want to share some small perf measures to understand how ASP.NET routing affect throughput compare to simple ASP.NET handler.
If you curious about what it is ASP.NET routing ,here a list of couple of articles:
Test environment and scenarios:
1. Request to simple JS
2. Request to handler which doing Response.Write
3. Request to URL which trigger Route Mapping and execution of Routing handler
1 Test Agent
Code:
Scenario 2 – Simple ASP.NET handler:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5:
6: namespace TestRouting
7: {
8: /// <summary>
9: /// Summary description for SimpleHandler
10: /// </summary>
11: public class SimpleHandler : IHttpHandler
12: {
13: public SimpleHandler()
14: {
15: //
16: // TODO: Add constructor logic here
17: //
18: }
19:
20: #region IHttpHandler Members
21:
22: public bool IsReusable
23: {
24: get { throw new NotImplementedException(); }
25: }
26:
27: public void ProcessRequest(HttpContext context)
28: {
29: context.Response.Write("var i = 'Simple Handler Response.Write';");
30: context.Response.End();
31: }
32:
33: #endregion
34: }
35: }
Scenario 3 – ASP.NET Routing:
Routing initialization in global.asax:
1: <%@ Application Language="C#" %>
2: <%@ Import Namespace="System.Web.Routing" %>
3: <%@ Import Namespace="TestRouting" %>
4:
5: <script runat="server">
6:
7: void Application_Start(object sender, EventArgs e)
8: {
9: RegisterRoutes(RouteTable.Routes);
10: }
11:
12: public static void RegisterRoutes(RouteCollection routes)
13: {
14: RouteTable.Routes.Add(
15: new Route("test/{name}/view.aspx", new CustomRouteHandler()));
16: }
Route implementation
1: using System;
2: using System.Web;
3: using System.Web.Routing;
4: using System.Web.UI;
5: using System.Web.UI.WebControls;
6: using System.Web.Compilation;
7:
8:
9: namespace TestRouting
10: {
11: /// <summary>
12: /// Summary description for CustomRouteHandler
13: /// </summary>
14: public class CustomRouteHandler : IRouteHandler
15: {
16: public CustomRouteHandler()
17: {
18: //this.VirtualPath = virtualPath;
19: }
20:
21: //public string VirtualPath { get; private set; }
22:
23: public IHttpHandler GetHttpHandler(RequestContext
24: requestContext)
25: {
26: var page = new Page();
27: requestContext.HttpContext.Response.Write("var i = 'Response.Write';");
28: requestContext.HttpContext.Response.End();
29: return page;
30: }
31: }
32: }
Some takeaways:
I expected that throughput for asp.net routing will have less throughput, because call execution time is longer due to extra module, but was disappointed that it x/3 number compare to simple handler.
I found strange that simple handler test showed better results compare to static JS file, but numbers are very close to each other so I am not considering them as reason to disregard other results.
If you thinking about url rewriting and don’t need dynamic configuration probably you will archive better results using built in support of url rewriting in IIS 7.0
Scenario 2:
Scenario 3: