KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TimeFills


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 import java.util.*;
24
25 import com.sosnoski.util.array.*;
26
27 /**
28  * Benchmark to compare the performance of various collections in a test using
29  * simple primitive values. The basic test is repeatedly constructing and
30  * discarding a collection of <code>int</code> values. Several variations of
31  * the test are performed, each using a different collection variation.
32  *
33  * @author Dennis M. Sosnoski
34  * @version 1.0
35  */

36
37 public class TimeFills
38 {
39     /**
40      * Default count of test passes to be executed.
41      */

42
43     public static final int DEFAULT_PASS_COUNT = 1;
44
45     /**
46      * Delay in milliseconds between test passes.
47      */

48
49     public static final int DELAY_BETWEEN_TESTS = 1000;
50
51     /**
52      * Time to wait after requesting a garbage collection.
53      */

54
55     public static final int GARBAGE_COLLECT_DELAY = 1000;
56
57     /**
58      * Initial size of array for storing values to be sorted.
59      */

60
61     public static final int INITIAL_COLLECTION_SIZE = 10;
62
63     /**
64      * Additive constant value for pseudo-random number generator.
65      */

66
67     public static final int SEQUENCE_ADD = 11;
68
69     /**
70      * Multiplicative constant value for pseudo-random number generator.
71      */

72
73     public static final int SEQUENCE_MULTIPLY = 43;
74
75     /**
76      * Last value in generated pseudo-random value sequence.
77      */

78
79     private int lastValue;
80
81     /**
82      * Start time for test run.
83      */

84
85     private long startTime;
86
87     /**
88      * Check value for validating test results.
89      */

90
91     private int checkResult;
92
93     /**
94      * Flag for printing of test results enabled.
95      */

96
97     private boolean printFlag;
98
99     /**
100      * Generates the next value in the pseudo-random sequence used for
101      * controlling the test.
102      *
103      * @return next value in sequence
104      */

105
106     protected int nextInSequence() {
107         lastValue = (lastValue + SEQUENCE_ADD) * SEQUENCE_MULTIPLY;
108         return lastValue;
109     }
110
111     /**
112      * Initializes a linear collection with a sequence of pseudo-random values.
113      *
114      * @param count number of data values to be used
115      * @param collect collection to be filled with values from pseudo-random
116      * sequence
117      */

118
119     public int fillCollection(int count, int[] collect) {
120         for (int i = 0; i < count; i++) {
121             collect[i] = nextInSequence();
122         }
123         return collect[count - 1];
124     }
125
126     public int fillCollection(int count, DirectIntArray collect) {
127         collect.clear();
128         for (int i = 0; i < count; i++) {
129             collect.add(nextInSequence());
130         }
131         return collect.get(count - 1);
132     }
133
134     public int fillCollection(int count, IntArray collect) {
135         collect.clear();
136         for (int i = 0; i < count; i++) {
137             collect.add(nextInSequence());
138         }
139         return collect.get(count - 1);
140     }
141
142     public int fillCollection(int count, IntegerArray collect) {
143         collect.clear();
144         for (int i = 0; i < count; i++) {
145             collect.add(new Integer JavaDoc(nextInSequence()));
146         }
147         return collect.get(count - 1).intValue();
148     }
149
150     public int fillCollection(int count, ObjectArray collect) {
151         collect.clear();
152         for (int i = 0; i < count; i++) {
153             collect.add(new Integer JavaDoc(nextInSequence()));
154         }
155         return ((Integer JavaDoc)collect.get(count - 1)).intValue();
156     }
157
158     public int fillCollection(int count, ArrayList collect) {
159         collect.clear();
160         for (int i = 0; i < count; i++) {
161             collect.add(new Integer JavaDoc(nextInSequence()));
162         }
163         return ((Integer JavaDoc)collect.get(count - 1)).intValue();
164     }
165
166     public int fillCollection(int count, Vector collect) {
167         collect.clear();
168         for (int i = 0; i < count; i++) {
169             collect.addElement(new Integer JavaDoc(nextInSequence()));
170         }
171         return ((Integer JavaDoc)collect.elementAt(count - 1)).intValue();
172     }
173
174     /**
175      * Resets the test timer in preparation for running a test.
176      */

177
178     protected final void startTimer() {
179         startTime = System.currentTimeMillis();
180     }
181
182     /**
183      * Attempts to trigger a garbage collection in preparation for running a
184      * test.
185      */

186
187     protected final void cleanMemory() {
188         Runtime JavaDoc rt = Runtime.getRuntime();
189         rt.gc();
190         try {
191             Thread.sleep(GARBAGE_COLLECT_DELAY);
192         } catch (InterruptedException JavaDoc ex) {}
193     }
194
195     /**
196      * Prints the time taken for a particular test run, in tenths of seconds.
197      * This method only generates output if the <code>printFlag</code> value
198      * is <code>true</code>. It assumes the <code>startTimer</code> method was
199      * called prior to the start of the test.
200      *
201      * @param result result value from test run (used for validation)
202      * @param text description text for test
203      */

204
205     protected void printTestTime(int result, String JavaDoc text) {
206         if (printFlag) {
207             long time = System.currentTimeMillis() - startTime;
208             System.out.println("Ran " + text + " add test in " + time + " ms.");
209             if (result != checkResult) {
210                 System.out.println(" Error: test result " + result +
211                     " does not match check value of " + checkResult);
212             }
213         }
214     }
215
216     /**
217      * Runs the entire sequence of tests. If printing results is enabled, the
218      * results of each timing test are printed immediately after that test is
219      * executed.
220      *
221      * @param count number of values to add to each collection
222      * @param repeats number of times to cycle each type of collection
223      * @param print flag for printing results
224      * @return sum of all test result values
225      */

226
227     public int runAllTests(int count, int repeats, boolean print) {
228
229         // print test run header
230
printFlag = print;
231         if (print) {
232             System.out.println("Running tests adding " + count +
233                 " values to each collection\n with " + repeats +
234                 " cycles for each collection type.");
235         }
236
237         // run the add element tests
238
int reset = lastValue;
239         int sum = 0;
240         int result = 0;
241         cleanMemory();
242         startTimer();
243         for (int i = 0; i < repeats; i++) {
244             int[] acoll = new int[count];
245             result += fillCollection(count, acoll);
246         }
247         checkResult = result;
248         printTestTime(result, "int[]");
249         sum += result;
250         lastValue = reset;
251         result = 0;
252         cleanMemory();
253         startTimer();
254         for (int i = 0; i < repeats; i++) {
255             DirectIntArray idcoll = new DirectIntArray(INITIAL_COLLECTION_SIZE);
256             result += fillCollection(count, idcoll);
257         }
258         printTestTime(result, "DirectIntArray");
259         sum += result;
260         lastValue = reset;
261         result = 0;
262         cleanMemory();
263         startTimer();
264         for (int i = 0; i < repeats; i++) {
265             IntArray iacoll = new IntArray(INITIAL_COLLECTION_SIZE);
266             result += fillCollection(count, iacoll);
267         }
268         printTestTime(result, "IntArray");
269         sum += result;
270         lastValue = reset;
271         result = 0;
272         cleanMemory();
273         startTimer();
274         for (int i = 0; i < repeats; i++) {
275             IntegerArray ioacoll = new IntegerArray(INITIAL_COLLECTION_SIZE);
276             result += fillCollection(count, ioacoll);
277         }
278         printTestTime(result, "IntegerArray");
279         sum += result;
280         lastValue = reset;
281         result = 0;
282         cleanMemory();
283         startTimer();
284         for (int i = 0; i < repeats; i++) {
285             ObjectArray oacoll = new ObjectArray(INITIAL_COLLECTION_SIZE);
286             result += fillCollection(count, oacoll);
287         }
288         printTestTime(result, "ObjectArray of Integer");
289         sum += result;
290         lastValue = reset;
291         result = 0;
292         cleanMemory();
293         startTimer();
294         for (int i = 0; i < repeats; i++) {
295             ArrayList alcoll = new ArrayList(INITIAL_COLLECTION_SIZE);
296             result += fillCollection(count, alcoll);
297         }
298         printTestTime(result, "ArrayList of Integer");
299         sum += result;
300         lastValue = reset;
301         result = 0;
302         cleanMemory();
303         startTimer();
304         for (int i = 0; i < repeats; i++) {
305             Vector vcoll = new Vector(INITIAL_COLLECTION_SIZE);
306             result += fillCollection(count, vcoll);
307         }
308         printTestTime(result, "Vector of Integer");
309         return sum += result;
310     }
311
312     /**
313      * Test driver, just reads the input parameters and executes the test. If
314      * requested, multiple test passes may be run.
315      *
316      * @param argv command line arguments
317      */

318
319     public static void main(String JavaDoc[] argv) {
320         if (argv.length > 1) {
321
322             // parse the parameter values
323
int count = Integer.parseInt(argv[0]);
324             int repeats = Integer.parseInt(argv[1]);
325             int passes = 0;
326             if (argv.length > 2) {
327                 passes = Integer.parseInt(argv[2]);
328             }
329
330             // construct the test instance
331
TimeFills inst = new TimeFills();
332
333             // report basic information
334
System.out.println("Java version " + System.getProperty("java.version"));
335             String JavaDoc text = System.getProperty("java.vm.name");
336             int lines = 1;
337             if (text != null) {
338                 System.out.println(text);
339                 lines++;
340             }
341             text = System.getProperty("java.vm.version");
342             if (text != null) {
343                 System.out.println(text);
344                 lines++;
345             }
346             text = System.getProperty("java.vm.vendor");
347             if (text == null) {
348                 text = System.getProperty("java.vendor");
349             }
350             System.out.println(text);
351
352             // align headers for convenient printout comparison
353
while (++lines < 5) {
354                 System.out.println("");
355             }
356
357             // execute the specified number of test passes
358
int sum = 0;
359             if (passes == 0) {
360                 System.out.println("Running initialization pass...");
361                 inst.runAllTests(count / 5, repeats, false);
362                 try {
363                     Thread.sleep(DELAY_BETWEEN_TESTS);
364                 } catch (InterruptedException JavaDoc ex) {}
365                 sum += inst.runAllTests(count, repeats, true);
366             } else {
367                 for (int i = 0; i < passes; i++) {
368                     sum += inst.runAllTests(count, repeats, true);
369                     if (i < passes) {
370                         try {
371                             Thread.sleep(DELAY_BETWEEN_TESTS);
372                         } catch (InterruptedException JavaDoc ex) {}
373                     }
374                 }
375             }
376             System.out.println("Check result value " + inst.checkResult);
377
378         } else {
379             System.out.println("Requires parameters:\n" +
380                 " count - the number of values to be added to each collection\n" +
381                 " repeats - the number of times each type of collection is built\n" +
382                 " [passes] - number of test passes to run (default is single printed\n" +
383                 " pass after initialization pass with one-fifth the loops\n");
384         }
385     }
386 }
387
Popular Tags