initial checkin
[org.ibex.nanogoat.git] / upstream / mips / 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 < 3) throw new Error("Runs must be >= 3");
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         }
41             
42         float times[] = new float[runs];
43         
44         for(int i=0;i<runs;i++) {
45             Runtime runtime = binary ? new Interpreter(className) : (Runtime) c.newInstance();
46             System.gc();
47             start();
48             int status = runtime.run(appArgs);
49             if(status != 0) { System.err.println(className + " failed with exit status: " + status); System.exit(1); }
50             end();
51             times[i] = diff();
52         }
53         
54         int best = 0;
55         int worst = 0;
56         for(int i=0;i<runs;i++)  {
57             System.out.println("Run " + (i+1) + ": " + times[i] + " sec");
58             if(times[i] < times[best]) best = i;
59             if(times[i] > times[worst]) worst = i;
60         }
61         float avg = 0.0f;
62         for(int i=0;i<runs;i++) if(i!=best && i!=worst) avg += times[i];
63         avg /= (runs-2);
64         
65         System.out.println("Best: " + times[best]);
66         System.out.println("Worst: " + times[worst]);
67         System.out.println("Avg of remaining " + (runs-2) + ": " + avg);
68     }
69 }