KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > samples > bench > Bench


1 package org.objectweb.jac.samples.bench;
2
3 import java.lang.reflect.Method JavaDoc;
4
5 public class Bench {
6     /**
7      * <p>Simple benchmark suite to mesure impact of wrappers</p>
8      * <p>Usage: <code>java org.objectweb.jac.samples.bench.Bench &lt;n&gt;</code>
9      *
10      * <p>This sample is a benchmark to test the impact of wrappers on
11      * performances. There are 8 different tests for each iteration,
12      * testing different calls.</p>
13      */

14     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
15         if (args.length<1 || args.length>2) {
16             System.err.println("Wrong number of arguments");
17             printUsage();
18             System.exit(1);
19         }
20         Bench b = new Bench(
21             Integer.parseInt(args[0]),
22             args.length > 1 ? args[1] : "d");
23       
24         try {
25             m1=Bench.class.getMethod("m1",new Class JavaDoc[]{});
26             m2=Bench.class.getMethod("m2",new Class JavaDoc[]{int.class});
27             m3=Bench.class.getMethod("m3",new Class JavaDoc[]{int.class,int.class});
28             m4=Bench.class.getMethod("m4",new Class JavaDoc[]{});
29             m5=Bench.class.getMethod("m5",new Class JavaDoc[]{int.class});
30             m6=Bench.class.getMethod("m6",new Class JavaDoc[]{int.class,int.class});
31             m7=Bench.class.getMethod("m7",new Class JavaDoc[]{Object JavaDoc.class});
32             m8=Bench.class.getMethod("m8",new Class JavaDoc[]{Object JavaDoc.class,Object JavaDoc.class});
33         } catch(Exception JavaDoc e) {
34             e.printStackTrace();
35         }
36         // Let the VM optimize
37
System.out.print("Warming up ");
38         b.doBench(); System.out.print(".");
39         b.doBench(); System.out.print(".");
40         b.doBench(); System.out.print(".");
41         b.doBench(); System.out.println(".");
42         //Thread.currentThread().sleep(5000);
43

44         System.out.println("Starting bench");
45         long total = 0;
46         int num_runs = 5;
47         for (int i=0; i<num_runs; i++) {
48             long start = System.currentTimeMillis();
49             b.doBench();
50             long end = System.currentTimeMillis();
51             System.out.print(i+": "+(end-start)+"ms; ");
52             total += end-start;
53         }
54         System.out.println(" average="+(total/num_runs));
55     }
56
57     static void printUsage() {
58         System.out.println("Usage: java org.objectweb.jac.samples.bench.Bench <nb iterations> [s|r]");
59         System.out.println(" <nb iterations>: number of iterations");
60         System.out.println(" [r|s]: r(reflective calls), d(direct calls)");
61     }
62
63     boolean rCalls;
64
65     void doBench() {
66         if(rCalls) {
67             // reflexive calls
68
bench1();
69             bench2();
70             bench3();
71             bench4();
72             bench5();
73             bench6();
74             bench7();
75             bench8();
76         } else {
77             // direct calls
78
sbench1();
79             sbench2();
80             sbench3();
81             sbench4();
82             sbench5();
83             sbench6();
84             sbench7();
85             sbench8();
86         }
87     }
88
89     static Method JavaDoc m1;
90     static Method JavaDoc m2;
91     static Method JavaDoc m3;
92     static Method JavaDoc m4;
93     static Method JavaDoc m5;
94     static Method JavaDoc m6;
95     static Method JavaDoc m7;
96     static Method JavaDoc m8;
97
98     public int n;
99     String JavaDoc s;
100
101     /**
102     * @param n numbers of method calls for the benchs.
103     * @param reflectiveCalls argument that indicates if the call must
104     * be reflexive ("r") or direct ("d")
105     */

106     public Bench(int n,String JavaDoc reflectiveCalls) {
107         this.n = n;
108         if(reflectiveCalls.equals("r")) {
109             this.rCalls=true;
110         } else {
111             this.rCalls=false;
112         }
113     }
114
115     /**
116     * Method with no argument returning void
117     */

118     void bench1() {
119         for(int i=0; i<n; i++) {
120             try {
121                 m1.invoke(this,new Object JavaDoc[]{});
122             } catch(Exception JavaDoc e) {
123                 e.printStackTrace();
124             }
125         }
126     }
127
128     void sbench1() {
129         for(int i=0; i<n; i++) {
130             m1();
131         }
132     }
133
134     public void m1() {
135         int i=0;
136     }
137
138
139     /**
140     * Method with 1 argument returning void
141     */

142     void bench2() {
143         for(int i=0; i<n; i++) {
144             try {
145                 m2.invoke(this,new Object JavaDoc[]{new Integer JavaDoc(0)});
146             } catch(Exception JavaDoc e) {
147                 e.printStackTrace();
148             }
149         }
150     }
151
152     void sbench2() {
153         //System.out.print("bench 2: ");
154
for(int i=0; i<n; i++) {
155             m2(0);
156         }
157     }
158
159     public void m2(int a) {
160         int i=0;
161     }
162
163     /**
164     * Method with 2 argument returning void
165     */

166     void bench3() {
167         for(int i=0; i<n; i++) {
168             try {
169                 m3.invoke(this,new Object JavaDoc[]{new Integer JavaDoc(0),new Integer JavaDoc(0)});
170             } catch(Exception JavaDoc e) {
171                 e.printStackTrace();
172             }
173         }
174     }
175
176     void sbench3() {
177         for(int i=0; i<n; i++) {
178             m3(0,0);
179         }
180     }
181
182     public void m3(int a, int b) {
183         int i=0;
184     }
185
186     /**
187     * Method with no argument returning an int
188     */

189     void bench4() {
190         for(int i=0; i<n; i++) {
191             try {
192                 m4.invoke(this,new Object JavaDoc[]{});
193             } catch(Exception JavaDoc e) {
194                 e.printStackTrace();
195             }
196         }
197     }
198
199     void sbench4() {
200         for(int i=0; i<n; i++) {
201             m4();
202         }
203     }
204
205     public int m4() {
206         return 0;
207     }
208
209     /**
210     * Method with 1 argument returning an int
211     */

212     void bench5() {
213         for(int i=0; i<n; i++) {
214             try {
215                 m5.invoke(this,new Object JavaDoc[]{new Integer JavaDoc(0)});
216             } catch(Exception JavaDoc e) {
217                 e.printStackTrace();
218             }
219         }
220     }
221
222     void sbench5() {
223         for(int i=0; i<n; i++) {
224             m5(0);
225         }
226     }
227
228     public int m5(int a) {
229         return 0;
230     }
231
232     /**
233     * Method with 1 argument returning an int
234     */

235     void bench6() {
236         for(int i=0; i<n; i++) {
237             try {
238                 m6.invoke(this,new Object JavaDoc[]{new Integer JavaDoc(0),new Integer JavaDoc(0)});
239             } catch(Exception JavaDoc e) {
240                 e.printStackTrace();
241             }
242         }
243     }
244
245     void sbench6() {
246         for(int i=0; i<n; i++) {
247             m6(0,0);
248         }
249     }
250
251     public int m6(int a, int b) {
252         return 0;
253     }
254
255     /**
256     * Method with 1 argument returning an int
257     */

258     void bench7() {
259         for(int i=0; i<n; i++) {
260             try {
261                 m7.invoke(this,new Object JavaDoc[]{null});
262             } catch(Exception JavaDoc e) {
263                 e.printStackTrace();
264             }
265         }
266     }
267
268     void sbench7() {
269         for(int i=0; i<n; i++) {
270             m7(null);
271         }
272     }
273
274     public void m7(Object JavaDoc a) {
275         return;
276     }
277
278
279     /**
280     * Method with 1 argument returning an int
281     */

282     void bench8() {
283         for(int i=0; i<n; i++) {
284             try {
285                 m8.invoke(this,new Object JavaDoc[]{null,null});
286             } catch(Exception JavaDoc e) {
287                 e.printStackTrace();
288             }
289         }
290     }
291
292     void sbench8() {
293         for(int i=0; i<n; i++) {
294             m8(null,null);
295         }
296     }
297
298     public void m8(Object JavaDoc a, Object JavaDoc b) {
299         return;
300     }
301
302 }
303
Popular Tags