views:

126

answers:

4

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!

+3  A: 

The better approach would be to enable tracing.

Andrew Hare
Right, but I don't want to be looking at tracing all the time. I want to fix the bottlenecks that I find, and then let the website run, and let it send me an e-mail when something in particular is slow. I can't do that with tracing, I think.
Daniel Magliola
That is a good point, no you couldn't do that with tracing.
Andrew Hare
+1  A: 

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.

Spencer Ruport
+2  A: 

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.

BobbyShaftoe
The problem with profiling is that it makes runtime performance really REALLY bad. I want to have this code running and let it notify me when something is slower than it should. I can't have a live website with a profiler attached to it.
Daniel Magliola
Yes, I know how bad it is to do this with "production" code, but in all honesty, it was in production already when I got it, so I can't do much about that.
Daniel Magliola
What kind of trouble in particular are you thinking about? Do you think this could generate exceptions, or a slowdown? Something else?
Daniel Magliola
Trouble as in a lot of work without a lot of benefit. You could do this in test by generating a load and profiling that, not on production. You could use something like WebLoad to generate the load if you want.
BobbyShaftoe
+1  A: 

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

Adam