import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import java.util.LinkedList; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; import javax.swing.Timer; public class RTApp { public static int delay = 40; //milliseconds public static long first_time = 0; public static long last_time = 0; public static int cnt = 0; public static long sum_delta_time = 0; public static long enqueued_sum_dt = 0; public static long enqueued_max_dt = 0; static LinkedList q = new LinkedList(); static long ops_per_period = 0; /** * @param args * @throws IOException * @throws UnsupportedAudioFileException * @throws FileNotFoundException * @throws InterruptedException * @throws LineUnavailableException */ public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException { if (args.length == 1) delay = Integer.parseInt(args[0]); ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { long curr_time = System.currentTimeMillis(); if (cnt == 0) { first_time = curr_time; } else { long delta_time = Math.abs(curr_time - last_time - delay); q.add(delta_time); sum_delta_time += delta_time; enqueued_sum_dt += delta_time; if (q.size() > 16) { long removed_time = q.poll(); enqueued_sum_dt -= removed_time; } Iterator it = q.iterator(); long max_dt = it.next(); while (it.hasNext()) { long dt = it.next(); if (dt > max_dt) max_dt = dt; } enqueued_max_dt = max_dt; } last_time = curr_time; cnt++; long s = 1; for (long i = 0; i < ops_per_period / 4; i++) { long n = System.currentTimeMillis(); s = s * 134573463; } } }; /* Benchmarking the machine */ long s = 1; long now = System.currentTimeMillis(); while (System.currentTimeMillis() - now < delay) { s = s * 134573463; ops_per_period++; } Timer t = new Timer(delay, taskPerformer); t.start(); while (true) { Thread.sleep(1000); if (cnt > 0) System.out.println("avg dt=" + sum_delta_time / cnt + ", avg qdt=" + enqueued_sum_dt / q.size() + ", max qdt=" + enqueued_max_dt); } } }