"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.
以下のようにして、パフォーマンス比較を行う事が出来ます。
java.lang.reflect.Methodクラスを使用するため、このクラスのAPIについて理解しておく必要があります。
You can use this like following sample.
- public static void main(String[] args) throws Exception
- {
- int executeTimes = 1000000;
- Object instance = new InstanciationSample();
- ComparativePerformanceTest test = null;
- Method getHashtable = InstanciationSample.class.getMethod("getHashtable");
- test = new ComparativePerformanceTestImpl("Hashtable");
- test.execute(executeTimes, instance, getHashtable, (Object[]) null);
- Method getHashMap = InstanciationSample.class.getMethod("getHashMap");
- test = new ComparativePerformanceTestImpl("HashMap");
- test.execute(executeTimes, instance, getHashMap, (Object[]) null);
- Method getConcurrentHashMap = InstanciationSample.class
- .getMethod("getConcurrentHashMap");
- test = new ComparativePerformanceTestImpl("ConcurrentHashMap");
- test.execute(executeTimes, instance, getConcurrentHashMap, (Object[]) null);
- }
- public static Map<Object, Object> getHashtable()
- {
- return new Hashtable<Object, Object>();
- }
- public static Map<Object, Object> getHashMap()
- {
- return new HashMap<Object, Object>();
- }
- public static Map<Object, Object> getConcurrentHashMap()
- {
- return new ConcurrentHashMap<Object, Object>();
- }
出力結果は以下のようになります。
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.
デフォルトでは以下のように出力されます。
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.
ヘルパークラス(1.0.1から追加)を使用するとよりシンプルかつDRYに使用できます。
Helper class(from 1.0.1) make you more DRY!
- ComparativePerformanceTest test = ComparativePerformanceTestHelper.initialize(
- executeTimes, instance);
- ComparativePerformanceTestHelper.invoke(test, "Hashtable", "getHashtable");
- ComparativePerformanceTestHelper.invoke(test, "HashMap", "getHashMap");
- ComparativePerformanceTestHelper.invoke(test, "ConcurrentHashMap",
- "getConcurrentHashMap");
クラスメソッドの場合は、初期化する際にclassオブジェクトを渡して下さい。
If you invoke class method, the 2nd arg of initialize method requires class object.
- ComparativePerformanceTest test = ComparativePerformanceTestHelper.initialize(
- executeTimes, InstantiationSample.class);
- ComparativePerformanceTestHelper.invoke(test, "Hashtable", "getHashtable");
- ComparativePerformanceTestHelper.invoke(test, "HashMap", "getHashMap");
- ComparativePerformanceTestHelper.invoke(test, "ConcurrentHashMap",
- "getConcurrentHashMap");
Loggerを渡す場合はアクセサか、initializeメソッドの引数で設定します。
If you need Logger, use accessor method or the 3rd arg of initialize method.