I am trying to simply measure my code execution time.
public class PerfMeasureUtil {
private static final Logger LOG = Logger.getLogger(PerfMeasureUtil.class.getName());
private long startTime;
private long duration;
private static final EnvironmentProperties env = new EnvironmentProperties();
/**
* start counting the time
*
* @return self
*/
public PerfMeasureUtil start() {
if (env.isDev()) {// || env.isTest()) {
LOG.log(Level.INFO, "====================================== measurement starts");
this.startTime = System.currentTimeMillis();
}
return this;
}
/**
* stops counting the time and returns the result
*
* @param methodName
* @return
*/
public long stop(String methodName) {
if (env.isDev()) {// || env.isTest()) {
this.duration = System.currentTimeMillis() - startTime;
LOG.log(Level.INFO, "====================================== {0}", methodName);
LOG.log(Level.INFO, "time spend: {0}ms", duration);
LOG.log(Level.INFO, "======================================");
return duration;
}
return -1;
}
}
EnvironmentProperties
has a boolean
property based on the Maven build type. The measurement should be executed only on dev, or maybe testing environment, not in production. Now I call it like this everywhere, where it is needed:
PerfMeasureUtil measureUtil = new PerfMeasureUtil().start();
foo = readFooFromSomewhere();
bar.applyFoo();
printResult(bar);
measureUtil.stop("FooBarApplication.aplyFooOnBar");
Is there something that could be made clearly better? I am not sure about the permanent if-check even on other built types when it is not needed.
And of course the pollution of code is pretty bad with it, but this way I have more control on the code amount measured. Maybe annotation could help?
AspectJ
maybe. \$\endgroup\$