more xwt -> ibex cleanup
[nestedvm.git] / src / tests / SpeedTest.java
1 package tests;
2
3 import org.ibex.nestedvm.Runtime;
4
5 class SpeedTest {
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: SpeedTest {classname|mips binary} number_of_runs args"); System.exit(1); }
16         String className = args[0];
17         int runs = Integer.parseInt(args[1]);
18         if(runs < 5) throw new Error("Runs must be >= 5");
19         String[] appArgs = new String[args.length-1];
20         appArgs[0] = className;
21         for(int i=2;i<args.length;i++) appArgs[i-1] = args[i];
22         
23         Class c = null;
24         boolean binary = className.endsWith(".mips");
25         if(!binary) {
26             start();
27             c = Class.forName(className);
28             end();
29             d = diff();
30             System.out.println("Class.forName() took " + d + "sec");
31             
32             start();
33             c.newInstance();
34             end();
35             d = diff();
36             System.out.println("c.newInstance() took " + d + "sec");
37             
38             if(!Runtime.class.isAssignableFrom(c)) { System.err.println(className + " isn't a MIPS compiled class"); System.exit(1); }
39         } else {
40             throw new Error("Interpreter not supported in speedtest");
41         }
42             
43         float times[] = new float[runs];
44         
45         for(int i=0;i<runs;i++) {
46             //Runtime runtime = binary ? new Interpreter(className) : (Runtime) c.newInstance();
47             Runtime runtime = (Runtime) c.newInstance();
48             System.gc();
49             start();
50             int status = runtime.run(appArgs);
51             if(status != 0) { System.err.println(className + " failed with exit status: " + status); System.exit(1); }
52             end();
53             times[i] = diff();
54             System.err.println("Run " + (i+1) + ": " + times[i] + " sec");
55         }
56         
57         java.util.Arrays.sort(times);
58         
59         System.out.println("Best: " + times[0]);
60         System.out.println("Worst: " + times[times.length-1]);
61         float sum = 0.0f;
62         for(int i=2;i<times.length-2;i++)
63             sum += times[i];
64         float avg = sum / (times.length-4);
65         System.out.println("Avg of middle " + (times.length-4) + ": " + avg);
66     }
67 }