licensing update to APSL 2.0
[nestedvm.git] / src / tests / GenericSpeedTest.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source 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 java.lang.reflect.*;
8
9 class GenericSpeedTest {
10     private static long start,end;
11     private static long now() { return System.currentTimeMillis(); }
12     private static void start()  { start = now(); }
13     private static void end() { end = now(); }
14     private static float diff() { return ((float)(end-start))/1000; }
15     
16     public static void main(String[] args) throws Exception {
17         float d;
18         
19         if(args.length < 2) { System.err.println("Usage: GenericSpeedTest runs classname args"); System.exit(1); }
20         int runs = Integer.parseInt(args[0]);
21         String className = args[1];
22         if(runs < 5) throw new Error("Runs must be >= 5");
23         String[] appArgs = new String[args.length-2];
24         for(int i=2;i<args.length;i++) appArgs[i-2] = args[i];
25         
26         Class c = Class.forName(className);
27         Method m = c.getMethod("main",new Class[]{Class.forName("[Ljava.lang.String;")});
28         
29         float times[] = new float[runs];
30         Object[] mainArgs = new Object[] { appArgs };
31         for(int i=0;i<runs;i++) {
32             System.gc();
33             start();
34             m.invoke(null,mainArgs);
35             end();
36             times[i] = diff();
37             System.err.println("Run " + (i+1) + ": " + times[i] + " sec");
38         }
39         
40         java.util.Arrays.sort(times);
41         
42         System.out.println("Best: " + times[0]);
43         System.out.println("Worst: " + times[times.length-1]);
44         float sum = 0.0f;
45         for(int i=2;i<times.length-2;i++)
46             sum += times[i];
47         float avg = sum / (times.length-4);
48         System.out.println("Avg of middle " + (times.length-4) + ": " + avg);
49     }
50 }