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.tc.util;
005:
006: import com.tc.object.ObjectID;
007: import com.tc.test.TCTestCase;
008:
009: import gnu.trove.THashMap;
010: import gnu.trove.THashSet;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.HashMap;
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.LinkedHashSet;
018: import java.util.List;
019: import java.util.Map;
020: import java.util.Random;
021: import java.util.Set;
022:
023: public class SetMapPerformanceTest extends TCTestCase {
024:
025: private static final int ADD_LENGTH = 200000;
026: private static final int PUT_LENGTH = 200000;
027:
028: private TestResults testResult;
029:
030: public void testSetPerformance() throws Exception {
031:
032: // These are here to make sure JIT compilation happens
033: println("Warmup runs....");
034: testResult = new TestResults(); // null
035: Collection c = createSequencialData(2000);
036: performTest(new ObjectIDSet(), c);
037: performTest(new ObjectIDSet2(), c);
038: performTest(new HashSet(), c);
039: performTest(new ObjectIDSet2(), c);
040: performTest(new HashSet(), c);
041: performTest(new ObjectIDSet2(), c);
042: performTest(new HashSet(), c);
043:
044: testResult = new TestResults();
045:
046: // real test begins - sequencial
047: c = createSequencialData(ADD_LENGTH);
048: println();
049: println("---------------------------------------------------------------------------------------");
050: println("Sequential ObjectIDs");
051: println("---------------------------------------------------------------------------------------");
052: // performTest(new ObjectIDSet(), c);
053: performTest(new ObjectIDSet2(), c);
054: performTest(new HashSet(), c);
055: performTest(new THashSet(), c);
056:
057: c = null;
058: testResult.printResults();
059: testResult = new TestResults();
060:
061: // Random
062: c = createRandomData(ADD_LENGTH);
063: println();
064: println("---------------------------------------------------------------------------------------");
065: println("Random ObjectIDs");
066: println("---------------------------------------------------------------------------------------");
067: // performTest(new ObjectIDSet(), c);
068: performTest(new ObjectIDSet2(), c);
069: performTest(new HashSet(), c);
070: performTest(new THashSet(), c);
071:
072: testResult.printResults();
073: }
074:
075: // This is no good. Need to improve this test
076: public void testTHashMapPerformance() {
077: testResult = new TestResults();
078: THashMap map = new THashMap();
079: Set c = createSequencialData(PUT_LENGTH);
080:
081: performTest("Sequencial IDs", map, c);
082:
083: c = null;
084: map = new THashMap();
085: c = createRandomData(PUT_LENGTH);
086: performTest("Random IDs", map, c);
087:
088: testResult.printResults();
089: }
090:
091: private void performTest(String characteristics, Map map, Set ids) {
092: TestRecord tr = testResult
093: .getTestRecord("Map Performance Test : size = "
094: + PUT_LENGTH);
095: Object val = new Object();
096: StopClock sc = new StopClock();
097: sc.start();
098: for (Iterator i = ids.iterator(); i.hasNext();) {
099: map.put(i.next(), val);
100: }
101: sc.stop();
102: tr.addResult(characteristics + " : " + map.getClass().getName()
103: + " : " + sc);
104:
105: }
106:
107: private void performTest(Set set, Collection c) {
108: println("Running tests on " + set.getClass().getName());
109: performAddsOneAtATime(set, c);
110: set.clear();
111: performAddAll(set, c);
112: performRemovesOneAtATime(set, c);
113: performRemoveAll(set, c);
114: }
115:
116: private void performRemoveAll(Set set, Collection c) {
117: TestRecord tr = testResult
118: .getTestRecord("Performing removeAll() : size = "
119: + set.size() + " removing " + c.size());
120: StopClock sc = new StopClock();
121: sc.start();
122: set.removeAll(c);
123: sc.stop();
124: tr.addResult(set.getClass().getName() + " : " + sc);
125: }
126:
127: private void performRemovesOneAtATime(Set set, Collection c) {
128: TestRecord tr = testResult
129: .getTestRecord("Performing remove() : size = "
130: + set.size() + " removing " + c.size() / 2);
131: StopClock sc = new StopClock();
132: for (int j = 0; j < 2; j++) {
133: int count = 0;
134: for (Iterator i = c.iterator(); i.hasNext()
135: && count++ <= c.size() / 4;) {
136: sc.start();
137: set.remove(i.next());
138: sc.stop();
139: }
140: }
141: tr.addResult(set.getClass().getName() + " : " + sc);
142: }
143:
144: private static void println() {
145: println("");
146: }
147:
148: private static void println(String s) {
149: System.out.println(s);
150: }
151:
152: private void performAddAll(Set set, Collection c) {
153: TestRecord tr = testResult
154: .getTestRecord("Performing addAll() : size = "
155: + c.size());
156: StopClock sc = new StopClock();
157: sc.start();
158: set.addAll(c);
159: sc.stop();
160: tr.addResult(set.getClass().getName() + " : " + sc);
161: }
162:
163: private Set createSequencialData(int size) {
164: return createSequencialData(0, size);
165: }
166:
167: private Set createSequencialData(int init, int size) {
168: return createSequencialData(init, size, 1);
169: }
170:
171: private Set createSequencialData(int init, int size, int step) {
172: HashSet set = new LinkedHashSet();
173: while (size-- > 0) {
174: set.add(new ObjectID(init));
175: init += step;
176: }
177: return set;
178: }
179:
180: Random r = new Random();
181:
182: private Set createRandomData(int size) {
183: HashSet set = new LinkedHashSet();
184: for (int j = 0; j < size; j++) {
185: set.add(new ObjectID(r.nextLong()));
186: }
187: return set;
188: }
189:
190: private void performAddsOneAtATime(Set set, Collection c) {
191: TestRecord tr = testResult
192: .getTestRecord("Performing add() : size = " + c.size());
193: StopClock sc = new StopClock();
194: for (Iterator i = c.iterator(); i.hasNext();) {
195: sc.start();
196: set.add(i.next());
197: sc.stop();
198: }
199: tr.addResult(set.getClass().getName() + " : " + sc);
200: }
201:
202: static class StopClock {
203: long cumulative;
204: long start;
205: long lastLap;
206:
207: void start() {
208: start = System.currentTimeMillis();
209: }
210:
211: void stop() {
212: lastLap = System.currentTimeMillis() - start;
213: cumulative += lastLap;
214: }
215:
216: void reset() {
217: cumulative = start = lastLap = 0;
218: }
219:
220: public String dump() {
221: return "Time taken = " + cumulative + " ms : Last Lap = "
222: + lastLap + " ms";
223: }
224:
225: public String toString() {
226: return "Time taken = " + cumulative + " ms";
227: }
228: }
229:
230: static class TestResults {
231: Map tests = new HashMap();
232:
233: public TestRecord getTestRecord(String testName) {
234: TestRecord tr = (TestRecord) tests.get(testName);
235: if (tr == null) {
236: tr = new TestRecord(testName);
237: tests.put(testName, tr);
238: }
239: return tr;
240: }
241:
242: public void printResults() {
243: println();
244: println("---------------------------------------------------------------------------------------");
245: println(" TEST RESULTS");
246: println("---------------------------------------------------------------------------------------");
247: println();
248: for (Iterator i = tests.values().iterator(); i.hasNext();) {
249: TestRecord tr = (TestRecord) i.next();
250: tr.printResults();
251: }
252: }
253:
254: }
255:
256: static class TestRecord {
257:
258: private final String testName;
259: private final List results = new ArrayList();
260:
261: public TestRecord(String testName) {
262: this .testName = testName;
263: }
264:
265: public void printResults() {
266: println();
267: println(testName);
268: println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
269: for (Iterator i = results.iterator(); i.hasNext();) {
270: println(String.valueOf(i.next()));
271: }
272: }
273:
274: public void addResult(String result) {
275: results.add(result);
276: }
277:
278: }
279: }
|