分析短期 Java 应用程序
是否有任何 Java 分析器可以分析短期应用程序?到目前为止,我发现的分析器似乎适用于一直运行直到用户终止的应用程序。但是,我想分析像命令行实用程序一样工作的应用程序,它立即运行并退出。 VisualVM 或 NetBeans Profiler 等工具甚至无法识别应用程序已运行。
我正在寻找类似于 Python 的 cProfile 的东西,因为当应用程序退出时会返回分析器结果。
Is there any Java profiler that allows profiling short-lived applications? The profilers I found so far seem to work with applications that keep running until user termination. However, I want to profile applications that work like command-line utilities, it runs and exits immediately. Tools like visualvm or NetBeans Profiler do not even recognize that the application was ran.
I am looking for something similar to Python's cProfile, in that the profiler result is returned when the application exits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以使用 JVM 内置的 HPROF 来分析您的应用程序。
它提供了两种方法:
采样
该方法揭示了在堆栈顶部。
计时
此方法对方法的实际调用进行计数。检测代码已预先由 JVM 注入。
注意:此方法会大大减慢执行时间。
对于这两种方法,如果
file=
选项不存在,则默认文件名是java.hprof.txt
。可以使用 java -agentlib:hprof=help 获得完整帮助,也可以在 Oracle 文档
You can profile your application using the JVM builtin HPROF.
It provides two methods:
Sampling
This method reveals how often methods were found on top of the stack.
Timing
This method counts the actual invocations of a method. The instrumenting code has been injected by the JVM beforehand.
Note: this method will slow down the execution time drastically.
For both methods, the default filename is
java.hprof.txt
if thefile=
option is not present.Full help can be obtained using
java -agentlib:hprof=help
or can be found on Oracles documentationSun Java 6 有
java -Xprof
开关将为您提供一些分析数据。
Sun Java 6 has the
java -Xprof
switch that'll give you some profiling data.运行 30 秒的程序并不是短暂的。您想要的是一个可以启动您的程序的分析器,而不必附加到正在运行的系统。我相信大多数分析器都可以做到这一点,但您很可能更喜欢集成在 IDE 中的分析器。看看 Netbeans。
A program running 30 seconds is not shortlived. What you want is a profiler which can start your program instead of you having to attach to a running system. I believe most profilers can do that, but you would most likely like one integrated in an IDE the best. Have a look at Netbeans.
对短时间运行的 Java 应用程序进行分析有几个技术困难:
您可以按照 @Mike 的建议,通过修改应用程序以循环运行多次来解决此问题。如果您的应用程序调用
System.exit()
,您将会遇到问题,但主要问题是...我不知道分析器是否尝试补偿 JVM 预热效应。但即使确实如此,这些影响也会影响应用程序的实际行为,并且应用程序开发人员无法采取很多措施来减轻这些影响。
回到我之前的观点……如果您在循环中运行一个短暂的应用程序,那么您实际上正在做一些修改其正常执行模式并删除 JVM 预热组件的操作。因此,当您优化修改后的应用程序中占用(例如)50% 执行时间的方法时,这实际上是 50% 的时间不包括 JVM 预热。如果应用程序正常执行时 JVM 预热占用了(比如说)80% 的执行时间,那么您实际上优化了 20% 的 50%……而这是不值得的。
Profiling a short running Java applications has a couple of technical difficulties:
You can address this by modifying the application to run a number of times in a loop, as suggested by @Mike. You'll have problems if your app calls
System.exit()
, but the main problem is ...I don't know if profilers attempt to compensate to for JVM warmup effects. But even if they do, these effects influence your applications real behavior, and there is not a great deal that the application developer can do to mitigate them.
Returning to my previous point ... if you run a short lived app in a loop you are actually doing something that modifies its normal execution pattern and removes the JVM warmup component. So when you optimize the method that takes (say) 50% of the execution time in the modified app, that is really 50% of the time excluding JVM warmup. If JVM warmup is using (say) 80% of the execution time when the app is executed normally, you are actually optimizing 50% of 20% ... and that is not worth the effort.
如果需要的时间不够长,只需在其周围缠绕一个循环,如果您愿意,可以是无限循环。这不会影响函数或代码行所花费的包含时间百分比。然后,鉴于这需要大量时间,我就是依赖这个技术。这表明哪些代码行(无论它们是否是函数调用)花费的时间比例最高,因此如果可以避免它们,将获得最大的收益。
If it doesn't take long enough, just wrap a loop around it, an infinite loop if you like. That will have no effect on the inclusive time percentages spent either in functions or in lines of code. Then, given that it's taking plenty of time, I just rely on this technique. That tells which lines of code, whether they are function calls or not, are costing the highest percentage of time and would therefore gain the most if they could be avoided.
在打开分析的情况下启动您的应用程序,等待分析器附加。任何符合 Java 分析架构的分析器都应该可以工作。我已经使用 NetBeans 的分析器尝试过此操作。
基本上,当您的应用程序启动时,它会在执行之前等待附加分析器。因此,从技术上讲,甚至可以分析代码执行行。
通过这种方法,您可以分析线程、内存、CPU、方法/类调用时间/持续时间等各种信息...
http://profiler.netbeans.org/
start your application with profiling turned on, waiting for profiler to attach. Any profiler that conforms to Java profiling architecture should work. i've tried this with NetBeans's profiler.
basically, when your application starts, it waits for a profiler to be attached before execution. So, technically even line of code execution can be profiled.
with this approach, you can profile all kinds of things from threads, memory, cpu, method/class invocation times/duration...
http://profiler.netbeans.org/
SD Java Profiler 可以捕获语句块执行计数数据,无论多短你的跑步是。相对执行计数将告诉您时间花在哪里。
The SD Java Profiler can capture statement block execution-count data no matter how short your run is. Relative execution counts will tell you where the time is spent.
您可以使用测量(计量)记录:http://www .jinspired.com/site/case-study-scala-compiler-part-9
您还可以检查生成的快照: http://www.jinspired .com/site/case-study-scala-compiler-part-10
免责声明:我是 JXInsight/OpenCore 的架构师。
You can use a measurement (metering) recording: http://www.jinspired.com/site/case-study-scala-compiler-part-9
You can also inspect the resulting snapshots: http://www.jinspired.com/site/case-study-scala-compiler-part-10
Disclaimer: I am the architect of JXInsight/OpenCore.
我建议你试试你的套件。它可以从一开始就进行分析,并在程序完成时转储结果。您必须付费,但您可以获得评估许可证或在没有许可证的情况下使用 EAP 版本。 (时间有限)
I suggest you try yourkit. It can profile from the start and dump the results when the program finishes. You have to pay for it but you can get an eval license or use the EAP version without one. (Time limited)
YourKit 可以拍摄配置文件会话的快照,稍后可以在 YourKit GUI 中进行分析。我使用它来分析我所使用的命令行短期应用程序。请参阅我对 此问题了解详细信息。
YourKit can take a snapshot of a profile session, which can be later analyzed in the YourKit GUI. I use this to feature to profile a command-line short-lived application I work on. See my answer to this question for details.