Time elasped in page execution
Here I am writing a simple utility to tell you how much time a asp page has taken for execution. Normally, Beginrequest is first event to fire when we request a aspx page and Endrequest is the last event. I have created a HTTPHandler to capture these events and alert time difference between begin request and end request. How to apply this in your application:
Step 1: Copy following code into TimeClass.cs file and compile it from .net command prompt as :
csc /target:library TimeClass.dll
using System;
using System.Web;
namespace TimeClass
{
public class ShowTime : IHttpModule
{
private HttpApplication myApplication;
private DateTime BeginTime, EndTime;
public void Init(System.Web.HttpApplication application)
{
application.BeginRequest += new System.EventHandler(BeginRequest);
application.EndRequest += new System.EventHandler(EndRequest);
myApplication = application;
}
public void BeginRequest(object sender, EventArgs e)
{
BeginTime=DateTime.Now;
}
public void EndRequest(object sender, EventArgs e)
{
EndTime=DateTime.Now;
int time= EndTime.Millisecond-BeginTime.Millisecond;
myApplication.Response.Output.Write("<script>alert('Time Elasped={0}(in Milliseconds)');>/script>",time);
}
public void Dispose()
{
}
}
}
Step 2: Copy TimeClass.dll into bin folder of your web application.
step 3: Add following lines in your web.config file in system.web section
<system.web>
<httpModules>
<add type="TimeClass.ShowTime, TimeClass" name="ShowTime" />
</httpModules>
Step 4: Request any aspx page from your application. you will get alert message displaying how much time elasped in executing your application.
That's it. Hope it is helpful in measuring the performance of your application. Any comment/criticism is welcome.
Regards,
Atal.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|