概要 - Summary

"java-cpt"は、Javaで性能比較を容易に行う為のモジュールです。

ユーザが定義したメソッドを指定された回数実行し、実行に要した時間を出力します。

測定を目的とするものではなく、あくまで相対的な比較用のツールです。

"java-cpt" is a module that enable you to execute comparative performance test easily. It executes methods defined by the user and print milli-seconds to execute the method. It is not for to measure absolute values, but for to check comparative performance.

サンプル - Sample

以下のようにして、パフォーマンス比較を行う事が出来ます。

java.lang.reflect.Methodクラスを使用するため、このクラスのAPIについて理解しておく必要があります。

You can use this like following sample.

  1. public static void main(String[] args) throws Exception
  2. {
  3. int executeTimes = 1000000;
  4. Object instance = new InstanciationSample();
  5. ComparativePerformanceTest test = null;
  6. Method getHashtable = InstanciationSample.class.getMethod("getHashtable");
  7. test = new ComparativePerformanceTestImpl("Hashtable");
  8. test.execute(executeTimes, instance, getHashtable, (Object[]) null);
  9. Method getHashMap = InstanciationSample.class.getMethod("getHashMap");
  10. test = new ComparativePerformanceTestImpl("HashMap");
  11. test.execute(executeTimes, instance, getHashMap, (Object[]) null);
  12. Method getConcurrentHashMap = InstanciationSample.class
  13. .getMethod("getConcurrentHashMap");
  14. test = new ComparativePerformanceTestImpl("ConcurrentHashMap");
  15. test.execute(executeTimes, instance, getConcurrentHashMap, (Object[]) null);
  16. }
  17. public static Map<Object, Object> getHashtable()
  18. {
  19. return new Hashtable<Object, Object>();
  20. }
  21. public static Map<Object, Object> getHashMap()
  22. {
  23. return new HashMap<Object, Object>();
  24. }
  25. public static Map<Object, Object> getConcurrentHashMap()
  26. {
  27. return new ConcurrentHashMap<Object, Object>();
  28. }

出力結果は以下のようになります。

Following output to console.

Hashtable : 281milsec.
HashMap : 328milsec.
ConcurrentHashMap : 1672milsec.

また、java.util.logging.Loggerオブジェクトをセットする事で、この出力をログに出力する事ができます。

The results are outputed to log file if java.util.logging.Logger object was set to Test object.

  1. Logger logger = Logger.getLogger("hoge");
  2. test.setLogger(logger);
  3. test.execute(executeTimes, instance, executeMethod, (Object[]) null);

デフォルトでは以下のように出力されます。

Following output by default.

2009/08/22 22:38:43 jp.sourceforge.javacpt.impl.ComparativePerformanceTestImpl printResult
情報: Hashtable : 266milsec.
2009/08/22 22:38:43 jp.sourceforge.javacpt.impl.ComparativePerformanceTestImpl printResult
情報: HashMap : 297milsec.
2009/08/22 22:38:45 jp.sourceforge.javacpt.impl.ComparativePerformanceTestImpl printResult
情報: ConcurrentHashMap : 1890milsec.

ヘルパーを使ったサンプル - Sample using Helper class

ヘルパークラス(1.0.1から追加)を使用するとよりシンプルかつDRYに使用できます。

Helper class(from 1.0.1) make you more DRY!

  1. ComparativePerformanceTest test = ComparativePerformanceTestHelper.initialize(
  2. executeTimes, instance);
  3. ComparativePerformanceTestHelper.invoke(test, "Hashtable", "getHashtable");
  4. ComparativePerformanceTestHelper.invoke(test, "HashMap", "getHashMap");
  5. ComparativePerformanceTestHelper.invoke(test, "ConcurrentHashMap",
  6. "getConcurrentHashMap");

クラスメソッドの場合は、初期化する際にclassオブジェクトを渡して下さい。

If you invoke class method, the 2nd arg of initialize method requires class object.

  1. ComparativePerformanceTest test = ComparativePerformanceTestHelper.initialize(
  2. executeTimes, InstantiationSample.class);
  3. ComparativePerformanceTestHelper.invoke(test, "Hashtable", "getHashtable");
  4. ComparativePerformanceTestHelper.invoke(test, "HashMap", "getHashMap");
  5. ComparativePerformanceTestHelper.invoke(test, "ConcurrentHashMap",
  6. "getConcurrentHashMap");

Loggerを渡す場合はアクセサか、initializeメソッドの引数で設定します。

If you need Logger, use accessor method or the 3rd arg of initialize method.

  1. ComparativePerformanceTest test = ComparativePerformanceTestHelper.initialize(executeTimes,
  2. instance, Logger.getLogger("hoge"));