私は通常、実行時の感覚を得るためにjunitまたはngunitを使用します。コンソールとは別に、IDEでテストを実行するのにかかった時間がわかります。
または、次のように開始時刻と終了時刻をログに記録することもできます。
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
doStuff();
date = new Date();
System.out.println(dateFormat.format(date));
または、実行時間をログに記録することもできます。
long start = System.currentTimeMillis();
System.out.println("Going to call the method.");
doStuff();
System.out.println("Method execution completed.");
long elapsedTime = System.currentTimeMillis() - start;
System.out.println("Method execution time: " + elapsedTime + " milliseconds.");
コンソールですべてを混同する代わりに、他の場所(つまりファイル)に書き込みたい場合は、log4jを使用できます。
logger.info(dateFormat.format(date);
より洗練されたものにしたい場合は、AOPポイントカットを使用して、たとえばSpringaopを使用したメソッド実行の開始時間と終了時間をログに記録できます。コードはここからです:http://veerasundar.com/blog/2010/01/spring-aop-example-profiling-method-execution-time-tutorial/
@Aspect
public class BusinessProfiler {
@Pointcut("execution(* com.veerasundar.spring.aop.*.*(..))")
public void businessMethods() { }
@Around("businessMethods()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
System.out.println("Going to call the method.");
Object output = pjp.proceed();
System.out.println("Method execution completed.");
long elapsedTime = System.currentTimeMillis() - start;
System.out.println("Method execution time: " + elapsedTime + " milliseconds.");
return output;
}
}
コンソールへの印刷は、IOをブロックしているため、ボトルネックになる可能性があります-なぜコンソールにそれほど多くの印刷をしたいのですか?このテストには価値がありますか?