KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > PerfContext


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution;
10
11 import javolution.context.ConcurrentContext;
12 import javolution.context.ObjectFactory;
13 import javolution.context.PoolContext;
14 import javolution.context.RealtimeObject;
15 import javolution.context.ConcurrentContext.Logic;
16 import javolution.lang.MathLib;
17 import javolution.util.FastComparator;
18 import javolution.util.FastTable;
19
20 /**
21  * <p> This class holds {@link javolution.context} benchmark.</p>
22  *
23  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
24  * @version 2.0, November 26, 2004
25  */

26 final class PerfContext extends Javolution implements Runnable JavaDoc {
27
28     Object JavaDoc[] _objects = new Object JavaDoc[1000]; // Larger arrays result in really
29
// poor heap performance (full GC ?)
30
/**
31      * Executes benchmark.
32      */

33     public void run() {
34         println("//////////////////////////////////");
35         println("// Package: javolution.context //");
36         println("//////////////////////////////////");
37         println("");
38
39         benchmarkConcurrency();
40         benchmarkSmallObjects();
41         benchmarkHeapArrays();
42         benchmarkStackArrays();
43         println("");
44     }
45
46     private static final int N = 10000;
47     private void benchmarkConcurrency() {
48  
49         println("-- Concurrent Context --");
50         print("Quick Sort " + N + " elements - Concurrency disabled: ");
51         ConcurrentContext.setEnabled(false);
52         for (int i=0; i < 1000; i++) {
53             FastTable table = randomTable();
54             startTime();
55             quickSort(table);
56             keepBestTime(1);
57         }
58         println(endTime());
59
60         print("Quick Sort " + N + " elements - Concurrency ("
61                 + ConcurrentContext.CONCURRENCY.get() + ") enabled: ");
62         ConcurrentContext.setEnabled(true);
63         for (int i=0; i < 1000; i++) {
64             FastTable table = randomTable();
65             startTime();
66             quickSort(table);
67             keepBestTime(1);
68         }
69         println(endTime());
70
71         println("");
72     }
73
74     private FastTable randomTable() {
75         FastTable table = new FastTable(N);
76         for (int i=0; i < N; i++) {
77             table.add(new Integer JavaDoc(MathLib.random(Integer.MIN_VALUE, Integer.MAX_VALUE)));
78         }
79         return table;
80     }
81
82     private void quickSort(final FastTable table) {
83         final int size = table.size();
84         if (size < 100) {
85             table.setValueComparator(INTEGER_COMPARATOR);
86             table.sort();
87         } else {
88             final FastTable t1 = FastTable.newInstance();
89             final FastTable t2 = FastTable.newInstance();
90             ConcurrentContext.enter();
91             try {
92                 ConcurrentContext.execute(new Logic() {
93                     public void run() {
94                         t1.addAll(table.subList(0, size / 2));
95                         quickSort(t1);
96                     }
97                 });
98                 ConcurrentContext.execute(new Logic() {
99                     public void run() {
100                         t2.addAll(table.subList(size / 2, size));
101                         quickSort(t2);
102                     }
103                 });
104             } finally {
105                 ConcurrentContext.exit();
106             }
107             // Merges results.
108
for (int i=0, i1=0, i2=0; i < size; i++) {
109                  if (i1 >= t1.size()) {
110                      table.set(i, t2.get(i2++));
111                  } else if (i2 >= t2.size()) {
112                      table.set(i, t1.get(i1++));
113                  } else {
114                      Integer JavaDoc o1 = (Integer JavaDoc) t1.get(i1);
115                      Integer JavaDoc o2 = (Integer JavaDoc) t2.get(i2);
116                      if (o1.intValue() < o2.intValue()) {
117                          table.set(i, o1);
118                          i1++;
119                      } else {
120                          table.set(i, o2);
121                          i2++;
122                      }
123                  }
124             }
125             FastTable.recycle(t1);
126             FastTable.recycle(t2);
127         }
128     }
129     
130     // For J2ME compability.
131
private static final FastComparator INTEGER_COMPARATOR = new FastComparator() {
132
133         public boolean areEqual(Object JavaDoc o1, Object JavaDoc o2) {
134             return ((Integer JavaDoc)o1).intValue() == ((Integer JavaDoc)o2).intValue();
135         }
136
137         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
138             return ((Integer JavaDoc)o2).intValue() - ((Integer JavaDoc)o1).intValue();
139         }
140
141         public int hashCodeOf(Object JavaDoc obj) {
142             return ((Integer JavaDoc)obj).intValue();
143         }
144     };
145     
146  
147     private void benchmarkSmallObjects() {
148
149         println("-- Heap versus Stack Allocation (Pool-Context) --");
150         print("Small object heap creation: ");
151         
152         for (int i = 0; i < 100000; i++) {
153             startTime();
154             for (int j = 0; j < _objects.length;) {
155                 _objects[j++] = new SmallObject();
156             }
157             keepBestTime(_objects.length);
158         }
159         println(endTime());
160
161         print("Small object stack creation: ");
162         for (int i = 0; i < 100000; i++) {
163             startTime();
164             PoolContext.enter();
165             for (int j = 0; j < _objects.length;) {
166                 _objects[j++] = SmallObject.FACTORY.object();
167             }
168             PoolContext.exit();
169             keepBestTime(_objects.length);
170         }
171         println(endTime());
172
173     }
174
175     private void benchmarkHeapArrays() {
176         print("char[256] heap creation: ");
177         for (int i = 0; i < 1000; i++) {
178             startTime();
179             for (int j = 0; j < _objects.length;) {
180                 _objects[j++] = new char[256];
181             }
182             keepBestTime(_objects.length);
183         }
184         println(endTime());
185
186         print("char[512] heap creation: ");
187         for (int i = 0; i < 1000; i++) {
188             startTime();
189             for (int j = 0; j < _objects.length;) {
190                 _objects[j++] = new char[512];
191             }
192             keepBestTime(_objects.length);
193         }
194         println(endTime());
195
196     }
197
198     private void benchmarkStackArrays() {
199         print("char[256] stack creation: ");
200         for (int i = 0; i < 1000; i++) {
201             startTime();
202             PoolContext.enter();
203             for (int j = 0; j < _objects.length;) {
204                 _objects[j++] = CHAR256_FACTORY.object();
205             }
206             PoolContext.exit();
207             keepBestTime(_objects.length);
208         }
209         println(endTime());
210
211         print("char[512] stack creation: ");
212         for (int i = 0; i < 1000; i++) {
213             startTime();
214             PoolContext.enter();
215             for (int j = 0; j < _objects.length;) {
216                 _objects[j++] = CHAR512_FACTORY.object();
217             }
218             PoolContext.exit();
219             keepBestTime(_objects.length);
220         }
221         println(endTime());
222     }
223
224     private static final class SmallObject extends RealtimeObject {
225         long longValue;
226
227         int intValue;
228
229         SmallObject refValue;
230
231         static final Factory FACTORY = new Factory() {
232             public Object JavaDoc create() {
233                 return new SmallObject();
234             }
235         };
236     }
237
238     private static final ObjectFactory CHAR256_FACTORY = new ObjectFactory() {
239         public Object JavaDoc create() {
240             return new char[256];
241         }
242     };
243
244     private static final ObjectFactory CHAR512_FACTORY = new ObjectFactory() {
245         public Object JavaDoc create() {
246             return new char[512];
247         }
248     };
249 }
Popular Tags