【发布时间】:2009-03-18 01:39:45
【问题描述】:
我刚刚继承了一个由其他人编写的网站 (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 并存储它可能会更好,这可能会比秒表更轻
- 我知道最好让所有页面都从我自己的页面继承,并留在那里,但我不想浏览几十个页面并全部更改。
非常感谢任何想法! 谢谢!
【问题讨论】:
标签: asp.net global-asax timing