tags:

views:

183

answers:

2

Where do i start the stopwatch and where should it stop?

The logical place for the stopwatch to start should be the controller action method, yes, no?

But where should the stop the clock? I would like to think that it goes at the end of the master page? It doesnt make sense to stop the watch at the end of the controller action method because there will additional process which happens in the View to render the page.

Any thoughts?

EDIT: I intend to use the elapsed time within a master page so i can get the time for any webpage.

Darin: Thanks for the code sample. But the sample does not work as i expected. I planned to use the elaspsed time in a master page so it can be written to every page.

So i think the Master page would've already loaded by the time EndRequest get executed?

In my EndRequest i set:

Context.Items["PageCreationTime"] = watch.ElapsedMilliseconds.ToString();

Then i my masterpage iam unable to get reference:

<%= HttpContext.Current.Items["PageCreationTime"].ToString()%>
+3  A: 

I thnk you'd best place it in the EndRequest event handler in the Global.asax. Something like:

protected void Application_EndRequest(object sender, EventArgs e)
{
  Stopwatch.Stop();
}

The EndRequest event is fired at the very end of earch request. You could start your stopwatch in the beginrequest eventhandler:

protected void Application_BeginRequest(object sender, EventArgs e)
{  
  Stopwatch.Start();
}

Starting it in the controller action may be too late (depending on what you want to measure) since a lot of other code will have run at that point, even custom code you wrote yourself, e.g. when you place custom attributes on your controller action.

Razzie
Thanks, but how do i get to the stop watch Elapsed time from the Global.ascx?
darin has already updated his answer, so look at his code. Come to think of it, aren't you better off using a *real* profiler tool like RedGate's Antz, or JitBits dotTrace? These tools are great and much more usuable / reliable than fiddling with a stopwatch. Just an idea!
Razzie
Thanks for the tip!
+4  A: 

Application.BeginRequest and Application.EndRequest are good candidates.

protected void Application_BeginRequest(object sender, EventArgs e)
{
    Stopwatch watch = Stopwatch.StartNew();
    Context.Items["watch"] = watch;
}

protected void Application_EndRequest(object sender, EventArgs e)
{
    Stopwatch watch = (Stopwatch)Context.Items["watch"];
    watch.Stop();
    // Do something with the results like for example:
    Context.Response.Write(watch.ElapsedMilliseconds);
}
Darin Dimitrov
Good suggestion, but how would i get the elaspsed time value out of the global class? If i declare a static variable for the stopwatch then this will not be thread-safe?
You must store it in the HttpContext. This way it will be per/request and thread safe.
Darin Dimitrov
No this does not work with the asp.net master page. I think the msaterpage has already rendered by the time EndRequest is fired.
By the time you call EndRequest the views have finished rendering. So by doing Context.Response.Write(watch.ElapsedMilliseconds) you could append after the </html> tag. If you need to insert it into a particular location inside your html you could write a custom Filer (http://aspnet.4guysfromrolla.com/articles/120308-1.aspx)
Darin Dimitrov