001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.test;
010:
011: import junit.framework.Test;
012: import junit.framework.TestCase;
013: import junit.framework.TestSuite;
014:
015: public class PerformanceTestCase extends TestCase {
016: private int max1 = 5000;
017: private int max2 = 500000;
018:
019: public PerformanceTestCase(String name) {
020: super (name);
021: }
022:
023: public void testFixedArray() throws Exception {
024: System.out
025: .println("------------------------------------ testFixedArray");
026: runGC();
027:
028: long time = System.currentTimeMillis();
029: long memory = usedMemory();
030:
031: Container[] array = new Container[0];
032: for (int i = 0; i < max1; i++) {
033: Container[] newArray = new Container[array.length + 1];
034: System.arraycopy(array, 0, newArray, 0, array.length);
035: newArray[array.length] = new Container(i);
036: array = newArray;
037: }
038:
039: System.out.println("time = "
040: + (System.currentTimeMillis() - time) + " ms");
041: System.out.println("memory = " + (usedMemory() - memory)
042: + " bytes");
043:
044: runGC();
045:
046: time = System.currentTimeMillis();
047:
048: long sum = 0;
049: for (int i = 0; i < array.length; i++)
050: sum += array[i].value;
051:
052: System.out.println("time = "
053: + (System.currentTimeMillis() - time) + " ms");
054: }
055:
056: public void testArray() throws Exception {
057: System.out
058: .println("------------------------------------ testArray");
059: runGC();
060:
061: long time = System.currentTimeMillis();
062: long memory = usedMemory();
063:
064: Container[] array = new Container[10];
065: int count = 0;
066: for (int i = 0; i < max1; i++) {
067: if (array.length == (count + 1)) {
068: Container[] newArray = new Container[array.length + 10];
069: System.arraycopy(array, 0, newArray, 0, array.length);
070: newArray[array.length] = new Container(i);
071: array = newArray;
072: }
073:
074: array[count++] = new Container(i);
075: }
076:
077: System.out.println("time = "
078: + (System.currentTimeMillis() - time) + " ms");
079: System.out.println("memory = " + (usedMemory() - memory)
080: + " bytes");
081:
082: runGC();
083:
084: time = System.currentTimeMillis();
085:
086: long sum = 0;
087: for (int i = 0; i < count; i++)
088: sum += array[i].value;
089:
090: System.out.println("time = "
091: + (System.currentTimeMillis() - time) + " ms");
092: }
093:
094: public void testList() throws Exception {
095: System.out
096: .println("------------------------------------ testList");
097: runGC();
098:
099: long time = System.currentTimeMillis();
100: long memory = usedMemory();
101:
102: Container head = null;
103: for (int i = 0; i < max1; i++)
104: head = new Container(i, head);
105:
106: System.out.println("time = "
107: + (System.currentTimeMillis() - time) + " ms");
108: System.out.println("memory = " + (usedMemory() - memory)
109: + " bytes");
110:
111: runGC();
112:
113: time = System.currentTimeMillis();
114:
115: long sum = 0;
116: for (Container c = head; c != null; c = c.previous)
117: sum += c.value;
118:
119: System.out.println("time = "
120: + (System.currentTimeMillis() - time) + " ms");
121: }
122:
123: public class Container {
124: public Container previous;
125: public int value;
126:
127: public Container(int value) {
128: this .value = value;
129: }
130:
131: public Container(int value, Container previous) {
132: this .value = value;
133: this .previous = previous;
134: }
135:
136: public void setPrevious(Container previous) {
137: this .previous = previous;
138: }
139:
140: public Container getPrevious() {
141: return previous;
142: }
143:
144: public void setValue(int value) {
145: this .value = value;
146: }
147:
148: public int getValue() {
149: return value;
150: }
151:
152: public String toString() {
153: return String.valueOf(value);
154: }
155: }
156:
157: public void testInheritConatiner() throws Exception {
158: System.out
159: .println("------------------------------------ testInheritConatiner");
160:
161: NumContainer head = null;
162: for (int i = 0; i < max2; i++) {
163: long value = (int) (Math.random() * 100.0);
164: if (value > 50)
165: head = new LongContainer(value, head);
166: else
167: head = new IntContainer((int) value, head);
168: }
169:
170: runGC();
171:
172: long time = System.currentTimeMillis();
173: long memory = usedMemory();
174:
175: long sum = 0;
176: for (NumContainer c = head; c != null; c = c.next) {
177: if (c instanceof IntContainer)
178: sum += ((IntContainer) c).value;
179: else
180: sum += ((LongContainer) c).value;
181: }
182:
183: System.out.println("time = "
184: + (System.currentTimeMillis() - time) + " ms");
185: System.out.println("memory = " + (usedMemory() - memory)
186: + " bytes");
187: }
188:
189: public void testMixedConatiner() throws Exception {
190: System.out
191: .println("------------------------------------ testMixedConatiner");
192:
193: MixedContainer head = null;
194: for (int i = 0; i < max2; i++) {
195: long value = (int) (Math.random() * 100.0);
196: if (value > 50)
197: head = new MixedContainer(value, head);
198: else
199: head = new MixedContainer((int) value, head);
200: }
201:
202: runGC();
203:
204: long time = System.currentTimeMillis();
205: long memory = usedMemory();
206:
207: long sum = 0;
208: for (MixedContainer c = head; c != null; c = c.next) {
209: if (c.isLong)
210: sum += c.longValue;
211: else
212: sum += c.intValue;
213: }
214:
215: System.out.println("time = "
216: + (System.currentTimeMillis() - time) + " ms");
217: System.out.println("memory = " + (usedMemory() - memory)
218: + " bytes");
219: }
220:
221: public void testBlind() throws Exception {
222: System.out
223: .println("------------------------------------ testBlind");
224: runGC();
225:
226: long time = System.currentTimeMillis();
227: long memory = usedMemory();
228:
229: long sum = 0;
230: for (int i = 0; i < max2; i++)
231: sum += i;
232:
233: System.out.println("time = "
234: + (System.currentTimeMillis() - time) + " ms");
235: System.out.println("memory = " + (usedMemory() - memory)
236: + " bytes");
237: }
238:
239: public abstract class NumContainer {
240: public NumContainer next;
241: }
242:
243: public class LongContainer extends NumContainer {
244: public long value;
245:
246: public LongContainer(long value, NumContainer next) {
247: this .value = value;
248: this .next = next;
249: }
250: }
251:
252: public class IntContainer extends NumContainer {
253: public int value;
254:
255: public IntContainer(int value, NumContainer next) {
256: this .value = value;
257: this .next = next;
258: }
259: }
260:
261: public class MixedContainer {
262: public MixedContainer next;
263: public boolean isLong;
264: public long longValue;
265: public int intValue;
266:
267: public MixedContainer(long value, MixedContainer next) {
268: isLong = true;
269: this .longValue = value;
270: this .next = next;
271: }
272:
273: public MixedContainer(int value, MixedContainer next) {
274: isLong = false;
275: this .intValue = value;
276: this .next = next;
277: }
278: }
279:
280: private static void runGC() throws Exception {
281: // It helps to call Runtime.gc()
282: // using several method calls:
283: for (int r = 0; r < 4; ++r)
284: _runGC();
285: }
286:
287: private static void _runGC() throws Exception {
288: long usedMem1 = usedMemory();
289: long usedMem2 = Long.MAX_VALUE;
290: for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) {
291: s_runtime.runFinalization();
292: s_runtime.gc();
293: Thread.yield();
294:
295: usedMem2 = usedMem1;
296: usedMem1 = usedMemory();
297: }
298: }
299:
300: private static long usedMemory() {
301: return s_runtime.totalMemory() - s_runtime.freeMemory();
302: }
303:
304: private static final Runtime s_runtime = Runtime.getRuntime();
305:
306: public static Test suite() {
307: return new TestSuite(PerformanceTestCase.class);
308: }
309: }
|