For a particular segment of Java code, I'd like to measure:
- execution time (most likely thread execution time)
- memory usage
- CPU load (specifically attributable to the code segment)
I'm a relative Java novice and am not familiar with how this might be achieved. I've been referred to JMX, however I'm not sure how that might be used, and JMX looks a bit 'heavy' for what I'm looking to do.
Ideally I'd like some measurement class that can be told what I would like to measure, with the option of calling a start()
method prior to a code segment and a stop()
method after. Relevant metrics would be logged to a file I specify.
For example:
import com.example.metricLogger;
metricLogger logger = new metricLogger();
logger.setLogPath(pathToLogFile);
logger.monitor(executionTime);
logger.monitor(memoryUsage);
logger.monitor(cpuLoad);
logger.start();
/* Code to be measured */
logger.stop();
Is there any standard/common/conventional way of achieving this in Java?
Such measurements are for one-off performance comparisons and so I'm not looking for any in-production long-term monitoring processes.
I'm more than happy to be referred to tutorials or external examples and don't expect a full answer here. That said, if anything as simple as the above can be achieved a realistic example would go down really well.