分析短期 Java 应用程序

发布于 2024-08-29 08:45:25 字数 206 浏览 6 评论 0原文

是否有任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

澉约 2024-09-05 08:45:25

您可以使用 JVM 内置的 HPROF 来分析您的应用程序。

它提供了两种方法:

  1. 对堆栈上的活动方法进行采样
  2. 计时使用注入的字节码(BCI,字节编码器注入)的方法执行时间

采样

该方法揭示了在堆栈顶部。

java -agentlib:hprof=cpu=samples,file=profile.txt ...

计时

此方法对方法的实际调用进行计数。检测代码已预先由 JVM 注入。

java -agentlib:hprof=cpu=times,file=profile.txt ...

注意:此方法会大大减慢执行时间。


对于这两种方法,如果 file= 选项不存在,则默认文件名是 java.hprof.txt

可以使用 java -agentlib:hprof=help 获得完整帮助,也可以在 Oracle 文档

You can profile your application using the JVM builtin HPROF.

It provides two methods:

  1. sampling the active methods on the stack
  2. timing method execution times using injected bytecode (BCI, byte codee injection)

Sampling

This method reveals how often methods were found on top of the stack.

java -agentlib:hprof=cpu=samples,file=profile.txt ...

Timing

This method counts the actual invocations of a method. The instrumenting code has been injected by the JVM beforehand.

java -agentlib:hprof=cpu=times,file=profile.txt ...

Note: this method will slow down the execution time drastically.


For both methods, the default filename is java.hprof.txt if the file= option is not present.

Full help can be obtained using java -agentlib:hprof=help or can be found on Oracles documentation

云朵有点甜 2024-09-05 08:45:25

Sun Java 6 有 java -Xprof开关将为您提供一些分析数据。

-Xprof            output cpu profiling data

Sun Java 6 has the java -Xprof switch that'll give you some profiling data.

-Xprof            output cpu profiling data
话少心凉 2024-09-05 08:45:25

运行 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.

娇柔作态 2024-09-05 08:45:25

对短时间运行的 Java 应用程序进行分析有几个技术困难:

  • 分析工具通常通过定期对处理器的 SP 或 PC 寄存器进行采样来了解应用程序当前正在执行的位置。如果您的应用程序是短暂的,则可能会采集不足的样本来获得准确的情况。

您可以按照 @Mike 的建议,通过修改应用程序以循环运行多次来解决此问题。如果您的应用程序调用 System.exit(),您将会遇到问题,但主要问题是...

  • 短期 Java 应用程序的性能特征可能会因 JVM 预热而扭曲影响。大量时间将花费在加载应用程序所需的类上。然后你的代码(和库代码)将被解释一下,直到 JIT 编译器找出需要编译为本机代码的内容。最后,JIT 编译器将花时间完成其工作。

我不知道分析器是否尝试补偿 JVM 预热效应。但即使确实如此,这些影响也会影响应用程序的实际行为,并且应用程序开发人员无法采取很多措施来减轻这些影响。

回到我之前的观点……如果您在循环中运行一个短暂的应用程序,那么您实际上正在做一些修改其正常执行模式并删除 JVM 预热组件的操作。因此,当您优化修改后的应用程序中占用(例如)50% 执行时间的方法时,这实际上是 50% 的时间不包括 JVM 预热。如果应用程序正常执行时 JVM 预热占用了(比如说)80% 的执行时间,那么您实际上优化了 20% 的 50%……而这是不值得的。

Profiling a short running Java applications has a couple of technical difficulties:

  • Profiling tools typically work by sampling the processor's SP or PC register periodically to see where the application is currently executing. If your application is short-lived, insufficient samples may be taken to get an accurate picture.

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 ...

  • The performance characteristics of a short-lived Java application are likely to be distorted by JVM warm-up effects. A lot of time will be spent in loading the classes required by your app. Then your code (and library code) will be interpreted for a bit, until the JIT compiler has figured out what needs to be compiled to native code. Finally, the JIT compiler will spend time doing its work.

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.

執念 2024-09-05 08:45:25

如果需要的时间不够长,只需在其周围缠绕一个循环,如果您愿意,可以是无限循环。这不会影响函数或代码行所花费的包含时间百分比。然后,鉴于这需要大量时间,我就是依赖这个技术。这表明哪些代码行(无论它们是否是函数调用)花费的时间比例最高,因此如果可以避免它们,将获得最大的收益。

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.

万水千山粽是情ミ 2024-09-05 08:45:25

在打开分析的情况下启动您的应用程序,等待分析器附加。任何符合 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/

〗斷ホ乔殘χμё〖 2024-09-05 08:45:25

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.

葬心 2024-09-05 08:45:25

您可以使用测量(计量)记录: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.

胡渣熟男 2024-09-05 08:45:25

我建议你试试你的套件。它可以从一开始就进行分析,并在程序完成时转储结果。您必须付费,但您可以获得评估许可证或在没有许可证的情况下使用 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)

成熟的代价 2024-09-05 08:45:25

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文