1 4 package com.tc.util; 5 6 import com.tc.object.ObjectID; 7 import com.tc.test.TCTestCase; 8 9 import gnu.trove.THashMap; 10 import gnu.trove.THashSet; 11 12 import java.util.ArrayList ; 13 import java.util.Collection ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.LinkedHashSet ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Random ; 21 import java.util.Set ; 22 23 public class SetMapPerformanceTest extends TCTestCase { 24 25 private static final int ADD_LENGTH = 200000; 26 private static final int PUT_LENGTH = 200000; 27 28 private TestResults testResult; 29 30 public void testSetPerformance() throws Exception { 31 32 println("Warmup runs...."); 34 testResult = new TestResults(); Collection c = createSequencialData(2000); 36 performTest(new ObjectIDSet(), c); 37 performTest(new ObjectIDSet2(), c); 38 performTest(new HashSet(), c); 39 performTest(new ObjectIDSet2(), c); 40 performTest(new HashSet(), c); 41 performTest(new ObjectIDSet2(), c); 42 performTest(new HashSet(), c); 43 44 testResult = new TestResults(); 45 46 c = createSequencialData(ADD_LENGTH); 48 println(); 49 println("---------------------------------------------------------------------------------------"); 50 println("Sequential ObjectIDs"); 51 println("---------------------------------------------------------------------------------------"); 52 performTest(new ObjectIDSet2(), c); 54 performTest(new HashSet(), c); 55 performTest(new THashSet(), c); 56 57 c = null; 58 testResult.printResults(); 59 testResult = new TestResults(); 60 61 c = createRandomData(ADD_LENGTH); 63 println(); 64 println("---------------------------------------------------------------------------------------"); 65 println("Random ObjectIDs"); 66 println("---------------------------------------------------------------------------------------"); 67 performTest(new ObjectIDSet2(), c); 69 performTest(new HashSet(), c); 70 performTest(new THashSet(), c); 71 72 testResult.printResults(); 73 } 74 75 public void testTHashMapPerformance() { 77 testResult = new TestResults(); 78 THashMap map = new THashMap(); 79 Set c = createSequencialData(PUT_LENGTH ); 80 81 performTest("Sequencial IDs" , map, c); 82 83 c = null; 84 map = new THashMap(); 85 c = createRandomData(PUT_LENGTH); 86 performTest("Random IDs" , map, c); 87 88 testResult.printResults(); 89 } 90 91 private void performTest(String characteristics, Map map, Set ids) { 92 TestRecord tr = testResult.getTestRecord("Map Performance Test : size = " + PUT_LENGTH); 93 Object val = new Object (); 94 StopClock sc = new StopClock(); 95 sc.start(); 96 for (Iterator i = ids.iterator(); i.hasNext();) { 97 map.put(i.next(), val); 98 } 99 sc.stop(); 100 tr.addResult( characteristics + " : " + map.getClass().getName()+ " : " + sc); 101 102 } 103 104 private void performTest(Set set, Collection c) { 105 println("Running tests on " + set.getClass().getName()); 106 performAddsOneAtATime(set, c); 107 set.clear(); 108 performAddAll(set, c); 109 performRemovesOneAtATime(set, c); 110 performRemoveAll(set, c); 111 } 112 113 private void performRemoveAll(Set set, Collection c) { 114 TestRecord tr = testResult.getTestRecord("Performing removeAll() : size = " + set.size() + " removing " + c.size()); 115 StopClock sc = new StopClock(); 116 sc.start(); 117 set.removeAll(c); 118 sc.stop(); 119 tr.addResult(set.getClass().getName() + " : " + sc); 120 } 121 122 private void performRemovesOneAtATime(Set set, Collection c) { 123 TestRecord tr = testResult 124 .getTestRecord("Performing remove() : size = " + set.size() + " removing " + c.size() / 2); 125 StopClock sc = new StopClock(); 126 for (int j = 0; j < 2; j++) { 127 int count = 0; 128 for (Iterator i = c.iterator(); i.hasNext() && count++ <= c.size() / 4;) { 129 sc.start(); 130 set.remove(i.next()); 131 sc.stop(); 132 } 133 } 134 tr.addResult(set.getClass().getName() + " : " + sc); 135 } 136 137 private static void println() { 138 println(""); 139 } 140 141 private static void println(String s) { 142 System.out.println(s); 143 } 144 145 private void performAddAll(Set set, Collection c) { 146 TestRecord tr = testResult.getTestRecord("Performing addAll() : size = " + c.size()); 147 StopClock sc = new StopClock(); 148 sc.start(); 149 set.addAll(c); 150 sc.stop(); 151 tr.addResult(set.getClass().getName() + " : " + sc); 152 } 153 154 private Set createSequencialData(int size) { 155 return createSequencialData(0, size); 156 } 157 158 private Set createSequencialData(int init, int size) { 159 return createSequencialData(init, size, 1); 160 } 161 162 private Set createSequencialData(int init, int size, int step) { 163 HashSet set = new LinkedHashSet (); 164 while (size-- > 0) { 165 set.add(new ObjectID(init)); 166 init += step; 167 } 168 return set; 169 } 170 171 Random r = new Random (); 172 173 private Set createRandomData(int size) { 174 HashSet set = new LinkedHashSet (); 175 for (int j = 0; j < size; j++) { 176 set.add(new ObjectID(r.nextLong())); 177 } 178 return set; 179 } 180 181 private void performAddsOneAtATime(Set set, Collection c) { 182 TestRecord tr = testResult.getTestRecord("Performing add() : size = " + c.size()); 183 StopClock sc = new StopClock(); 184 for (Iterator i = c.iterator(); i.hasNext();) { 185 sc.start(); 186 set.add(i.next()); 187 sc.stop(); 188 } 189 tr.addResult(set.getClass().getName() + " : " + sc); 190 } 191 192 static class StopClock { 193 long cumulative; 194 long start; 195 long lastLap; 196 197 void start() { 198 start = System.currentTimeMillis(); 199 } 200 201 void stop() { 202 lastLap = System.currentTimeMillis() - start; 203 cumulative += lastLap; 204 } 205 206 void reset() { 207 cumulative = start = lastLap = 0; 208 } 209 210 public String dump() { 211 return "Time taken = " + cumulative + " ms : Last Lap = " + lastLap + " ms"; 212 } 213 214 public String toString() { 215 return "Time taken = " + cumulative + " ms"; 216 } 217 } 218 219 static class TestResults { 220 Map tests = new HashMap(); 221 222 public TestRecord getTestRecord(String testName) { 223 TestRecord tr = (TestRecord) tests.get(testName); 224 if (tr == null) { 225 tr = new TestRecord(testName); 226 tests.put(testName, tr); 227 } 228 return tr; 229 } 230 231 public void printResults() { 232 println(); 233 println("---------------------------------------------------------------------------------------"); 234 println(" TEST RESULTS"); 235 println("---------------------------------------------------------------------------------------"); 236 println(); 237 for (Iterator i = tests.values().iterator(); i.hasNext();) { 238 TestRecord tr = (TestRecord) i.next(); 239 tr.printResults(); 240 } 241 } 242 243 } 244 245 static class TestRecord { 246 247 private final String testName; 248 private final List results = new ArrayList (); 249 250 public TestRecord(String testName) { 251 this.testName = testName; 252 } 253 254 public void printResults() { 255 println(); 256 println(testName); 257 println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 258 for (Iterator i = results.iterator(); i.hasNext();) { 259 println(String.valueOf(i.next())); 260 } 261 } 262 263 public void addResult(String result) { 264 results.add(result); 265 } 266 267 } 268 } 269 | Popular Tags |