私は現在、ある種のデータベースベンチマークアプリケーションに取り組んでいます。基本的に、私がやろうとしているのは、特定の期間、データベースに対してすべて同じ操作(例:読み取り操作)を繰り返す特定の数のクライアントをスレッドを使用してシミュレートすることです。
この間、各スレッドで、データベースから回答を取得するための平均遅延を測定したいと思います。
私の最初の選択は、ThreadMXBeanのgetThreadCpuTime()メソッド(http://docs.oracle.com/javase/7/docs/api/java/lang/management/ThreadMXBean.html)に依存することでしたが、要点は、操作が実行されることです。測定するには速すぎます(操作前のgetThreadCpuTime()は、操作後のgetThreadCpuTime()と同じです)。
問題を理解して説明するために、少し実験を行いました。
public class ExampleClass {
class LongRunningThread extends Thread {
    private int n;
    public LongRunningThread(int n) {
        this.n = n;
    }
    public void run() {
        ArrayList l = new ArrayList();
        for (int i = 0; i < n; i++) {
            l.add(new Object());
        }
        long time = ManagementFactory.getThreadMXBean().getThreadCpuTime(this.getId());
        System.out.println("Long running thread " + this.getId() + " execution time: " + time);
    }
}
class MyThread extends Thread {
    int n;
    public MyThread(int n) {
        this.n = n;
    }
    public void run() {
        ArrayList l = new ArrayList();
        for (int i = 0; i < n; i++) {
            l.add(new Object());
        }
        long time = ManagementFactory.getThreadMXBean().getThreadCpuTime(this.getId());
        System.out.println("My thread " + this.getId() + " execution time: " + time);
    }
}
public static void main(String [] args) {
        System.out.println("Cpu time supported? " + ManagementFactory.getThreadMXBean().isThreadCpuTimeSupported());
    System.out.println("Cpu time enabled? " + ManagementFactory.getThreadMXBean().isThreadCpuTimeEnabled());
    for (int i = 1; i < 10; ++i) {
        new LongRunningThread(i*1000000).start();
    }
    for (int i = 1; i < 10; ++i) {
        new MyThread(i*100).start();
    }
}
Output:
Cpu time supported? true
Cpu time enabled? true
My thread 18 execution time: 0
My thread 26 execution time: 0
My thread 20 execution time: 0
My thread 22 execution time: 0
My thread 24 execution time: 0
My thread 21 execution time: 0
My thread 25 execution time: 0
My thread 19 execution time: 0
My thread 23 execution time: 0
Long running thread 9 execution time: 15600100
Long running thread 10 execution time: 15600100
Long running thread 11 execution time: 46800300
Long running thread 12 execution time: 31200200
Long running thread 14 execution time: 78000500
Long running thread 13 execution time: 78000500
Long running thread 17 execution time: 124800800
Long running thread 15 execution time: 140400900
Long running thread 16 execution time: 109200700
すべてのインスタンスの実行時間を取得できませんが、MyThreadインスタンスの問題はありませんLongRunningThread。私が言ったように、私の仮説は、最初のスレッドによって実行される操作が実際に測定するには速すぎるということです。私がやろうとしていることを達成する方法はありますか?このような短時間の実行スレッドの実行時間を測定することは可能ですか?
よろしくお願いします:)