-1

I would like to measure the CPU usage within a Java application, for example the

 System.out.println("...") 

of HelloWorld application. Any idea? I do not want to measure the CPU usage of the entire application, only the methods within the application.

Is there a way to correlate the the total execution time of the application with the CPU usage of the entire application?

Message Passing
  • 119
  • 3
  • 13
  • This question is a bit obtuse because there may be more than one method executing at once. The profiling methods I use are geared towards the usage of the entire application. You can see how much time it takes to evaluate a method, and this could be correlated to the CPU usage. – Tim Biegeleisen Oct 28 '15 at 08:48
  • Ok. Once I measured the time to execute the method (e.g `System.out.println("..."))`, how do I correlate the time measured to the CPU usage? – Message Passing Oct 28 '15 at 08:56
  • Can you update your question with why you want to do this? Even if you could see what the CPU spike might be when executing a single method, that would not necessarily be a useful point of data for load testing your overall application. – Tim Biegeleisen Oct 28 '15 at 09:05
  • Question updated. BTW, Is there a way to correlate the the total execution time of the application with the CPU usage of the entire application? – Message Passing Oct 28 '15 at 09:16
  • 1
    You've approached profiling in a completely wrong way. There's no use in measuring small, "random" parts of an application. You measure the performance of an application as a whole, which will potentially give you "hotspots" i.e. the places that require the most resources. In order to improve performance, you'll concentrate on the hotspots. Everything else is just noise. You could invest your time a lot better in something else than trying to measure `System.out.println()`. – Kayaman Oct 28 '15 at 09:26
  • @Kayaman is right. Set aside your magnifying glass because what you're looking at is almost certainly not what spends time. Here's how to make programs fast: First, *have a problem*. There is no performance problem that doesn't come down to *things making you wait longer than you want*. Second, now you have a bug. The bug is that it's doing things it doesn't have to do. So find that bug. You *don't do it by eyeballing the code.* [*Here's a simple way to do it in java.*](http://stackoverflow.com/a/317160/23771) – Mike Dunlavey Oct 28 '15 at 14:54

1 Answers1

0

In my opinion, there is no point in profiling single methods. Because a) it is very easy to spot resource hogging methods (ie if you have to iterate through 10000 of input values to find only 100 valid values you want to output, then it is obvious that you are not effective and waste CPU time on 9900 useless iterations) and b) key of efficient design is to foresee the resource hog, and again using the same example as in point a, it is up to you as designer to find more efficient way of solving the problem, using leaner and faster data structure or just finding a way to trim the junk data from the method.

EDIT: And no, there is no way to correlate execution time and cpu usage, thanks to the java JIT and the fact that it uses garbage collector autonomously.

The Law
  • 344
  • 3
  • 20