File: PerformanceTests.java

package info (click to toggle)
piccolo 1.2-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,668 kB
  • sloc: java: 16,496; xml: 259; sh: 18; makefile: 13
file content (359 lines) | stat: -rw-r--r-- 10,300 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;

import junit.framework.TestCase;

import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.util.PAffineTransform;
import edu.umd.cs.piccolo.util.PBounds;

public class PerformanceTests extends TestCase {
	
	private static PerformanceLog log = new PerformanceLog();
	private static int NUMBER_NODES = 20000;

	public PerformanceTests(String name) {
		super(name);
	}
		
	public void testRunPerformanceTests() {
		// three times to warm up JVM
		for (int i = 0; i < 3; i++) {
			addNodes();
			copyNodes();
			createNodes();
			createPaths();
			fullIntersectsNodes();
			memorySizeOfNodes();
			//removeNodes();
			translateNodes();
			costOfNoBoundsCache();
//			renderSpeed();
			if (i != 2) {
				log.clear();
			}
		}
		log.writeLog();
	}
	
	public void createNodes() { 	
		PNode[] nodes = new PNode[NUMBER_NODES];

		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i] = new PNode();
		}		
		log.endTest("Create " + NUMBER_NODES + " new nodes");		
	}
	
	public void createPaths() { 	
		PNode[] nodes = new PNode[NUMBER_NODES];

		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i] = PPath.createRectangle(0, 0, 100, 80);
		}		
		log.endTest("Create " + NUMBER_NODES + " new rect paths");		
		
		Random r = new Random();
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i].translate(r.nextFloat() * 300, r.nextFloat() * 300);
		}		
	}
	
	public void addNodes() {
		PNode parent = new PNode();
		PNode[] nodes = new PNode[NUMBER_NODES];

		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i] = new PNode();
		}		
		
		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			parent.addChild(nodes[i]);
		}		
		log.endTest("Add " + NUMBER_NODES + " nodes to a new parent");
	}
	
	public void removeNodes() {
		PNode parent = new PNode();
		PNode[] nodes = new PNode[NUMBER_NODES];
		ArrayList list = new ArrayList();

		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i] = new PNode();
		}		
		
		for (int i = 0; i < NUMBER_NODES; i++) {
			parent.addChild(nodes[i]);
			list.add(nodes[i]);
		}		

		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {	
			parent.removeChild(nodes[i]);	
		}
		log.endTest("Remove " + NUMBER_NODES + " nodes using removeChild() front to back");
		
		parent.addChildren(list);
				
		log.startTest();
		for (int i = NUMBER_NODES - 1; i >= 0; i--) {	
			parent.removeChild(i);
		}
		log.endTest("Remove " + NUMBER_NODES + " nodes using removeChild() back to front by index");

		log.startTest();
//		for (int i = NUMBER_NODES - 1; i >= 0; i--) {	
//			parent.removeChild(nodes[i]);	
//		}
		log.endTest("Remove " + NUMBER_NODES + " nodes using removeChild() back to front by object, TO_SLOW");

		parent.addChildren(list);
				
		log.startTest();
		parent.removeChildren(list);		
		log.endTest("Remove " + NUMBER_NODES + " nodes using removeChildren()");

		parent.addChildren(list);
				
		log.startTest();
		parent.removeAllChildren(); 	
		log.endTest("Remove " + NUMBER_NODES + " nodes using removeAllChildren()");
	}

	public void translateNodes() {
		PNode parent = new PNode();
		PNode[] nodes = new PNode[NUMBER_NODES];
		PBounds b = new PBounds();
		Random r = new Random();
		
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i] = new PNode();
			nodes[i].setBounds(1000 * r.nextFloat(), 1000 * r.nextFloat(), 100, 80);
			parent.addChild(nodes[i]);
			nodes[i].getFullBoundsReference();
		}
		
		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i].translate(1000 * r.nextFloat(), 1000 * r.nextFloat());
			nodes[i].scale(1000 * r.nextFloat());			
//			nodes[i].translateBy(100.01, 100.2);
//			nodes[i].scaleBy(0.9);
		}		
		log.endTest("Translate " + NUMBER_NODES + " nodes, not counting repaint or validate layout");
				
		log.startTest();
		//parent.validateFullBounds(); now protected.
		parent.getFullBoundsReference(); // calls validateFullBounds as a side effect.
		log.endTest("Validate Layout after translate " + NUMBER_NODES + " nodes");
		
		log.startTest();
		parent.validateFullPaint();
		log.endTest("Validate Paint after translate " + NUMBER_NODES + " nodes");
		
		log.startTest();
		parent.computeFullBounds(b);
		log.endTest("Parent compute bounds of " + NUMBER_NODES + " children nodes");
	}
	
	public void fullIntersectsNodes() {
		PNode parent = new PNode();
		PNode[] nodes = new PNode[NUMBER_NODES];
		PBounds b = new PBounds(0, 50, 100, 20);
		
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i] = new PNode();
			parent.addChild(nodes[i]);
		}
		
		//parent.validateFullBounds(); // now protected
		parent.getFullBoundsReference(); // calls validateFullBounds as a side effect.
		
		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i].fullIntersects(b);
		}		
		log.endTest("Do fullIntersects test for " + NUMBER_NODES + " nodes");
	}
	
	public void memorySizeOfNodes() {
		PNode[] nodes = new PNode[NUMBER_NODES];
		Runtime.getRuntime().gc();
		long startTotalMemory = Runtime.getRuntime().totalMemory();
		long startFree = Runtime.getRuntime().freeMemory();
		long endFree;
		long endTotal;
		
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i] = new PNode();
		}
		
		Runtime.getRuntime().gc();
		endFree = Runtime.getRuntime().freeMemory();
		endTotal = Runtime.getRuntime().totalMemory();
		
		log.addEntry("Approximate k used by " + NUMBER_NODES + " nodes", ((endTotal - startTotalMemory) + (startFree - endFree)) / 1024);
		nodes[0].getPaint();
	}
	
	public void copyNodes() {
		PNode parent = new PNode();
		PNode[] nodes = new PNode[NUMBER_NODES];

		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i] = new PNode();
			parent.addChild(nodes[i]);
		}		
		
		log.startTest();
		parent.clone();
		log.endTest("Copy/Serialize " + NUMBER_NODES + " nodes");		
	}	
	
	public void costOfNoBoundsCache() {
		PNode[] nodes = new PNode[NUMBER_NODES];
		PBounds[] bounds = new PBounds[NUMBER_NODES];
		PBounds pickRect = new PBounds(0, 0, 1, 1);
		Random r = new Random();
				
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i] = new PNode();
			nodes[i].translate(1000 * r.nextFloat(), 1000 * r.nextFloat());
			nodes[i].scale(1000 * r.nextFloat());	
			bounds[i] = new PBounds(1000 * r.nextFloat(), 1000 * r.nextFloat(), 100, 80);
		}

		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			bounds[i].intersects(pickRect);
		}		
		log.endTest("Do intersects test for " + NUMBER_NODES + " bounds");
			
		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			nodes[i].localToParent(bounds[i]);
		}		
		log.endTest("Transform " + NUMBER_NODES + " bounds from local to parent");
		
		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			pickRect.add(bounds[i]);
		}		
		log.endTest("Sum " + NUMBER_NODES + " bounds");
		
		PBounds b = new PBounds(r.nextDouble(), r.nextDouble(), r.nextDouble(), r.nextDouble());
		log.startTest();
		for (int i = 0; i < NUMBER_NODES * 10; i++) {
			b.clone();
		}				
		log.endTest("Clone " + NUMBER_NODES * 10 + " PBounds");

	}
	
	public void renderSpeed() {
		Random r = new Random();
		PAffineTransform at = new PAffineTransform();
		at.setScale(r.nextFloat());
		at.translate(r.nextFloat(), r.nextFloat());

		try {
			log.startTest();
			for (int i = 0; i < NUMBER_NODES; i++) {
				at.createInverse();
			}		
			log.endTest("Create inverse transform " + NUMBER_NODES + " times");
		} catch (NoninvertibleTransformException e) {
		}

		int height = 400;
		int width = 400;

		double scale1 = 0.5;
		double scale2 = 2;
		boolean scaleFlip = true;

		PAffineTransform transorm1 = new PAffineTransform();
		//transorm1.scale(0.5, 0.5);
		transorm1.translate(0.5, 10.1);
		PAffineTransform transorm2 = null;
		
		try {
			transorm2 = new PAffineTransform(transorm1.createInverse());
		} catch (NoninvertibleTransformException e) {}
			
			
		GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		BufferedImage result = (BufferedImage)graphicsConfiguration.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
		Graphics2D g2 = result.createGraphics();

		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			if (scaleFlip) {
				g2.scale(scale2, scale2);
				scaleFlip = !scaleFlip;
			} else {
				g2.scale(scale1, scale1);
				scaleFlip = !scaleFlip;
			}
		}		
		log.endTest("Scale graphics context " + NUMBER_NODES + " times");

		g2.setTransform(new AffineTransform());
		
		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			g2.translate(0.5, 0.5);
		}		
		log.endTest("Translate graphics context " + NUMBER_NODES + " times");

		g2.setTransform(new AffineTransform());

		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			if (scaleFlip) {
				g2.transform(transorm1);
				scaleFlip = !scaleFlip;
			} else {
				g2.transform(transorm2);
				scaleFlip = !scaleFlip;
			}
		}		
		log.endTest("Transform graphics context " + NUMBER_NODES + " times");		

	
		Rectangle2D rect = new Rectangle2D.Double(0, 0, 100, 80);
		GeneralPath path = new GeneralPath(rect);
		
		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			g2.fill(rect);
		}		
		log.endTest("Fill " + NUMBER_NODES + " rects"); 	

		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			g2.getTransform().getScaleX();
		}		
		log.endTest("Call g2.getTransform() " + NUMBER_NODES + " times");		

		
		log.startTest();
		for (int i = 0; i < NUMBER_NODES; i++) {
			g2.fill(path);
		}		
		log.endTest("Fill " + NUMBER_NODES + " paths"); 	
	}	
}