35941c112877d9cdebaca005c829bbc97f43690f
[nestedvm.git] / src / tests / SpeedTest.java
1 package tests;
2
3 import org.ibex.nestedvm.Runtime;
4
5 //import java.io.*;
6 //import java.util.*;
7
8 class SpeedTest {
9     private static long start,end;
10     private static long now() { return System.currentTimeMillis(); }
11     private static void start()  { start = now(); }
12     private static void end() { end = now(); }
13     private static float diff() { return ((float)(end-start))/1000; }
14     
15     /*private static InputStream is = new InputStream() {
16         int left = 100*1024*1024;
17         int c = 0;
18         public int read() { if(left==0) return -1; left--; return (c++)&0xff; }
19         public int read(byte[] buf, int pos,int len) {
20             len = Math.min(left,len);
21             Arrays.fill(buf,pos,len,(byte)c++);
22             left -= len;
23             return len;
24         }
25         public void close() { left =  100*1024*1024; }
26     };
27     
28     private static OutputStream os = new OutputStream() {
29         public void write(int c) { }
30         public void write(byte[] buf, int pos, int len) { }
31     };*/
32     
33     public static void main(String[] args) throws Exception {
34         float d;
35         
36         if(args.length < 2) { System.err.println("Usage: SpeedTest {classname|mips binary} number_of_runs args"); System.exit(1); }
37         String className = args[0];
38         int runs = Integer.parseInt(args[1]);
39         if(runs < 5) throw new Error("Runs must be >= 5");
40         String[] appArgs = new String[args.length-1];
41         appArgs[0] = className;
42         for(int i=2;i<args.length;i++) appArgs[i-1] = args[i];
43         
44         Class c = null;
45         boolean binary = className.endsWith(".mips");
46         if(!binary) {
47             start();
48             c = Class.forName(className);
49             end();
50             d = diff();
51             System.out.println("Class.forName() took " + d + "sec");
52             
53             start();
54             c.newInstance();
55             end();
56             d = diff();
57             System.out.println("c.newInstance() took " + d + "sec");
58             
59             if(!Runtime.class.isAssignableFrom(c)) { System.err.println(className + " isn't a MIPS compiled class"); System.exit(1); }
60         } else {
61             throw new Error("Interpreter not supported in speedtest");
62         }
63             
64         float times[] = new float[runs];
65         
66         for(int i=0;i<runs;i++) {
67             //Runtime runtime = binary ? new Interpreter(className) : (Runtime) c.newInstance();
68             Runtime runtime = (Runtime) c.newInstance();
69             /*runtime.closeFD(0);
70             runtime.closeFD(1);
71             runtime.addFD(new Runtime.InputStreamFD(is));
72             runtime.addFD(new Runtime.OutputStreamFD(os));*/
73             System.gc();
74             start();
75             int status = runtime.run(appArgs);
76             if(status != 0) { System.err.println(className + " failed with exit status: " + status); System.exit(1); }
77             end();
78             //is.close();
79             times[i] = diff();
80             System.err.println("Run " + (i+1) + ": " + times[i] + " sec");
81         }
82         
83         java.util.Arrays.sort(times);
84         
85         System.out.println("Best: " + times[0]);
86         System.out.println("Worst: " + times[times.length-1]);
87         float sum = 0.0f;
88         for(int i=2;i<times.length-2;i++)
89             sum += times[i];
90         float avg = sum / (times.length-4);
91         System.out.println("Avg of middle " + (times.length-4) + ": " + avg);
92     }
93 }