前回、log4jとlogbackのパフォーマンス比較では、Loggerの実装を比較したんだけど、commons-loggingやSLF4jのようなLoggerのFacadeの比較もやらなきゃなーとおもったので、パフォーマンスを調べてみた。
以下のケースで実験。
- commons-logging + log4j
- SLF4J + log4j
条件は以下。
- CPU:Core 2 Duo T8100 2.1GHz
- Memory:2G
- PC:藤原のDellラップトップ
- Eclipse Ganymedeから実行
- 1回のテストで100万行出力。これを3回連続で実施。
- ログ出力のフォーマットは同じ(文字数もそろえた)
- ログ出力開始から終了までの秒数を測定
ソースコードは以下のような感じ。Loggerを差し替えて実施している。
private static final Log logger = LogFactory.getLog(CommonsLoggingPerformanceTest.class); public static void main(String[] args) { long start = System.currentTimeMillis(); int cnt = 1000; for (int i = 0; i < cnt; i++) { for (int j = 0; j < cnt; j++) { logger.debug("fujihara."); } } long end = System.currentTimeMillis(); logger.debug("total:" + String.valueOf((end -start) / 1000)); }
結果は以下。
// 1回目 2009-07-20 14:35:10,182 DEBUG com.daipresents.logging.facade.test.CommonsLoggingPerformanceTest - total:89 // 2回目 2009-07-20 14:40:15,918 DEBUG com.daipresents.logging.facade.test.CommonsLoggingPerformanceTest - total:86 // 3回目 2009-07-20 14:42:04,358 DEBUG com.daipresents.logging.facade.test.CommonsLoggingPerformanceTest - total:91
Ave 88.66秒。
// 1回目 2009-07-20 14:44:26,058 DEBUG com.daipresents.logging.facade.test.XXSLF4JLoggingPerformanceTest - total:100 // 2回目 2009-07-20 14:46:01,849 DEBUG com.daipresents.logging.facade.test.XXSLF4JLoggingPerformanceTest - total:84 // 3回目 2009-07-20 14:48:04,455 DEBUG com.daipresents.logging.facade.test.XXSLF4JLoggingPerformanceTest - total:98
Ave 94秒。
SLF4Jのほうが若干遅い。でも、これからはSLF4Jだろうね。