微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 准确测量Asp.Net MVC应用程序的请求/响应时间的方法

请考虑以下用于测量Asp.Net应用程序中的请求/响应时间的代码
protected void Application_EndRequest()
{
    Trace.WriteLine(string.Format("{0}:{1}",(DateTime.Now - HttpContext.Current.Timestamp).TotalMilliseconds,HttpContext.Current.Request.RawUrl));
}

根据MSDN,DateTime.Now的近似分辨率为10毫秒.

此外,从MSDN HttpContext.Timestamp description开始,

The timestamp returned from the Timestamp property is the local time of the server and is set during the instantiation of the HttpContext object. The local time is equal to the UTC time plus the UTC offset.

理论上,上面的代码应该以毫秒为单位给出总的请求/响应时间.

我的问题是,这将是多么准确?还有更准确/更好的方法吗?

解决方法

StopWatch类的精度可能低至几微秒,具体取决于硬件和操作系统.如果您处于高分辨率模式,则可以读取IsHighResolution属性.如果没有,那么你的系统计时器的准确性就达到了.

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

使用这个类的好处是你肯定不会比上面的代码更糟糕.如果在高分辨率,你会得到更多的准确性.

但是,如果您正在尝试衡量性能并更好地了解您的服务,那么就有更好的工具.

例如,perfview是一个很好的工具:

http://blogs.msdn.com/b/dotnet/archive/2012/10/09/improving-your-app-s-performance-with-perfview.aspx

原文地址:https://www.jb51.cc/csharp/91388.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐