001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.tests.performance.layout;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.graphics.Point;
015: import org.eclipse.swt.widgets.Composite;
016: import org.eclipse.ui.WorkbenchException;
017: import org.eclipse.ui.tests.performance.BasicPerformanceTest;
018:
019: /**
020: * Measures the performance of a widget's computeSize method
021: *
022: * @since 3.1
023: */
024: public class ComputeSizeTest extends BasicPerformanceTest {
025:
026: private TestWidgetFactory widgetFactory;
027: private int xIterations = 10;
028: private int yIterations = 10;
029:
030: /**
031: * @param testName
032: */
033: public ComputeSizeTest(TestWidgetFactory widgetFactory) {
034: super (widgetFactory.getName() + " computeSize");
035:
036: this .widgetFactory = widgetFactory;
037: }
038:
039: /**
040: * Run the test
041: */
042: protected void runTest() throws CoreException, WorkbenchException {
043:
044: widgetFactory.init();
045: final Composite widget = widgetFactory.getControl();
046: //Rectangle initialBounds = widget.getBounds();
047: final Point maxSize = widgetFactory.getMaxSize();
048:
049: // Iteration counter. We increment this each pass through the loop in order to
050: // generate slightly different test data each time
051: final int[] counter = new int[] { 0 };
052:
053: for (int j = 0; j < 100; j++) {
054: // This counter determines whether we're computing a width,
055: // height, or fixed
056: // size and whether or not we flush the cache.
057:
058: // We do things this way to avoid calling computeSize with the same (or
059: // similar) values
060: // twice in a row, which would be too easy to cache.
061: int count = counter[0];
062:
063: startMeasuring();
064: for (int i = 0; i < 200; i++) {
065:
066: for (int xIteration = 0; xIteration < xIterations; xIteration++) {
067:
068: for (int yIteration = 0; yIteration < yIterations; yIteration++) {
069: // Avoid giving the same x value twice in a row in order to make it hard to cache
070: int xSize = maxSize.x
071: * ((xIteration + yIteration) % xIterations)
072: / xIterations;
073: int ySize = maxSize.y * yIteration
074: / yIterations;
075:
076: // Alternate between flushing and not flushing the cache
077: boolean flushState = (count % 2) != 0;
078:
079: // Alternate between width, height, and fixed, and default size queries
080: // (note: we need to alternate in order to make the result hard to cache)
081: switch (count % 4) {
082: case 0:
083: widget.computeSize(xSize, SWT.DEFAULT,
084: flushState);
085: break;
086: case 1:
087: widget.computeSize(SWT.DEFAULT, ySize,
088: flushState);
089: break;
090: case 2:
091: widget
092: .computeSize(xSize, ySize,
093: flushState);
094: break;
095: case 3:
096: widget.computeSize(SWT.DEFAULT,
097: SWT.DEFAULT, flushState);
098: break;
099: }
100:
101: count++;
102: }
103: }
104:
105: }
106: stopMeasuring();
107: processEvents();
108: counter[0]++;
109: }
110:
111: commitMeasurements();
112: assertPerformance();
113: widgetFactory.done();
114: }
115: }
|