将分析支持构建到代码中
我希望(我不知道是否可能)在我的代码中构建分析支持,而不是使用一些外部分析器。我听说大多数探查器编写者都使用一些探查器 API。该 API 可以用于从正在执行的代码中进行分析吗?还有其他考虑吗?
I wish (I dont know if its possible) to build profiling support into my code instead of using some external profiler. I have heard that there is some profiler api that is used by most of the profiler writers. Can that api be used to profile from within the code that is being executed? Are there any other considerations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您不想使用常规分析器,则可以使用应用程序输出性能计数器。
您可能会发现此博客条目对于入门很有用:链接
If you don't want to use a regular profiler, you could have your application output performance counters.
You may find this blog entry useful to get started: Link
EQATEC 分析器构建应用程序的检测版本,该版本将完全自行运行和收集分析统计信息 - 您不需要附加分析器。默认情况下,您的应用程序只会将统计信息转储到纯文本 xml 文件中。
这意味着您可以构建应用程序的配置文件版本,将其部署在客户的站点上,让他们运行它并将统计报告发送回给您。他们不需要安装任何特殊的东西或运行分析器或任何东西。
此外,如果您可以通过网络连接到达已部署的应用程序的计算机,并且它允许传入连接,那么您甚至可以坐在家里使用分析器自己拍摄正在运行的分析应用程序的快照。您所需要的只是一个套接字连接 - 您自己决定端口号,并且控制协议本身是普通的 http,因此它很可能甚至可以通过内容过滤网关。
The EQATEC Profiler builds an instrumented version of your app that will run and collect profiling statistics entirely by itself - you don't need to attach the profiler. By default your app will simply dump the statistics into plaintext xml-files.
This means that you can build a profiled version of your app, deploy it at your customer's site, and have them run it and send back the statistics-reports to you. No need for them to install anything special or run a profiler or anything.
Also, if you can reach your deployed app's machine via a network-connection and it allows incoming connections then you can even take snapshots of the running profiled app yourself, sitting at home with the profiler. All you need is a socket-connection - you decide the port-number yourself and the control-protocol itself is plain http, so it's pretty likely to make it past even content-filtering gateways.
.NET 框架探查器 API 是一个 COM 对象,它在 .NET 处理调用之前拦截调用。我的理解是它不能托管在托管(C#)代码中。
根据您想要执行的操作,您可以插入秒表计时器来测量调用的长度,或者将性能计数器添加到您的应用程序,以便您可以从性能监视器监视应用程序的性能。
The .NET framework profiler API is a COM object that intercepts calls before .NET handles them. My understanding is that it cannot be hosted in managed (C#) code.
Depending on what you want to do, you can insert Stopwatch timers to measure length of calls, or add Performance Counters to your application so that you can monitor the performance of the application from the Performance Monitor.
有一篇 GameDev 文章讨论了如何在 C++ 程序中构建分析基础结构。您可以采用此方法来使用 C#,前提是在退出时释放堆栈上创建的对象,而不是留给垃圾收集器
http://www.gamedev.net/reference/programming/features/enginuity3/
即使您无法掌握整个技术,也可能有一些有用的想法。
There's a GameDev article that discusses how to build profiling infrastructure in a C++ program. You may be able to adapt this approach to work with C# provided objects created on the stack are freed on exit instead of left for the garbage collector
http://www.gamedev.net/reference/programming/features/enginuity3/
Even if you can't take the whole technique, there may be some useful ideas.
当我无法使用我的最喜欢的技术是这个。它很笨拙并且提供低分辨率信息,但它确实有效。首先,有一个全局字符串堆栈。这是用 C 编写的,但您可以将其改编为 C#:
然后,在进入和退出您拥有源代码的每个例程时,推送/弹出例程的名称:
所以现在 stack[0..nStack] 保持运行调用堆栈(减去调用函数的行号),因此它不如真正的调用堆栈,但总比没有好。
现在您需要一种方法来随机或伪随机地拍摄快照。使用另一个全局变量和一个例程来查看它:
现在,在整个代码中散布对
CheckForSnap
的调用,尤其是在低级例程中。运行完成后,您将获得一个堆栈样本文件。您可以查看这些是否有意外行为。例如,在很大一部分样本上显示的任何函数的包含时间大致等于该部分。就像我说的,这总比没有好。它确实有缺点:
time(NULL)
的所有调用,因此当您删除所有大问题时,将更难找到小问题。了解以下几点很重要:
What I've done when I can't use my favorite technique is this. It's clumsy and gives low-resolution information, but it works. First, have a global stack of strings. This is in C, but you can adapt it to C#:
Then, on entry and exit to each routine you have source code for, push/pop the name of the routine:
So now stack[0..nStack] keeps a running call stack (minus the line numbers of where functions are called from), so it's not as good as a real call stack, but better than nothing.
Now you need a way to take snapshots of it at random or pseudo-random times. Have another global variable and a routine to look at it:
Now, sprinkle calls to
CheckForSnap
throughout your code, especially in the low-level routines. When the run is finished, you have a file of stack samples. You can look at those for unexpected behavior. For example, any function showing up on a significant fraction of samples has inclusive time roughly equal to that fraction.Like I said, this is better than nothing. It does have shortcomings:
time(NULL)
, so when you have removed all your big problems, it will be harder to find the small ones.It is important to understand a few things: