KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > perf > collections > CollectionsPerfTestAppBase


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest.perf.collections;
5
6 import com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.simulator.app.ApplicationConfig;
9 import com.tc.simulator.listener.ListenerProvider;
10 import com.tc.simulator.listener.StatsListener;
11 import com.tctest.runner.AbstractTransparentApp;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 /**
20  * Generic timing for {@link Collections} classes, including insertion, iteration, sorting and removal. Subclasses
21  * provide the actual {@link Collection} implemention along with the type of elements to use inside it.
22  */

23 public abstract class CollectionsPerfTestAppBase extends AbstractTransparentApp {
24
25   private Map JavaDoc rootMap = new HashMap JavaDoc();
26   private CollectionType sharedCollection;
27   private Integer JavaDoc transactionSize;
28   private StatsListener txnLogger;
29   boolean staging = true;
30   boolean described = false;
31
32   public CollectionsPerfTestAppBase() {
33     super();
34   }
35
36   public CollectionsPerfTestAppBase(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
37     super(appId, cfg, listenerProvider);
38     transactionSize = new Integer JavaDoc(System.getProperty("dso.txn.size", "5"));
39     Properties JavaDoc p = new Properties JavaDoc();
40     txnLogger = listenerProvider.newStatsListener(p);
41   }
42
43   protected void setCollection(CollectionType collect) {
44     synchronized (rootMap){
45       sharedCollection = (CollectionType)rootMap.get("collection");
46       if((sharedCollection == null) || !sharedCollection.getClass().getName().equals(collect.getClass().getName())){
47         sharedCollection = collect;
48         rootMap.put("collection", sharedCollection);
49       }
50     }
51   }
52
53   public void runPerType(ElementType.Factory elementFactory, int stageStart) {
54
55     final int totalElementCount = getIntensity();
56     final int txnSize = transactionSize.intValue();
57     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc("intensity: ").append(getIntensity());
58     sbuf.append(" participant count: ").append(getParticipantCount());
59     sbuf.append(" totalElementCount: ").append(totalElementCount);
60     sbuf.append(" transactionSize: ").append(txnSize);
61     if (!described) {
62       described = true;
63       System.out.println(sbuf.toString());
64     }
65
66     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("collection type: " + sharedCollection.describeType());
67     buf.append(", element type: " + elementFactory.describeType());
68     buf.append(" staging:" + (staging ? "On" : "Off"));
69     buf.append(", clientId=" + getApplicationId() + " ");
70     // buf.append(", threadId=" + Thread.currentThread().getName() + " ");
71
String JavaDoc testBase = buf.toString();
72     String JavaDoc testDesc = testBase;
73
74     try {
75       moveToStageAndWait(stageStart + 1);
76       testDesc = "stage: " + (stageStart + 1) + " " + testBase;
77       addElements(elementFactory, totalElementCount, txnSize, testDesc);
78
79       if (staging) moveToStageAndWait(stageStart + 2);
80       testDesc = "stage: " + (stageStart + 2) + " " + testBase;
81       sortCollection(testDesc);
82
83       if (staging) moveToStageAndWait(stageStart + 3);
84       testDesc = "stage: " + (stageStart + 3) + " " + testBase;
85       iterateCollection(testDesc);
86
87       if (staging) moveToStageAndWait(stageStart + 4);
88       testDesc = "stage: " + (stageStart + 4) + " " + testBase;
89       removeElements(totalElementCount, txnSize, testDesc);
90
91       moveToStageAndWait(stageStart + 5);
92       testDesc = "stage: " + (stageStart + 5) + " " + testBase;
93       clearCollection(testDesc);
94     } catch (Exception JavaDoc e) {
95       notifyError(e);
96       return;
97     }
98   }
99
100   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
101     // add root
102
config.addRoot("rootMap", CollectionsPerfTestAppBase.class.getName() + ".rootMap");
103     // add all perf classes
104
config.addIncludePattern("com.tctest.perf.collections.*");
105     // add all methods
106
String JavaDoc methodExpression = "* " + CollectionsPerfTestAppBase.class.getName() + "*.*(..)";
107     config.addWriteAutolock(methodExpression);
108   }
109
110   public final void run() {
111     int stageStart = 0;
112     int stageInc = 5;
113     runPerType(new ElementType.LongFactory(), stageStart);
114     stageStart += stageInc;
115     runPerType(new ElementType.StringFactory(), stageStart);
116     stageStart += stageInc;
117     runPerType(new ElementType.GraphFactory(20, new ElementType.LongFactory()), stageStart);
118     stageStart += stageInc;
119     runPerType(new ElementType.GraphFactory(20, new ElementType.StringFactory()), stageStart);
120     staging = false;
121     stageStart += stageInc;
122     runPerType(new ElementType.LongFactory(), stageStart);
123     stageStart += stageInc;
124     runPerType(new ElementType.StringFactory(), stageStart);
125     stageStart += stageInc;
126     runPerType(new ElementType.GraphFactory(20, new ElementType.LongFactory()), stageStart);
127     stageStart += stageInc;
128     runPerType(new ElementType.GraphFactory(20, new ElementType.StringFactory()), stageStart);
129
130   }
131
132   private void removeElements(final int totalElementCount, final int txnSize, String JavaDoc testDesc) {
133     final Timer txnTimer = new Timer();
134     final Timer lockTimer = new Timer();
135     final Timer unlockTimer = new Timer();
136     final Timer opTimer = new Timer();
137
138     for (int elementCount = 0; elementCount < totalElementCount; elementCount += txnSize) {
139       int count = Math.min(txnSize, totalElementCount - elementCount);
140       txnTimer.start();
141       lockTimer.start();
142       synchronized (sharedCollection) {
143         lockTimer.stop();
144
145         opTimer.start();
146         sharedCollection.remove(count);
147         opTimer.stop();
148
149         unlockTimer.start();
150       }
151       unlockTimer.stop();
152       txnTimer.stop();
153       txnLogger.sample(-1, testDesc + "removed " + count + " elements Txn stats: lock=" + lockTimer.getElapsedMillis() + " op="
154                            + opTimer.getElapsedMillis() + " unlock=" + unlockTimer.getElapsedMillis() + " txn="
155                            + txnTimer.getElapsedMillis());
156       Thread.yield();
157     }
158     txnLogger.sample(-1, testDesc + "remove stage avg stats: lock=" + lockTimer.getAvgElapsed() + " op="
159                          + opTimer.getAvgElapsed() + " unlock=" + unlockTimer.getAvgElapsed() + " txn="
160                          + txnTimer.getAvgElapsed());
161   }
162
163   private void iterateCollection(String JavaDoc testDesc) {
164     final Timer txnTimer = new Timer();
165     final Timer lockTimer = new Timer();
166     final Timer unlockTimer = new Timer();
167     final Timer opTimer = new Timer();
168
169     txnTimer.start();
170     lockTimer.start();
171     synchronized (sharedCollection) {
172       lockTimer.stop();
173
174       opTimer.start();
175       sharedCollection.iterate();
176       opTimer.stop();
177
178       unlockTimer.start();
179     }
180     unlockTimer.stop();
181     txnTimer.stop();
182     txnLogger.sample(-1, testDesc + "iterate Txn stats: lock=" + lockTimer.getElapsedMillis() + " op="
183                          + opTimer.getElapsedMillis() + " unlock=" + unlockTimer.getElapsedMillis() + " txn="
184                          + txnTimer.getElapsedMillis());
185     Thread.yield();
186   }
187
188   private void sortCollection(String JavaDoc testDesc) {
189     final Timer txnTimer = new Timer();
190     final Timer lockTimer = new Timer();
191     final Timer unlockTimer = new Timer();
192     final Timer opTimer = new Timer();
193
194     txnTimer.start();
195     lockTimer.start();
196     synchronized (sharedCollection) {
197       lockTimer.stop();
198
199       if (!sharedCollection.isSorted()) {
200         opTimer.start();
201         sharedCollection.sort();
202         sharedCollection.setSorted(true);
203         opTimer.stop();
204
205       }
206       unlockTimer.start();
207     }
208     unlockTimer.stop();
209     txnTimer.stop();
210     txnLogger.sample(-1, testDesc + "sort Txn stats: lock=" + lockTimer.getElapsedMillis() + " op="
211                          + opTimer.getElapsedMillis() + " unlock=" + unlockTimer.getElapsedMillis() + " txn="
212                          + txnTimer.getElapsedMillis());
213     Thread.yield();
214   }
215
216   private void addElements(ElementType.Factory elementFactory, final int totalElementCount, final int txnSize,
217                            String JavaDoc testDesc) {
218     final Timer txnTimer = new Timer();
219     final Timer lockTimer = new Timer();
220     final Timer unlockTimer = new Timer();
221     final Timer opTimer = new Timer();
222
223     for (int elementCount = 0; elementCount < totalElementCount; elementCount += txnSize) {
224       int count = Math.min(txnSize, totalElementCount - elementCount);
225       txnTimer.start();
226       lockTimer.start();
227       synchronized (sharedCollection) {
228         lockTimer.stop();
229
230         opTimer.start();
231         sharedCollection.add(count, elementFactory);
232         opTimer.stop();
233
234         unlockTimer.start();
235       }
236       unlockTimer.stop();
237       txnTimer.stop();
238       txnLogger.sample(-1, testDesc + "added " + count + " elements Txn stats: lock=" + lockTimer.getElapsedMillis() + " op="
239                            + opTimer.getElapsedMillis() + " unlock=" + unlockTimer.getElapsedMillis() + " txn="
240                            + txnTimer.getElapsedMillis());
241       Thread.yield();
242     }
243     txnLogger.sample(-1, testDesc + "addElements stage avg stats: lock=" + lockTimer.getAvgElapsed() + " op="
244                          + opTimer.getAvgElapsed() + " unlock=" + unlockTimer.getAvgElapsed() + " txn="
245                          + txnTimer.getAvgElapsed());
246   }
247
248   private void clearCollection(String JavaDoc testDesc) {
249     synchronized (sharedCollection) {
250       if (!(sharedCollection.size() == 0)) {
251         txnLogger.sample(-1, testDesc + "clearing collection");
252         sharedCollection.clear();
253       }
254     }
255   }
256
257 }
258
Popular Tags