title: Measure java short time running thread execution time
body: <p>I'm currently working on some sort of database benchmark application. Basically, what I'm trying to do is to simulate using threads a certain number of clients that all repeat the same operation (example: a read operation) against the database during a certain period of time.<br>
  During this time, I want, in each thread, to measure the average delay for getting an answer from the database.</p>
  
  <p>My first choice was to rely on ThreadMXBean's getThreadCpuTime() method (<a href="http://docs.oracle.com/javase/7/docs/api/java/lang/management/ThreadMXBean.html" rel="noreferrer">http://docs.oracle.com/javase/7/docs/api/java/lang/management/ThreadMXBean.html</a>) but the point is that the operation is done too quickly to be measured (getThreadCpuTime() before the operation is equal to getThreadCpuTime() after the operation).</p>
  
  <p>I made a little experiment to understand and illustrate the problem:</p>
  
  <pre><code>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 &lt; 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 &lt; 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 &lt; 10; ++i) {
          new LongRunningThread(i*1000000).start();
      }
  
      for (int i = 1; i &lt; 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
  </code></pre>
  
  <p>I cannot get the execution time for all <code>MyThread</code> instances but no problem for <code>LongRunningThread</code> instances. Like I said, my hypothesis is that the operation done by the first threads happen too fast to be actually measured. 
  Is there any way to achieve what I'm trying to do? Is it possible to measure the execution time for such short time running threads?</p>
  
  <p>Thanks in advance for you help :)</p>