001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest.perf.collections;
005:
006: import com.tc.object.config.ConfigVisitor;
007: import com.tc.object.config.DSOClientConfigHelper;
008: import com.tc.simulator.app.ApplicationConfig;
009: import com.tc.simulator.listener.ListenerProvider;
010: import com.tc.simulator.listener.StatsListener;
011: import com.tctest.runner.AbstractTransparentApp;
012:
013: import java.util.Collection;
014: import java.util.Collections;
015: import java.util.HashMap;
016: import java.util.Map;
017: import java.util.Properties;
018:
019: /**
020: * Generic timing for {@link Collections} classes, including insertion, iteration, sorting and removal. Subclasses
021: * provide the actual {@link Collection} implemention along with the type of elements to use inside it.
022: */
023: public abstract class CollectionsPerfTestAppBase extends
024: AbstractTransparentApp {
025:
026: private Map rootMap = new HashMap();
027: private CollectionType sharedCollection;
028: private Integer transactionSize;
029: private StatsListener txnLogger;
030: boolean staging = true;
031: boolean described = false;
032:
033: public CollectionsPerfTestAppBase() {
034: super ();
035: }
036:
037: public CollectionsPerfTestAppBase(String appId,
038: ApplicationConfig cfg, ListenerProvider listenerProvider) {
039: super (appId, cfg, listenerProvider);
040: transactionSize = new Integer(System.getProperty(
041: "dso.txn.size", "5"));
042: Properties p = new Properties();
043: txnLogger = listenerProvider.newStatsListener(p);
044: }
045:
046: protected void setCollection(CollectionType collect) {
047: synchronized (rootMap) {
048: sharedCollection = (CollectionType) rootMap
049: .get("collection");
050: if ((sharedCollection == null)
051: || !sharedCollection.getClass().getName().equals(
052: collect.getClass().getName())) {
053: sharedCollection = collect;
054: rootMap.put("collection", sharedCollection);
055: }
056: }
057: }
058:
059: public void runPerType(ElementType.Factory elementFactory,
060: int stageStart) {
061:
062: final int totalElementCount = getIntensity();
063: final int txnSize = transactionSize.intValue();
064: StringBuffer sbuf = new StringBuffer("intensity: ")
065: .append(getIntensity());
066: sbuf.append(" participant count: ").append(
067: getParticipantCount());
068: sbuf.append(" totalElementCount: ").append(totalElementCount);
069: sbuf.append(" transactionSize: ").append(txnSize);
070: if (!described) {
071: described = true;
072: System.out.println(sbuf.toString());
073: }
074:
075: StringBuffer buf = new StringBuffer("collection type: "
076: + sharedCollection.describeType());
077: buf.append(", element type: " + elementFactory.describeType());
078: buf.append(" staging:" + (staging ? "On" : "Off"));
079: buf.append(", clientId=" + getApplicationId() + " ");
080: // buf.append(", threadId=" + Thread.currentThread().getName() + " ");
081: String testBase = buf.toString();
082: String testDesc = testBase;
083:
084: try {
085: moveToStageAndWait(stageStart + 1);
086: testDesc = "stage: " + (stageStart + 1) + " " + testBase;
087: addElements(elementFactory, totalElementCount, txnSize,
088: testDesc);
089:
090: if (staging)
091: moveToStageAndWait(stageStart + 2);
092: testDesc = "stage: " + (stageStart + 2) + " " + testBase;
093: sortCollection(testDesc);
094:
095: if (staging)
096: moveToStageAndWait(stageStart + 3);
097: testDesc = "stage: " + (stageStart + 3) + " " + testBase;
098: iterateCollection(testDesc);
099:
100: if (staging)
101: moveToStageAndWait(stageStart + 4);
102: testDesc = "stage: " + (stageStart + 4) + " " + testBase;
103: removeElements(totalElementCount, txnSize, testDesc);
104:
105: moveToStageAndWait(stageStart + 5);
106: testDesc = "stage: " + (stageStart + 5) + " " + testBase;
107: clearCollection(testDesc);
108: } catch (Exception e) {
109: notifyError(e);
110: return;
111: }
112: }
113:
114: public static void visitL1DSOConfig(ConfigVisitor visitor,
115: DSOClientConfigHelper config) {
116: // add root
117: config.addRoot("rootMap", CollectionsPerfTestAppBase.class
118: .getName()
119: + ".rootMap");
120: // add all perf classes
121: config.addIncludePattern("com.tctest.perf.collections.*");
122: // add all methods
123: String methodExpression = "* "
124: + CollectionsPerfTestAppBase.class.getName()
125: + "*.*(..)";
126: config.addWriteAutolock(methodExpression);
127: }
128:
129: public final void run() {
130: int stageStart = 0;
131: int stageInc = 5;
132: runPerType(new ElementType.LongFactory(), stageStart);
133: stageStart += stageInc;
134: runPerType(new ElementType.StringFactory(), stageStart);
135: stageStart += stageInc;
136: runPerType(new ElementType.GraphFactory(20,
137: new ElementType.LongFactory()), stageStart);
138: stageStart += stageInc;
139: runPerType(new ElementType.GraphFactory(20,
140: new ElementType.StringFactory()), stageStart);
141: staging = false;
142: stageStart += stageInc;
143: runPerType(new ElementType.LongFactory(), stageStart);
144: stageStart += stageInc;
145: runPerType(new ElementType.StringFactory(), stageStart);
146: stageStart += stageInc;
147: runPerType(new ElementType.GraphFactory(20,
148: new ElementType.LongFactory()), stageStart);
149: stageStart += stageInc;
150: runPerType(new ElementType.GraphFactory(20,
151: new ElementType.StringFactory()), stageStart);
152:
153: }
154:
155: private void removeElements(final int totalElementCount,
156: final int txnSize, String testDesc) {
157: final Timer txnTimer = new Timer();
158: final Timer lockTimer = new Timer();
159: final Timer unlockTimer = new Timer();
160: final Timer opTimer = new Timer();
161:
162: for (int elementCount = 0; elementCount < totalElementCount; elementCount += txnSize) {
163: int count = Math.min(txnSize, totalElementCount
164: - elementCount);
165: txnTimer.start();
166: lockTimer.start();
167: synchronized (sharedCollection) {
168: lockTimer.stop();
169:
170: opTimer.start();
171: sharedCollection.remove(count);
172: opTimer.stop();
173:
174: unlockTimer.start();
175: }
176: unlockTimer.stop();
177: txnTimer.stop();
178: txnLogger.sample(-1, testDesc + "removed " + count
179: + " elements Txn stats: lock="
180: + lockTimer.getElapsedMillis() + " op="
181: + opTimer.getElapsedMillis() + " unlock="
182: + unlockTimer.getElapsedMillis() + " txn="
183: + txnTimer.getElapsedMillis());
184: Thread.yield();
185: }
186: txnLogger.sample(-1, testDesc + "remove stage avg stats: lock="
187: + lockTimer.getAvgElapsed() + " op="
188: + opTimer.getAvgElapsed() + " unlock="
189: + unlockTimer.getAvgElapsed() + " txn="
190: + txnTimer.getAvgElapsed());
191: }
192:
193: private void iterateCollection(String testDesc) {
194: final Timer txnTimer = new Timer();
195: final Timer lockTimer = new Timer();
196: final Timer unlockTimer = new Timer();
197: final Timer opTimer = new Timer();
198:
199: txnTimer.start();
200: lockTimer.start();
201: synchronized (sharedCollection) {
202: lockTimer.stop();
203:
204: opTimer.start();
205: sharedCollection.iterate();
206: opTimer.stop();
207:
208: unlockTimer.start();
209: }
210: unlockTimer.stop();
211: txnTimer.stop();
212: txnLogger.sample(-1, testDesc + "iterate Txn stats: lock="
213: + lockTimer.getElapsedMillis() + " op="
214: + opTimer.getElapsedMillis() + " unlock="
215: + unlockTimer.getElapsedMillis() + " txn="
216: + txnTimer.getElapsedMillis());
217: Thread.yield();
218: }
219:
220: private void sortCollection(String testDesc) {
221: final Timer txnTimer = new Timer();
222: final Timer lockTimer = new Timer();
223: final Timer unlockTimer = new Timer();
224: final Timer opTimer = new Timer();
225:
226: txnTimer.start();
227: lockTimer.start();
228: synchronized (sharedCollection) {
229: lockTimer.stop();
230:
231: if (!sharedCollection.isSorted()) {
232: opTimer.start();
233: sharedCollection.sort();
234: sharedCollection.setSorted(true);
235: opTimer.stop();
236:
237: }
238: unlockTimer.start();
239: }
240: unlockTimer.stop();
241: txnTimer.stop();
242: txnLogger.sample(-1, testDesc + "sort Txn stats: lock="
243: + lockTimer.getElapsedMillis() + " op="
244: + opTimer.getElapsedMillis() + " unlock="
245: + unlockTimer.getElapsedMillis() + " txn="
246: + txnTimer.getElapsedMillis());
247: Thread.yield();
248: }
249:
250: private void addElements(ElementType.Factory elementFactory,
251: final int totalElementCount, final int txnSize,
252: String testDesc) {
253: final Timer txnTimer = new Timer();
254: final Timer lockTimer = new Timer();
255: final Timer unlockTimer = new Timer();
256: final Timer opTimer = new Timer();
257:
258: for (int elementCount = 0; elementCount < totalElementCount; elementCount += txnSize) {
259: int count = Math.min(txnSize, totalElementCount
260: - elementCount);
261: txnTimer.start();
262: lockTimer.start();
263: synchronized (sharedCollection) {
264: lockTimer.stop();
265:
266: opTimer.start();
267: sharedCollection.add(count, elementFactory);
268: opTimer.stop();
269:
270: unlockTimer.start();
271: }
272: unlockTimer.stop();
273: txnTimer.stop();
274: txnLogger.sample(-1, testDesc + "added " + count
275: + " elements Txn stats: lock="
276: + lockTimer.getElapsedMillis() + " op="
277: + opTimer.getElapsedMillis() + " unlock="
278: + unlockTimer.getElapsedMillis() + " txn="
279: + txnTimer.getElapsedMillis());
280: Thread.yield();
281: }
282: txnLogger.sample(-1, testDesc
283: + "addElements stage avg stats: lock="
284: + lockTimer.getAvgElapsed() + " op="
285: + opTimer.getAvgElapsed() + " unlock="
286: + unlockTimer.getAvgElapsed() + " txn="
287: + txnTimer.getAvgElapsed());
288: }
289:
290: private void clearCollection(String testDesc) {
291: synchronized (sharedCollection) {
292: if (!(sharedCollection.size() == 0)) {
293: txnLogger.sample(-1, testDesc + "clearing collection");
294: sharedCollection.clear();
295: }
296: }
297: }
298:
299: }
|