1 package org.e2etrace.timer; 2 3 /* 4 * Copyright 2006 Gunther Popp 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 /** 20 * Utility class to determine the e2etrace timer configuration and accuracy. 21 * <p> 22 * 23 * The source code of this utility class is based on the article "My kingdom for 24 * a good timer!" by Vladimir Roubtsov on JavaWorld (see link below). 25 * 26 * @author Gunther Popp 27 * @see <a 28 * href="http://www.javaworld.com/javaworld/javaqa/2003-01/01-qa-0110-timing.html">Article 29 * "My kingdom for a good timer!"</a> 30 * 31 */ 32 public class PrintTimerAccuracy { 33 34 /** 35 * Main method. 36 * 37 * @param args command line args 38 */ 39 public static void main(String[] args) { 40 ITimerFactory tf = new DefaultTimerFactory(); 41 ITimer timer = tf.newInstance(); 42 long time, time_prev; 43 44 System.out.println("e2etrace - PrintTimerAccuracy\n"); 45 System.out.println("Used timer type: " + timer.getClass().getName() + "\n"); 46 47 // JIT/hotspot warmup: 48 timer.start(); 49 for (int r = 0; r < 3000; ++r) { 50 timer.measure(); 51 } 52 53 timer.start(); 54 55 for (int i = 0; i < 5; ++i) { 56 time = timer.measure(); 57 time_prev = time; 58 while (time == time_prev) 59 time = timer.measure(); 60 61 System.out.println("accuracy = " + (time - time_prev) + " ms"); 62 } 63 } 64 }