如何分析在生产服务器上运行的 ASP.NET 应用程序?
我有一个 ASP.NET 应用程序,在生产服务器上始终使用 75% - 100% 的 CPU。如何分析应用程序以找出代码的哪一部分占用了最多的 CPU?我研究了几种不同的工具(Xte Profiler、EQATEC、dotTrace),但它们似乎都希望您在它们的工具中加载和运行应用程序。在我看来,他们希望您在他们的工具中加载应用程序并在本地运行测试(而不是在生产中)。我想在应用程序在生产环境中运行时对其进行分析,让人们点击它来查看实际发生的情况。这可能吗?
我是应用程序分析的新手,所以如果我错过了一些明显的事情或者没有正确思考这一点,请原谅我。
谢谢, 科里
I have an ASP.NET application that is consistently using 75% - 100% of the CPU on a production server. How can I profile the application to figure out what part of the code is using up the most CPU? I have looked at a couple of different tools (Xte Profiler, EQATEC, dotTrace), but they all seem to want you to load and run the application within their tool. It seems to me that they want you to load up the application in their tool and run tests locally (not in production). I want to profile the application while it is running in production with people hitting it to see what is actually going on. Is this possible?
I am a newbie to application profiling so forgive me if I have missed something obvious or am not thinking about this correctly.
Thanks,
Corey
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Sam Saffron(StackoverFlow 创建者之一)不久前编写了一个很棒的命令行工具,但不幸的是已经放弃了它。
我的一个朋友在 2015 年分叉了代码以使其正常工作:
https://github.com/jitbit/cpu -analyzer
(该页面有一个链接到 Sam 的帖子,解释如何使用它)
这个工具的伟大之处(除了“无需安装”的可移植性、命令行界面等)是 APM 软件包,例如NewRelic 等仅监视 http 请求。如果您的应用程序有一些后台线程 - 它们不会有太大帮助。
Sam Saffron (one of the StackoverFlow creators) has written a great command-line tool a while ago, but unfortunately has abandoned it.
A friend of mine forked the code to make it work in 2015:
https://github.com/jitbit/cpu-analyzer
(the page has a link to Sam's post explaining how to use it)
The great thing about this tool (besides "no-install required" portability, cmd-line interface, etc etc) is that APM packages like NewRelic etc only monitor http-requests. If your app has some background threads - they won't help much.
当生产服务器的 CPU 利用率较高时,您应该考虑在生产服务器上进行内存转储。查看 ADPlus 并进行挂起转储在asp.net进程上。然后可以使用 Windbg 或其他工具。
我刚刚经历了类似的经历,我们的生产服务器遇到了过多的 CPU 负载 - 我们无法在本地或测试/登台环境中重新创建这种情况。与数据库无关(数据库CPU正常)。分析转储文件可以帮助我们了解导致问题的原因(我们正在使用的某些库过度编译正则表达式对象)。
如果没有 Tess 的博客,这个答案将是不完整的,所以这里是关联。
You should consider taking a memory dump on the production server while it's experiencing high CPU. Check out ADPlus and taking a hang dump on the asp.net process. This can then be analyzed with Windbg or other tools.
I just went through a similar experience where our production servers were experiencing excessive CPU load - a scenario we could not recreate locally or in test/staging environments. It had nothing to do with the database (database CPU was normal). Analyzing the dump file is what clued us in on what was causing the problem (excessive compilation of regex objects by some library we were using).
This answer would be incomplete without Tess' blog, so here's the link.
我猜这与长时间运行的数据库查询有关,而不是 ASP.net 应用程序本身。根据我的经验,十分之九这就是我所看到的情况,这会使应用程序服务器陷入爬行状态,因为资源被消耗,并且应用程序必须等待每个查询完成才能继续。查看数据库服务器上的 SQL 分析器,看看是否有任何查询需要很长时间才能执行。
它可以像向列添加索引或其他一些小的优化一样简单。一旦您知道了查询,您也可以返回代码并调整该部分。
My guess it has to do with long running database queries rather than the ASP.net application itself. In my experience 9 times out of 10 this is what I see and this takes the APPLICATION server down to a crawl as resources are consumed and the app has to wait for each query to finish to move on. Take a look at SQL profilier on the DB server and see if there are any queries that are taking a long time to execute.
It could be as simple as adding an index to a column or some other small minor optimizations. Once you know the query, you can then also go back to your code and tweak that section as well.
对于那些仍然偶然发现这个问题的人来说,这实际上取决于您想要完成的任务。
如果服务器在 CPU 上运行得那么高,那么标准分析器很可能会因为额外的开销而使其陷入停顿。
实际上存在三种不同类型的分析器。标准分析器、轻量级事务分析器和 APM 工具。您可以在我的博客文章中阅读更多相关内容,该文章讨论了所有 3 个:
.NET 分析器: 3 种类型以及为什么您需要全部它们
For those who stumble upon this question still, it really depends on what you are trying to accomplish.
If a server is running that high on CPU, odds are, a standard profiler will bring it to a grinding halt due to it's additional overhead.
There are actually three different types of profilers. Standard profilers, lightweight transaction profilers, and APM tools. You can read more about this in my blog post that discusses all 3:
.NET Profilers: 3 types and why you need all of them
当然可以使用 EQATEC Profiler 来分析 ASP.NET。请参阅:
使用 EQATEC Profiler 分析 ASP.NET 网站
EQATEC Profiler 会在其中检测您的应用这是一个单独的步骤,使应用程序本身能够收集自己的分析信息,然后分析器仅显示该计时数据。
这意味着您可以完全独立于探查器本身来运行已检测的 ASP.NET 应用程序。
例如,您可以检测您的应用程序,将其邮寄到您在印度的测试站点,让他们在其服务器上运行几天,它将自行生成计时报告,然后让他们将这些报告邮寄给您,您可以然后在分析器中查看。相当整洁。
注意:要让分析的应用程序“自行”生成计时快照,它必须知道何时生成它们。默认情况下,这是在 ASP.NET 应用程序中调用 Application_End 方法时的情况。您可以使用 EQATEC Profiler API 在适合您的情况下以编程方式转储快照。请参阅用户指南或查看此帖子。
It's certainly possible to profile ASP.NET with the EQATEC Profiler. See:
Profiling ASP.NET websites with EQATEC Profiler
EQATEC Profiler instruments your app in a separate step that enable the app itself to collect it's own profiling info, and the profiler then merely displays that timing data afterwards.
That means that you can run your instrumented ASP.NET app completely independent of the profiler itself.
You could e.g. instrument your app, mail it to your test site in India, have them run it on their server for some days where it will generate timing reports all on it's own, and have them mail back those reports to you, which you can then view in the profiler. Pretty neat.
Note: To have the profiled app generate timing snapshots "on it's own" it must know when to generate them. By default this is when the method Application_End is called in an ASP.NET app. You can programmatically dump snapshots when it suits you by using the EQATEC Profiler API. See the user guide or check out this thread.
您可以在 Microsoft Developer Network 上阅读相关内容。
您可以根据您的Visual Studio版本选择文档。您应该验证是否为您的 Visual Studio 类型提供了分析功能。
如何:使用以下方法分析网站或 Web 应用程序性能向导
You can read about this on Microsoft Developer Network.
You can select documentation according to the version of your Visual Studio. You should verify profiling functionality is provided for your Visual Studio type.
How to: Profile a Web Site or Web Application Using the Performance Wizard
最好的选择是在您自己的机器上分析您的代码,以确定它在哪里花费了时间。
获取十天免费试用版:
http://www.jetbrains.com/profiler/
以下是一些可以帮助您继续操作的链接:
链接
http://msdn.microsoft.com/en-us/library/ms178643(v=VS.100 ).aspx
http://www.codeproject.com/KB/aspnet /10ASPNetPerformance.aspx
Your best bet is to profile your code on your own machine to identify where it is spending time.
Grab a ten day free trial of this:
http://www.jetbrains.com/profiler/
Here are some links to get you going:
Link
http://msdn.microsoft.com/en-us/library/ms178643(v=VS.100).aspx
http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx