跟踪 vb.net 中函数调用的持续时间
在我们的 VB6 应用程序中,我们添加了一些实用函数来跟踪函数所花费的时间。我们这样做是为了跟踪性能瓶颈。
基本上,它的工作原理是有两个实用函数:StartTickCount() 和 EndTickCount()。您将在每个函数中传递函数名称,函数将使用字典在调用 StartTickCount() 时获取滴答计数,然后在调用 EndTickCount() 时减去滴答计数。这并不完美,因为它当然没有考虑到调用获取滴答计数需要时间等,但基本上它符合我们的目的。屁股部分的痛苦在于确保在每个函数的开头调用 StartTickCount() 并在每个退出点调用 EndTickCount():
Private Function SomeFuction() as String
' indicate the function started
StartTickCount("MyClass.SomeFunction")
' some logic that causes the function to end
If (some logic) Then
EndTickCount("MyClass.SomeFunction")
Return "Hello!"
End If
' final exit point
EndTickCount("MyClass.SomeFunction")
Return "World"
End Function
无论如何,是否有任何内置功能,无论是通过 VS 2010 调试器还是在 中System.Reflection
命名空间,在 VB.NET 中执行类似的操作?
基本上,我想要的是记录每个函数被调用的次数、该函数花费的总时间、该函数花费的平均秒数以及该函数中单次调用花费的最大时间。功能。
我当然可以手工编写(因为我们已经在 VB6 中编写过一次),但如果有现有的工具可以使其更容易,我宁愿使用它们。
In our VB6 applications, we added a few utility functions for tracking the amount of time spent in a function. We did this to track performance bottlenecks.
Basically, how it worked was there were two utility functions: StartTickCount() and EndTickCount(). You would pass the name of the function in each, and the functions would use a dictionary to get the tick count when StartTickCount() was called, and then subtract the tick count when EndTickCount() was called. This wasn't perfect because it didn't of course take into consideration that calls to get tick count takes time, etc, but basically it worked for our purposes. The pain in the butt part was making sure to call StartTickCount() at the beginning of each function and EndTickCount() at each exit point:
Private Function SomeFuction() as String
' indicate the function started
StartTickCount("MyClass.SomeFunction")
' some logic that causes the function to end
If (some logic) Then
EndTickCount("MyClass.SomeFunction")
Return "Hello!"
End If
' final exit point
EndTickCount("MyClass.SomeFunction")
Return "World"
End Function
Anyway, is there any functionality built in, either through the VS 2010 debugger or in the System.Reflection
namespace, to do something similar in VB.NET?
Basically, what I want is to log the number of times each function is called, the total amount to time spent in that function, the average number of seconds spent in that function, and the maximum amount of time spent in a single call in that function.
I can certainly write this by hand (since we already did once in VB6), but if there are existing tools to make it easier, I'd rather use them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过根据您的需要修改/调整以下代码来实现相同的结果:
您可以查看有关 TimeSpan 的文档以直接获取以毫秒或其他格式表示的时间。
You can achieve the same results by modifying/adapting the following code to your needs:
You can check documentation about TimeSpan to get directly time in milliseconds or in other formats.
我想看看秒表类 - 它是为此类事情设计的。
然而,已经有一个很棒的工具可以做到这一点,而且它实际上只需很少的工作就可以做得更好。它还有 10 天的免费试用期。
http://www.jetbrains.com/profiler/
(我不工作,也不是隶属于jetbrains - 但我希望我是因为他们制造了一些非常酷的产品)
I'd have a look at the stopwatch class - it's designed for this type of thing.
However there is also a fantastic tool out there to do this already and it actually does a better job with very little work. It has a 10-day free trial as well.
http://www.jetbrains.com/profiler/
(I don't work for and am not affiliated with jetbrains - but I wish I was because they make some really cool products)
听起来您需要在应用程序中添加一些分析。您可能需要从初学者分析指南开始与VS。
您还可以在应用程序中添加性能计数器以获取运行时指标。请参阅 http://msdn.microsoft.com/en -us/library/w8f5kw2e(v=vs.100).aspx。
有关工具和技术的更完整列表,请参阅 http://msdn.microsoft.com /en-us/magazine/hh288073.aspx。
It sounds like you need to add some profiling in your application. You may want to start with the beginner's guide to profiling with VS.
You can also add performance counters in your application for runtime metrics. See http://msdn.microsoft.com/en-us/library/w8f5kw2e(v=vs.100).aspx.
For a fuller list of tools and techniques, see http://msdn.microsoft.com/en-us/magazine/hh288073.aspx.
我经常使用这个辅助类。它基本上按照@AndreaAntonangeli的建议进行操作,但安排了更清晰的代码:
使用示例:
您的输出应该类似于:
I ofently use this helper class. It basically does what @AndreaAntonangeli suggested but arranged to have a cleaner code:
Example of use:
Your output should be something like: