1

我刚刚继承了一个我需要维护的由其他人编写的网站(ASP.Net 2.0)。
该代码并不可怕,但它有许多使网站运行缓慢的东西。

我有一个想法来监控这个,我想看看更有经验的开发人员是怎么想的。

我现在的目标是找出页面加载时间过长,将注意力集中在这些地方。

我正在考虑在 Global.asax 中挂钩 PreRequestHandlerExecute 和 PostRequestHandlerExecute 事件,并在“Pre”中创建一个 StopWatch,将其附加到 HttpContext.Items,并在“Post”中读取它,如果请求超过,比如说, 100ms,它会向我报告让我知道。

一些“伪代码”将是:

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)
}

您如何看待这种方法?
这样做合理吗?
它会让我的网站爬行吗?
有一个更好的方法吗?

一些想法:
- 抓住 DateTime.Now.Ticks 并存储它可能会更好,这可能会比 StopWatch 更轻
- 我知道让所有页面都继承自我自己的页面会更好,并且时间在那里,但我不想浏览几十页并全部更改。

任何想法都非常感谢!谢谢!

4

4 回答 4

3

更好的方法是启用跟踪

于 2009-03-18T01:42:34.373 回答
2

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.

于 2009-03-18T01:45:41.620 回答
1

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.

于 2009-03-18T01:43:43.857 回答
1

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

于 2009-03-18T02:03:52.123 回答