View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.test.performance;
20  
21  import java.io.PrintWriter;
22  
23  import jgroup.util.log.Eventlogger;
24  
25  /**
26   *  This class is used by both Jgroup and standard RMI test to perform
27   *  repeated invocations and take mean times.  A single test consists of
28   *  NUM_CALLS invocations of a method; the time needed to perform an
29   *  invocation is the average computed from the total time.  Each test
30   *  is repeated NUM_ROUNDS times.
31   *
32   *  @author Alberto Montresor
33   *  @author Hein Meling
34   *  @since Jgroup 1.1
35   */
36  class ClientTest
37  {
38  
39    ////////////////////////////////////////////////////////////////////////////////////////////
40    // Constants
41    ////////////////////////////////////////////////////////////////////////////////////////////
42  
43    /** Max number of */
44    private static final int PRE_NUM_ROUNDS      = 5000;
45    private static final int POST_NUM_ROUNDS     = 5000;
46    private static final int NUM_ROUNDS          = 100000;
47    private static final int NUM_CALLS_PER_ROUND = 1;
48    private static final int MIN_SIZE = 1000;
49    private static final int MAX_SIZE = 1000;
50    private static final int INC_SIZE = 1000;
51  
52  
53    /**
54     *  Anycast/unicast invocations, with byte arrays of variable size
55     *  as arguments and return values.
56     */
57    private static void pre_test2(SpeedTest test)
58      throws Exception
59    {
60      /* Create a buffer of size MIN_SIZE */
61      byte[] buffer = new byte[MIN_SIZE];
62      /* Run some iterations to initialize the experiment */
63      for (int i=0; i < PRE_NUM_ROUNDS; i++) {
64        // Perform test 
65        for (int j=1; j <= NUM_CALLS_PER_ROUND; j++) {
66          test.test(buffer);
67        }
68      }
69    }
70  
71    /**
72     *  Post experiment run to allow other clients to also complete the
73     *  main part of the experiment under identical load conditions.
74     */
75    private static void post_test2(SpeedTest test)
76      throws Exception
77    {
78      /* Create a buffer of size MIN_SIZE */
79      byte[] buffer = new byte[MIN_SIZE];
80      /* Run some iterations to finalize the experiment */
81      for (int i=0; i < POST_NUM_ROUNDS; i++) {
82        // Perform test 
83        for (int j=1; j <= NUM_CALLS_PER_ROUND; j++) {
84          test.test(buffer);
85        }
86      }
87    }
88  
89    
90    ////////////////////////////////////////////////////////////////////////////////////////////
91    // Methods
92    ////////////////////////////////////////////////////////////////////////////////////////////
93  
94    /**
95     * Anycast/unicast invocations, without arguments/return values
96     */
97    static void test1(SpeedTest test, PrintWriter out)
98      throws Exception
99    {
100     /* Min, max and average time; initialization */
101     long min = Long.MAX_VALUE, max = 0, avg = 0, start = 0, time = 0;
102 
103     System.out.println("\n\n-- Test1: test() -----------------------------------------------------");
104     out.println("\n\n-- Test1: test() -----------------------------------------------------");
105     System.out.println("ROUND NO ; NUM OF CALLS ; TOTAL TIME ; AVG TIME PR CALL");
106     out.println("ROUND NO ; NUM OF CALLS ; TOTAL TIME ; AVG TIME PR CALL");
107 
108     for (int i=0; i < NUM_ROUNDS; i++) {
109 
110       // Perform test and measure times
111       start = System.currentTimeMillis();
112       for (int j=1; j <= NUM_CALLS_PER_ROUND; j++) {
113         System.out.print("+");
114         if (j % 20 == 0) {
115           System.out.print("+");
116         }
117         test.test();
118       }
119       System.out.println("");
120 
121       // Compute statics
122       time = System.currentTimeMillis() - start;
123       if (time < min)
124         min = time;
125       if (time > max)
126         max = time;
127       avg += time;
128 
129       // Print statistics
130       System.out.println(i + " ; " + NUM_CALLS_PER_ROUND + " ; " + time + " ; " + ((float) time / NUM_CALLS_PER_ROUND));
131       out.println(i + " ; " + NUM_CALLS_PER_ROUND + " ; " + time + " ; " + ((float) time / NUM_CALLS_PER_ROUND));
132     }
133     System.out.println("MIN TIME ; MAX TIME ; AVG TIME");
134     System.out.print(((float) min/NUM_CALLS_PER_ROUND) + " ; ");
135     System.out.print(((float) max/NUM_CALLS_PER_ROUND) + " ; ");
136     System.out.println(((float) avg/(NUM_CALLS_PER_ROUND*NUM_ROUNDS)));
137     out.println("MIN TIME ; MAX TIME ; AVG TIME");
138     out.print(((float) min/NUM_CALLS_PER_ROUND) + " ; ");
139     out.print(((float) max/NUM_CALLS_PER_ROUND) + " ; ");
140     out.println(((float) avg/(NUM_CALLS_PER_ROUND*NUM_ROUNDS)));
141     System.out.println("----------------------------------------------------------------------");
142     out.println("----------------------------------------------------------------------");
143   }
144 
145   /**
146    *  Anycast/unicast invocations, with byte arrays of variable size
147    *  as arguments and return values.
148    */
149   static void test2(SpeedTest test, PrintWriter out)
150     throws Exception
151   {
152     pre_test2(test);
153     out.println("\n-- byte[] test(byte[]) ----------------------------------------");
154     /* Start and delay time values */
155     long start = 0, delay = 0;
156     /* Create a buffer */
157     byte[] buffer;
158     for (int k = MIN_SIZE; k <= MAX_SIZE; k += INC_SIZE) {
159       /* Create a buffer of size k */
160       buffer = new byte[k];
161       for (int i=0; i < NUM_ROUNDS; i++) {
162         // Perform test and measure times
163 //        start = System.currentTimeMillis();
164         //Debug.logMem("start");
165         Eventlogger.logEvent("start");
166         for (int j=1; j <= NUM_CALLS_PER_ROUND; j++) {
167           test.test(buffer);
168         }
169         //Debug.logMem("stop");
170         Eventlogger.logEvent("stop");
171 //        delay = System.currentTimeMillis() - start;
172 //        float callTime = ((float) delay / NUM_CALLS_PER_ROUND);
173 //        out.println("k=" + k + "; " + callTime);
174       }
175       //Debug.flushMem();
176       Eventlogger.flushMem();
177     }
178     out.println("----------------------------------------------------------------------");
179     post_test2(test);
180   }
181 
182 
183   /**
184    *  Anycast/unicast invocations, with string of variable size
185    *  as arguments and return values.
186    */
187   static void test3(SpeedTest test, PrintWriter out)
188     throws Exception
189   {
190     /* Min, max and average time; initialization */
191     long min = Long.MAX_VALUE, max = 0, avg = 0, start = 0, time = 0;
192   
193     System.out.println("\n\n-- Test3: String test(String) ----------------------------------------");
194     out.println("\n\n-- Test3: String test(String) ----------------------------------------");
195 
196 //    for (int k = MIN_STR_SIZE; k <= MAX_STR_SIZE; k += INC_STR_SIZE) {
197     int k = 50;
198       /* Create a string of size k */
199       StringBuilder buf = new StringBuilder();
200       for (int i = 0; i < k; i++) {
201         buf.append(i);
202       }
203       String str = buf.toString();
204 
205       /* Reset the min, max and avg values for the next array size */
206       min = Long.MAX_VALUE;  max = 0;  avg = 0;
207 
208       for (int i=0; i < NUM_ROUNDS; i++) {
209 
210         // Perform test and measure times
211         start = System.currentTimeMillis();
212         for (int j=1; j <= NUM_CALLS_PER_ROUND; j++) {
213           String reply = test.test(str);
214 //          System.out.println(reply.substring(0, 30));
215         }
216 
217         // Compute statics
218         time = System.currentTimeMillis() - start;
219         if (time < min)
220           min = time;
221         if (time > max)
222           max = time;
223         avg += time;
224   
225         // Print statistics
226         System.out.println(i + " ; " + NUM_CALLS_PER_ROUND + " ; " + time + " ; " + ((float) time / NUM_CALLS_PER_ROUND));
227 //        out.println(i + " ; " + NUM_CALLS + " ; " + time + " ; " + ((float) time / NUM_CALLS));
228         out.println(((float) time / NUM_CALLS_PER_ROUND));
229       }
230       System.out.println("SIZE ; MIN TIME ; MAX TIME ; AVG TIME");
231       System.out.print(k + " ; ");
232       System.out.print(((float) min/NUM_CALLS_PER_ROUND) + " ; ");
233       System.out.print(((float) max/NUM_CALLS_PER_ROUND) + " ; ");
234       System.out.println(((float) avg/(NUM_CALLS_PER_ROUND*NUM_ROUNDS)));
235 /*
236       out.println("SIZE ; MIN TIME ; MAX TIME ; AVG TIME");
237       out.print(k + " ; ");
238       out.print(((float) min/NUM_CALLS) + " ; ");
239       out.print(((float) max/NUM_CALLS) + " ; ");
240       out.println(((float) avg/(NUM_CALLS*NUM_ROUNDS)));
241       */
242 //    }
243     System.out.println("----------------------------------------------------------------------");
244     out.println("----------------------------------------------------------------------");
245   }
246 
247   /**
248    * Multicast invocations, without arguments/return values
249    */
250   static void mtest1(SpeedTest test, PrintWriter out)
251     throws Exception
252   {
253     /* Min, max and average time; initialization */
254     long min = Long.MAX_VALUE, max = 0, avg = 0, start = 0, time = 0;
255 
256     System.out.println("\n\n-- Test3: mtest() ----------------------------------------------------");
257     out.println("\n\n-- Test3: mtest() -----------------------------------------------------");
258     System.out.println("ROUND NO ; NUM OF CALLS ; TOTAL TIME ; AVG TIME PR CALL");
259     out.println("ROUND NO ; NUM OF CALLS ; TOTAL TIME ; AVG TIME PR CALL");
260 
261     for (int i=0; i < NUM_ROUNDS; i++) {
262 
263       // Perform test and measure times
264       start = System.currentTimeMillis();
265       for (int j=1; j <= NUM_CALLS_PER_ROUND; j++) {
266         System.out.print("+");
267         if (j % 20 == 0) {
268           System.out.print("+");
269         }
270         test.mtest();
271       }
272       System.out.println("");
273 
274       // Compute statics
275       time = System.currentTimeMillis() - start;
276       if (time < min)
277         min = time;
278       if (time > max)
279         max = time;
280       avg += time;
281 
282       // Print statistics
283       System.out.println(i + " ; " + NUM_CALLS_PER_ROUND + " ; " + time + " ; " + ((float) time / NUM_CALLS_PER_ROUND));
284       out.println(i + " ; " + NUM_CALLS_PER_ROUND + " ; " + time + " ; " + ((float) time / NUM_CALLS_PER_ROUND));
285     }
286     System.out.println("MIN TIME ; MAX TIME ; AVG TIME");
287     System.out.print(((float) min/NUM_CALLS_PER_ROUND) + " ; ");
288     System.out.print(((float) max/NUM_CALLS_PER_ROUND) + " ; ");
289     System.out.println(((float) avg/(NUM_CALLS_PER_ROUND*NUM_ROUNDS)));
290     out.println("MIN TIME ; MAX TIME ; AVG TIME");
291     out.print(((float) min/NUM_CALLS_PER_ROUND) + " ; ");
292     out.print(((float) max/NUM_CALLS_PER_ROUND) + " ; ");
293     out.println(((float) avg/(NUM_CALLS_PER_ROUND*NUM_ROUNDS)));
294     System.out.println("----------------------------------------------------------------------");
295     out.println("----------------------------------------------------------------------");
296   }
297 
298   /**
299    *  Multicast invocations, with byte arrays of variable size
300    *  as arguments and return values.
301    */
302   static void mtest2(SpeedTest test, PrintWriter out)
303     throws Exception
304   {
305 //    pre_test2(test);
306     System.out.println("\n-- byte[] mtest(byte[]) ----------------------------------------");
307     /* Start and stop time values */
308     long start = 0, stop = 0;
309     /* Create a buffer */
310     byte[] buffer;
311     for (int k = MIN_SIZE; k <= MAX_SIZE; k += INC_SIZE) {
312       /* Create a buffer of size k */
313       buffer = new byte[k];
314       for (int i=0; i < NUM_ROUNDS; i++) {
315         // Perform test and measure times
316         start = System.currentTimeMillis();
317         for (int j=1; j <= NUM_CALLS_PER_ROUND; j++) {
318 //          Thread.sleep(50);
319           test.mtest(buffer);
320         }
321         stop = System.currentTimeMillis() - start;
322         float callTime = ((float) stop / NUM_CALLS_PER_ROUND);
323         System.out.println("i=" + i*NUM_CALLS_PER_ROUND + ", k=" + k + "; " + callTime);
324       }
325     }
326     System.out.println("----------------------------------------------------------------------");
327 //    post_test2(test);
328   }
329 
330 
331   /**
332    *  Multicast invocations, with byte arrays of variable size
333    *  as arguments and return values.
334    */
335   static void xmtest2(SpeedTest test, PrintWriter out)
336     throws Exception
337   {
338     /* Min, max and average time; initialization */
339     long min = Long.MAX_VALUE, max = 0, avg = 0, start = 0, time = 0;
340 
341     System.out.println("\n\n-- Test4: byte[] mtest(byte[]) ---------------------------------------");
342     System.out.println("SIZE ; MIN TIME ; MAX TIME ; AVG TIME");
343     out.println("\n\n-- Test4: byte[] mtest(byte[]) ----------------------------------------");
344     out.println("SIZE ; MIN TIME ; MAX TIME ; AVG TIME");
345 
346     for (int k=0; k <= MAX_SIZE; k += INC_SIZE) {
347 //    for (int k=INC_SIZE; k <= MAX_SIZE; k += INC_SIZE) {
348 System.out.print(" k=" + k);
349       byte[] buffer = new byte[k];
350       min = Long.MAX_VALUE;  max = 0;  avg = 0;
351 
352       for (int i=0; i < NUM_ROUNDS; i++) {
353 
354         // Perform test and measure times
355         start = System.currentTimeMillis();
356         for (int j=1; j <= NUM_CALLS_PER_ROUND; j++) {
357           System.out.println(j);
358           if (j % 20 == 0) {
359             System.out.println("+");
360           }
361           Thread.sleep(500);
362           test.mtest(buffer);
363         }
364         System.out.println("");
365 
366         // Compute statics
367         time = System.currentTimeMillis() - start;
368         if (time < min)
369           min = time;
370         if (time > max)
371           max = time;
372         avg += time;
373 
374         // Print statistics
375         System.out.println(i + " ; " + NUM_CALLS_PER_ROUND + " ; " + time + " ; " + ((float) time / NUM_CALLS_PER_ROUND));
376         out.println(i + " ; " + NUM_CALLS_PER_ROUND + " ; " + time + " ; " + ((float) time / NUM_CALLS_PER_ROUND));
377       }
378       System.out.print(k + " ; ");
379       System.out.print(((float) min/NUM_CALLS_PER_ROUND) + " ; ");
380       System.out.print(((float) max/NUM_CALLS_PER_ROUND) + " ; ");
381       System.out.println(((float) avg/(NUM_CALLS_PER_ROUND*NUM_ROUNDS)));
382       out.print(k + " ; ");
383       out.print(((float) min/NUM_CALLS_PER_ROUND) + " ; ");
384       out.print(((float) max/NUM_CALLS_PER_ROUND) + " ; ");
385       out.println(((float) avg/(NUM_CALLS_PER_ROUND*NUM_ROUNDS)));
386     }
387     System.out.println("----------------------------------------------------------------------");
388     out.println("----------------------------------------------------------------------");
389   }
390 
391   /**
392    *  Multicast invocations, with byte arrays of variable size
393    *  as arguments and return values.
394    */
395   static void mtest3(SpeedTest test, PrintWriter out)
396     throws Exception
397   {
398     System.out.println("\n\n-- Test5: byte[] mtest(byte[]) ---------------------------------------");
399     System.out.println("JIT Compiling and optimization phase:");
400     long start = 0;
401 
402     byte[] buffer = new byte[10000];
403     int calls = NUM_ROUNDS*NUM_CALLS_PER_ROUND;
404     long[] timeVals = new long[calls];
405     for (int j=0; j < calls; j++) {
406       start = System.nanoTime();
407       test.mtest(buffer);
408       timeVals[j] = System.nanoTime() - start;
409     }
410     for (int j=0; j < calls; j++) {
411       System.out.println(timeVals[j]);
412     }
413 
414     System.out.println("\n\nStandard measurements:");
415     for (int k=0; k <= MAX_SIZE; k += INC_SIZE) {
416       System.out.println("-- k=" + k + " ------------------------------------------------");
417       buffer = new byte[k];
418       for (int j=0; j < calls; j++) {
419         start = System.nanoTime();
420         test.mtest(buffer);
421         timeVals[j] = System.nanoTime() - start;
422       }
423       for (int j=0; j < calls; j++) {
424         System.out.println(timeVals[j]);
425       }
426     }
427   }
428 
429   /**
430    *  Multicast invocations, with byte arrays of variable size
431    *  as arguments and return values.
432    */
433   static void mtest4(SpeedTest test, PrintWriter out)
434     throws Exception
435   {
436     System.out.println("\n\n-- Test6: void vmtest(byte[]) ---------------------------------------");
437     System.out.println("JIT Compiling and optimization phase:");
438 
439     byte[] buffer = new byte[10000];
440     test.vmtest(buffer);
441     System.out.println("Completed void vmtest(byte[])");
442   }
443 
444 } // END ClientTest