import java.util.Arrays; import java.util.Date; import java.util.Random; public class JvmPauseLatency { private static final int WARMUP = 20000; private static final int EXTRA = 15; private static final long PAUSE = 5 * 1000000000L; // in nanos private final Random rand = new Random(); private int count; private double calculation; private final long[] results = new long[WARMUP + EXTRA]; private long interval = 1; // in nanos private long busyPause(long pauseInNanos) { long until = System.nanoTime() + pauseInNanos; while(System.nanoTime() < until); return until; } public void run() { // Warm up: busyPause(1); busyPause(PAUSE); long testDuration = ((WARMUP * 1) + (EXTRA * PAUSE)) / 1000000000L; System.out.println(new Date() +" => Please wait " + testDuration + " seconds for the results..."); while(count < results.length) { if ((count & 7) > 0) interval = 0; // No lag after first hiccup: long until = System.nanoTime() + interval; while(System.nanoTime() < until); double x = until; // Yes lag hiccup after hiccup: // double x = busyPause(interval); long latency = System.nanoTime(); latency = System.nanoTime() - latency; results[count++] = latency; interval = (count / WARMUP * (PAUSE - 1)) + 1; // it will change to PAUSE when it reaches WARMUP } // now print the last (EXTRA * 2) results so you can compare before and after the pause change (from 1 to PAUSE) System.out.println(new Date() + " => Calculation: " + calculation); System.out.println("Results:"); long[] array = Arrays.copyOfRange(results, results.length - EXTRA * 2, results.length); for(long t: array) System.out.println(t); } public static void main(String[] args) { new JvmPauseLatency().run(); } }