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