ASP.Net 页面的全局计时

发布于 2024-07-15 22:05:09 字数 1144 浏览 6 评论 0原文

我刚刚继承了一个由其他人编写的网站 (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 更轻
- 我知道最好让所有页面都继承自我自己的页面,并且在那里,但我不想浏览数十个页面并将它们全部更改。

任何想法都将不胜感激! 谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野稚 2024-07-22 22:05:09

更好的方法是启用跟踪

The better approach would be to enable tracing.

夜光 2024-07-22 22:05:09

我不会这样做。 这会带来更多的麻烦,而不是其价值。 有一些适用于 ASP.NET 的优秀分析工具,例如 CLR Profiler 和 Red Gate 的 ANTS Profiler。

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.

长途伴 2024-07-22 22:05:09

我不会乱搞秒表,而是将开始时间放入 HttpContext 集合中,然后将其拉出来。 然后,您可以在请求结束时执行简单的减法,这对性能的影响应该可以忽略不计。

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.

独自←快乐 2024-07-22 22:05:09

正如 BobbyShaftoe 所说,分析是最好的选择。 但也请考虑 JetBrains 的 dotTrace; 这非常非常好。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文