Measure Java Program Performance

人盡茶涼 提交于 2020-01-04 17:52:30

问题


I had an old application, a JAR file, that went through some enhancements. Basically some parts of the code had to be modified along with modifying some of the logic.

Comparing the OLD version against the NEW version, the NEW version is about 2X slower than the old one.

I'm trying to narrow down whats causing the slow down, but I'm finding myself measuring the time for certain for-loops using System.println with System.currentTimeMillis(). This is really getting very tedious.

Is there a Java performance tool that will help me in figuring out why the NEW JAR is about 2X slower than the old one?

Thanks in advance.


回答1:


JProfiler has the capability to compare CPU snapshots. Record the execution for the old and the new JAR file and save snapshots (if the JVM exits at the end, configure a "JVM exit" trigger that saves a snapshot).

Then open the snapshot comparison window with "Session->Compare Snapshots in New Window" and add the two snapshot. A hot spots comparison will look like this (a view filter is set in this case):

It will immediately show you which methods are responsible for the increase in execution time.

Another way to analyze the differences in execution time is the call tree comparison which will look like this:

Disclaimer: My company develops JProfiler.




回答2:


You should use a profiler. This will show you which methods are taking the most time (and what is calling them), without you having to guess which ones to measure.

Java comes with a built-in profiler called hprof, but see also:

  • https://stackoverflow.com/questions/14762/please-recommend-a-java-profiler
  • 5 things you didn't know about ... Java performance monitoring
  • The JConsole and VisualVM tools



回答3:


Depending on how long-running the process is, I'd think about Visual VM 1.3.3. If you download all the plugins, you'll be able to see heap, threads, objects, etc. That ought to help, and it won't cost a dime.

I believe it assumes the Oracle/Sun JVM.




回答4:


A profiler tool like YourKit or something to measure performance reliably like Hyperic's Sigar is a good canditate for your case. Have a look at those tools.
The former will find bottlenecks in your code and/or memory leaks (not all of them) while as the latter is an API that you can measure performance reliably since Oracle's JVM & OpenJDK have no way of getting perfomance metrics reliably/consistently/accurately (like CPU wall clock time or CPU time spent from the application, memory usage, application threads, etc).

By default, Java provides packages for these things.

For example:
java.lang.management.ManagementFactory
java.lang.management.ThreadMXBean
but depending on your case they may or may not be adequate (keep in mind they are OK for most cases unless we are talking about something critical).



来源:https://stackoverflow.com/questions/9550174/measure-java-program-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!