1 8 9 package net.sourceforge.chaperon.test; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 15 public class PerformanceTestCase extends TestCase 16 { 17 private int max1 = 5000; 18 private int max2 = 500000; 19 20 public PerformanceTestCase(String name) 21 { 22 super(name); 23 } 24 25 public void testFixedArray() throws Exception 26 { 27 System.out.println("------------------------------------ testFixedArray"); 28 runGC(); 29 30 long time = System.currentTimeMillis(); 31 long memory = usedMemory(); 32 33 Container[] array = new Container[0]; 34 for (int i = 0; i<max1; i++) 35 { 36 Container[] newArray = new Container[array.length+1]; 37 System.arraycopy(array, 0, newArray, 0, array.length); 38 newArray[array.length] = new Container(i); 39 array = newArray; 40 } 41 42 System.out.println("time = "+(System.currentTimeMillis()-time)+" ms"); 43 System.out.println("memory = "+(usedMemory()-memory)+" bytes"); 44 45 runGC(); 46 47 time = System.currentTimeMillis(); 48 49 long sum = 0; 50 for (int i = 0; i<array.length; i++) 51 sum += array[i].value; 52 53 System.out.println("time = "+(System.currentTimeMillis()-time)+" ms"); 54 } 55 56 public void testArray() throws Exception 57 { 58 System.out.println("------------------------------------ testArray"); 59 runGC(); 60 61 long time = System.currentTimeMillis(); 62 long memory = usedMemory(); 63 64 Container[] array = new Container[10]; 65 int count = 0; 66 for (int i = 0; i<max1; i++) 67 { 68 if (array.length==(count+1)) 69 { 70 Container[] newArray = new Container[array.length+10]; 71 System.arraycopy(array, 0, newArray, 0, array.length); 72 newArray[array.length] = new Container(i); 73 array = newArray; 74 } 75 76 array[count++] = new Container(i); 77 } 78 79 System.out.println("time = "+(System.currentTimeMillis()-time)+" ms"); 80 System.out.println("memory = "+(usedMemory()-memory)+" bytes"); 81 82 runGC(); 83 84 time = System.currentTimeMillis(); 85 86 long sum = 0; 87 for (int i = 0; i<count; i++) 88 sum += array[i].value; 89 90 System.out.println("time = "+(System.currentTimeMillis()-time)+" ms"); 91 } 92 93 public void testList() throws Exception 94 { 95 System.out.println("------------------------------------ testList"); 96 runGC(); 97 98 long time = System.currentTimeMillis(); 99 long memory = usedMemory(); 100 101 Container head = null; 102 for (int i = 0; i<max1; i++) 103 head = new Container(i, head); 104 105 System.out.println("time = "+(System.currentTimeMillis()-time)+" ms"); 106 System.out.println("memory = "+(usedMemory()-memory)+" bytes"); 107 108 runGC(); 109 110 time = System.currentTimeMillis(); 111 112 long sum = 0; 113 for (Container c = head; c!=null; c = c.previous) 114 sum += c.value; 115 116 System.out.println("time = "+(System.currentTimeMillis()-time)+" ms"); 117 } 118 119 public class Container 120 { 121 public Container previous; 122 public int value; 123 124 public Container(int value) 125 { 126 this.value = value; 127 } 128 129 public Container(int value, Container previous) 130 { 131 this.value = value; 132 this.previous = previous; 133 } 134 135 public void setPrevious(Container previous) 136 { 137 this.previous = previous; 138 } 139 140 public Container getPrevious() 141 { 142 return previous; 143 } 144 145 public void setValue(int value) 146 { 147 this.value = value; 148 } 149 150 public int getValue() 151 { 152 return value; 153 } 154 155 public String toString() 156 { 157 return String.valueOf(value); 158 } 159 } 160 161 public void testInheritConatiner() throws Exception 162 { 163 System.out.println("------------------------------------ testInheritConatiner"); 164 165 NumContainer head = null; 166 for (int i = 0; i<max2; i++) 167 { 168 long value = (int)(Math.random()*100.0); 169 if (value>50) 170 head = new LongContainer(value, head); 171 else 172 head = new IntContainer((int)value, head); 173 } 174 175 runGC(); 176 177 long time = System.currentTimeMillis(); 178 long memory = usedMemory(); 179 180 long sum = 0; 181 for (NumContainer c = head; c!=null; c = c.next) 182 { 183 if (c instanceof IntContainer) 184 sum += ((IntContainer)c).value; 185 else 186 sum += ((LongContainer)c).value; 187 } 188 189 System.out.println("time = "+(System.currentTimeMillis()-time)+" ms"); 190 System.out.println("memory = "+(usedMemory()-memory)+" bytes"); 191 } 192 193 public void testMixedConatiner() throws Exception 194 { 195 System.out.println("------------------------------------ testMixedConatiner"); 196 197 MixedContainer head = null; 198 for (int i = 0; i<max2; i++) 199 { 200 long value = (int)(Math.random()*100.0); 201 if (value>50) 202 head = new MixedContainer(value, head); 203 else 204 head = new MixedContainer((int)value, head); 205 } 206 207 runGC(); 208 209 long time = System.currentTimeMillis(); 210 long memory = usedMemory(); 211 212 long sum = 0; 213 for (MixedContainer c = head; c!=null; c = c.next) 214 { 215 if (c.isLong) 216 sum += c.longValue; 217 else 218 sum += c.intValue; 219 } 220 221 System.out.println("time = "+(System.currentTimeMillis()-time)+" ms"); 222 System.out.println("memory = "+(usedMemory()-memory)+" bytes"); 223 } 224 225 public void testBlind() throws Exception 226 { 227 System.out.println("------------------------------------ testBlind"); 228 runGC(); 229 230 long time = System.currentTimeMillis(); 231 long memory = usedMemory(); 232 233 long sum = 0; 234 for (int i = 0; i<max2; i++) 235 sum += i; 236 237 System.out.println("time = "+(System.currentTimeMillis()-time)+" ms"); 238 System.out.println("memory = "+(usedMemory()-memory)+" bytes"); 239 } 240 241 public abstract class NumContainer 242 { 243 public NumContainer next; 244 } 245 246 public class LongContainer extends NumContainer 247 { 248 public long value; 249 250 public LongContainer(long value, NumContainer next) 251 { 252 this.value = value; 253 this.next = next; 254 } 255 } 256 257 public class IntContainer extends NumContainer 258 { 259 public int value; 260 261 public IntContainer(int value, NumContainer next) 262 { 263 this.value = value; 264 this.next = next; 265 } 266 } 267 268 public class MixedContainer 269 { 270 public MixedContainer next; 271 public boolean isLong; 272 public long longValue; 273 public int intValue; 274 275 public MixedContainer(long value, MixedContainer next) 276 { 277 isLong = true; 278 this.longValue = value; 279 this.next = next; 280 } 281 282 public MixedContainer(int value, MixedContainer next) 283 { 284 isLong = false; 285 this.intValue = value; 286 this.next = next; 287 } 288 } 289 290 private static void runGC() throws Exception 291 { 292 for (int r = 0; r<4; ++r) 295 _runGC(); 296 } 297 298 private static void _runGC() throws Exception 299 { 300 long usedMem1 = usedMemory(); 301 long usedMem2 = Long.MAX_VALUE; 302 for (int i = 0; (usedMem1<usedMem2) && (i<500); ++i) 303 { 304 s_runtime.runFinalization(); 305 s_runtime.gc(); 306 Thread.yield(); 307 308 usedMem2 = usedMem1; 309 usedMem1 = usedMemory(); 310 } 311 } 312 313 private static long usedMemory() 314 { 315 return s_runtime.totalMemory()-s_runtime.freeMemory(); 316 } 317 318 private static final Runtime s_runtime = Runtime.getRuntime(); 319 320 public static Test suite() 321 { 322 return new TestSuite(PerformanceTestCase.class); 323 } 324 } 325 | Popular Tags |