CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Display Request Processing Time in ASP.Net pages

By Satheesh babu
Posted On Aug 26,2008
Article Rating:
Be first to rate
this article.
No of Comments: 9
Category: ASP.Net
Print this article.

Display Request Processing Time in ASP.Net pages

 

Introduction

There are some sites in internet who are displaying the time taken to process the request given to the server at the end of every page. This article will help us to implement this feature in our asp.net sites. This also helps a developer to understand how much time it takes to complete the processing of a particular request which he can use for performance check. This article will help us to implement this feature in 2 ways,

Ø       Using a custom HttpModule.

Ø       Using Global.asax file.

 

Even though, we can implement this simple feature in Global.asax file we will also learn this feature through HttpModules. By implementing this simple feature through HttpModules we will understand the basics of HttpModules and building our own custom HttpModules.

 

What is an HttpModule?

The HttpModules are the components in worker process that helps in completing the request given to the server. HttpModules are similar to ISAPI filters which help the runtime to filter the request given to the server prior to asp.net release. Developers can implement their own filters specific to their application needs. These ISAPI filters implementation can be done through unmanaged code and it is extremely difficult to implement. ISAPI equivalent in ASP.Net is the HttpModules, which are easy to implement. To implement this easily, .Netframework has an interface called IHttpModule which should be utilized with simple config settings.

 

In ASP.Net, whenever a request is given to the server (http://localhost/WebApp/Default.aspx), there is a mechanism called HttpPipeline which happens in the worker process to complete a request given to the server. HttpPipeline is a mechanism which will receive the request with appropriate information’s through HttpContext object and passes the request to series of object for request completion. During this stage, the involvement of HttpModules comes into picture for servicing the request. There are a number of system-level (asp.net) HTTP modules, providing services like authentication, state management, output caching, etc. We can always write our own HttpModules and configure it for our web application in web.config.

 

In simple words, writing our own HttpModule will help us to hook a event in every request processing of our application to implement out own application specific needs.

 

To implement a custom HttpModule, we need to implement IHttpModule interface in System.Web namespace.

public interface IHttpModule

    {

        // Summary:

        //     Disposes of the resources (other than memory) used by the module that implements

        //     System.Web.IHttpModule.

        void Dispose();

        //

        // Summary:

        //     Initializes a module and prepares it to handle requests.

        //

        // Parameters:

        //   context:

        //     An System.Web.HttpApplication that provides access to the methods, properties,

        //     and events common to all application objects within an ASP.NET application

        void Init(HttpApplication context);

    }

The module that implement the above interface should implement Init() and Dispose() events. A web.Config entry inside <System.Web> is needed to hook this module in the request processing.

    <httpModules>

      <add type="namespace.ClassName, namespace" name="ClassName" />

    </httpModules>

 

Using a Custom HttpModule

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

 

namespace CodeDigestModule

{

    public class PageProcessingTime : IHttpModule

    {

        DateTime dtStart = DateTime.MinValue;

 

        public PageProcessingTime()

        {

            //

            // TODO: Add constructor logic here

            //

        }

 

        private HttpApplication mApplication;

 

        public void Init(System.Web.HttpApplication application)

        {

            // Wire up beginrequest

            application.BeginRequest += new System.EventHandler(BeginRequest);

            // Wire up endrequest

            application.EndRequest += new System.EventHandler(EndRequest);

            // Save the application

            mApplication = application;

        }

 

        public void BeginRequest(object sender, EventArgs e)

        {

            dtStart = DateTime.Now;          

        }

 

        public void EndRequest(object sender, EventArgs e)

        {

            DateTime dtEnd = DateTime.Now;

            TimeSpan tsProcessingTime = dtEnd - dtStart;

            mApplication.Response.Write("<b>Request Processing Time: Using HttpMoules </b>" + tsProcessingTime.ToString());

        }

 

        public void Dispose()

        {

        }

    }

}

Web.Config

    <httpModules>

      <add type="CodeDigestModule.PageProcessingTime, CodeDigestModule" name="PageProcessingTime" />

    </httpModules>

When we execute the project, we will get an output with the processing time at the end of the every page.

 




Using Global.asax file

By default, there will be no Global.asax file added to your solution when using Visual Studio 2005. We need add it explicitly through “Add New Item” dialog of Visual Studio 2005.

<%@ Application Language="C#" %>

 

<script runat="server">

 

    void Application_Start(object sender, EventArgs e)

    {

        // Code that runs on application startup

 

    }

   

    void Application_End(object sender, EventArgs e)

    {

        //  Code that runs on application shutdown

 

    }

       

    void Application_Error(object sender, EventArgs e)

    {

        // Code that runs when an unhandled error occurs

 

    }

    void Session_Start(object sender, EventArgs e)

    {

        // Code that runs when a new session is started

 

    }

    void Session_End(object sender, EventArgs e)

    {

        // Code that runs when a session ends.

        // Note: The Session_End event is raised only when the sessionstate mode

        // is set to InProc in the Web.config file. If session mode is set to StateServer

        // or SQLServer, the event is not raised.

 

    }

    protected void Application_BeginRequest(Object sender, EventArgs e)

    {

        Context.Items.Add("Request_Start_Time", DateTime.Now);

    }

    protected void Application_EndRequest(Object sender, EventArgs e)

    {

        TimeSpan tsDuration = DateTime.Now.Subtract((DateTime)Context.Items["Request_Start_Time"]);

        Context.Response.Write("<b>Request Processing Time: From Global.asax file " + tsDuration.ToString());

    }

      

</script>

When we execute the project, we will get an output with the processing time at the end of the every page.

 

Downloads

Source Code

 

Conclusion

Thus, we can get the time taken to complete request in the server through this article. If we develop this as an HttpModule it is possible to share this across applications whereas implementing in Global.asax file is private to the application. Making the config setting in machine.config will make all the application hosted on the server to display the request processing time in all the WebPages.

Happy Coding!!!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
loading images
how to display loading image when getting response from server response to client request
MiniProfiler + Log4Net
A more advanced scenario (exposing username in log file, rolling file output) can be done using https://miniprofilerlog4net.codeplex.com/
try to put some hiden field in ur form like and put if statmet in the peorcssing page..sth like if request.form( getThis )= getTheValue then show ur info here end ifif this dosnt work..u gotta set
try to put some hiden field in ur form like and put if statmet in the peorcssing page..sth like if request.form( getThis )= getTheValue then show ur info here end ifif this dosnt work..u gotta set session that would work for sure
Best Code HElp
Thanks for usefull information.

http://www.customsolutionsindia.com
Plain ASP.NET
The solution presented above works fine for plain ASP.NET. Is not working anymore if the page is ajaxified with some UpdatePanel and/or UpdateProgress since only controls from UpdatePanel will be updated.
Good one
Nice article dude.
Excellent codes ad articles
I found this article as a good source for asp.net developer in terms of calculating respone time for website
keep it up.

Pawan Kumar
http://dev.codeplatter.com
RE: Trace... ?
Hi Miron, Thanks for your comment. If performance tuning is only your target then Trace is the good option. The subject matter here is to display the request processing time at the end of each page.
Trace... ?
It is not easier to just using the 'Built-in' tracing ???