LoginSignup
5
4

More than 5 years have passed since last update.

Javaのベンチマークツール、JMHをコマンドラインから実行するときのメモ

Posted at

これは何

JMHの測定クラスををeclipseやNetbeansなどのIDEからではなく、コマンドラインで実行するときの覚書。

前提

測定クラスにmain()メソッドが書かれていること

package jmhTest;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
public class MyBenchmark {

    private static int count = 300000;
    private final static String reg = "\\d+";

    private static String testWord = "1234567";

    @Benchmark
    public void test1CharacterisDigit() {
        for(int i = 0; i < count; i++) {
            test1(testWord);
        }
    }

    @Benchmark
    public void test2IntegerParseInt() {
        for(int i = 0; i < count; i++) {
            test2(testWord);
        }
    }

    @Benchmark
    public void test3Reg() {
        for(int i = 0; i < count; i++) {
            test3(testWord);
        }
    }

    private static boolean test1(String s) {

        boolean isDigit = true;

        for (int i = 0; i < s.length(); i++) {
            isDigit = Character.isDigit(s.charAt(i));
            if (!isDigit) {
                break;
            }
        }

        return isDigit;
    }

    private static boolean test2(String s) {

        boolean isDigit = true;

        try {
            Integer.parseInt(s);
        } catch (Exception e) {
            isDigit = false;
        }

        return isDigit;

    }

    private static boolean test3(String s) {

        return s.matches(reg);

    }

    public static void main(String... args) throws RunnerException {

        Options opt = new OptionsBuilder().include(MyBenchmark.class.getSimpleName()).warmupIterations(5)
                .measurementIterations(5).forks(2).build();

        if(args.length > 0) {
            count = Integer.parseInt(args[0]);
        }

        if(args.length > 1) {
            testWord = args[1];
        }

        System.out.println("テスト設定 繰り返し数:" + count);
        System.out.println("テスト設定 判定ワード:" + testWord);

        new Runner(opt).run();
    }

}

手順

プロジェクトのフォルダに移動して以下のコマンドを実行する。

  • jarファイルを作る。
maven clean install
  • テストしたいクラスを指定して実行
java -classpath target/benchmarks.jar jmhTest.MyBenchmark

繰り返し回数や、テストキーワードを引数で指定したい、というような場合もこれなら指定可能。
java -jar コマンドだと、メインクラス(org.openjdk.jmh.Main)がデフォルトで指定されているのでウォーミングアップ回数などもデフォルト値が指定されてしまう。

参考

Javaのベンチマークツール、JMHの導入
Javaのベンチマークツール、JMHにテストコードを書いて計測してみる

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4