add generic speed test
[nestedvm.git] / src / tests / GenericSpeedTest.java
1 package tests;
2
3 import java.lang.reflect.*;
4
5 class GenericSpeedTest {
6     private static long start,end;
7     private static long now() { return System.currentTimeMillis(); }
8     private static void start()  { start = now(); }
9     private static void end() { end = now(); }
10     private static float diff() { return ((float)(end-start))/1000; }
11     
12     public static void main(String[] args) throws Exception {
13         float d;
14         
15         if(args.length < 2) { System.err.println("Usage: GenericSpeedTest runs classname args"); System.exit(1); }
16         int runs = Integer.parseInt(args[0]);
17         String className = args[1];
18         if(runs < 5) throw new Error("Runs must be >= 5");
19         String[] appArgs = new String[args.length-2];
20         for(int i=2;i<args.length;i++) appArgs[i-2] = args[i];
21         
22         Class c = Class.forName(className);
23         Method m = c.getMethod("main",new Class[]{Class.forName("[Ljava.lang.String;")});
24         
25         float times[] = new float[runs];
26         Object[] mainArgs = new Object[] { appArgs };
27         for(int i=0;i<runs;i++) {
28             System.gc();
29             start();
30             m.invoke(null,mainArgs);
31             end();
32             times[i] = diff();
33             System.err.println("Run " + (i+1) + ": " + times[i] + " sec");
34         }
35         
36         java.util.Arrays.sort(times);
37         
38         System.out.println("Best: " + times[0]);
39         System.out.println("Worst: " + times[times.length-1]);
40         float sum = 0.0f;
41         for(int i=2;i<times.length-2;i++)
42             sum += times[i];
43         float avg = sum / (times.length-4);
44         System.out.println("Avg of middle " + (times.length-4) + ": " + avg);
45     }
46 }