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