Question

I've just inherited a website (ASP.Net 2.0) written by someone else that I need to maintain.
The code is not horrible, but it has a number of things that make the website run incredibly slow.

I have an idea to monitor this, and I want to see what more experienced developers think of it.

My objective right now is to find out when pages take too long to load, to focus attention in those spots.

I'm thinking on hooking the PreRequestHandlerExecute and PostRequestHandlerExecute events in Global.asax, and create a StopWatch in "Pre", attach it to HttpContext.Items, and read it in "Post", and if the request took more than, say, 100ms, it'll report to me to let me know.

Some "pseudo code" for that would be:

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e) {
    System.Diagnostics.Stopwatch theTimer = new Stopwatch();
    theTimer.Start();
    HttpContext.Current.Items.Add("RequestTimer", theTimer);
}

protected void Application_PostRequestHandlerExecute(Object sender, EventArgs e) {
     Stopwatch theTimer = (Stopwatch) HttpContext.Current.Items["RequestTimer"];
     System.Diagnostics.Debug.WriteLine(theTimer.ElapsedMilliseconds + "@: " + HttpContext.Current.Request.RawUrl)
}

What do you think of this approach?
Is it reasonable to do this?
Is it going to make my website crawl to its knees?
Is there a better way to do this?

Some thoughts:
- It may be better to grab DateTime.Now.Ticks and store that, which is probably going to be lighter than the StopWatch
- I know it'd be better to have all pages inherit from a page of my own, and time there, but I don't want to go through dozens of pages and change them all.

Any thoughts are greatly appreciated! Thanks!

Was it helpful?

Solution

Instead of messing with a stopwatch I'd just throw the start time in the HttpContext collection and pull it back out. Then you can perform a simple subtraction at the end of the request which should have a negligible affect on performance.

OTHER TIPS

The better approach would be to enable tracing.

I wouldn't do this. This is going to be more trouble than it's worth. There are some good profiling tools for ASP.NET like CLR Profiler and Red Gate's ANTS Profiler.

As BobbyShaftoe remarked, profiling's your best bet. But consider JetBrains' dotTrace as well; it's very, very good.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top