001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 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.jface.tests.performance;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014:
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.jface.viewers.IContentProvider;
017: import org.eclipse.jface.viewers.ITreeContentProvider;
018: import org.eclipse.jface.viewers.StructuredViewer;
019: import org.eclipse.jface.viewers.TreeViewer;
020: import org.eclipse.jface.viewers.Viewer;
021: import org.eclipse.jface.viewers.ViewerSorter;
022: import org.eclipse.swt.widgets.Shell;
023: import org.eclipse.test.performance.Dimension;
024: import org.eclipse.ui.tests.performance.TestRunnable;
025:
026: public class TreeTest extends ViewerTest {
027:
028: TreeViewer viewer;
029:
030: static int TEST_COUNT = 1000;
031:
032: public TreeTest(String testName, int tagging) {
033: super (testName, tagging);
034: }
035:
036: public TreeTest(String testName) {
037: super (testName);
038: }
039:
040: /*
041: * (non-Javadoc)
042: *
043: * @see org.eclipse.jface.tests.performance.ViewerTest#createViewer(org.eclipse.swt.widgets.Shell)
044: */
045: protected StructuredViewer createViewer(Shell shell) {
046: viewer = new TreeViewer(browserShell);
047: viewer.setContentProvider(getContentProvider());
048: viewer.setLabelProvider(getLabelProvider());
049: viewer.setSorter(new ViewerSorter());
050: viewer.setUseHashlookup(true);
051: return viewer;
052: }
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see org.eclipse.jface.tests.performance.ViewerTest#getInitialInput()
058: */
059: protected Object getInitialInput() {
060: return new TestTreeElement(0, null);
061: }
062:
063: /**
064: * Get a content provider for the tree viewer.
065: *
066: * @return
067: */
068: private IContentProvider getContentProvider() {
069: return new ITreeContentProvider() {
070:
071: /*
072: * (non-Javadoc)
073: *
074: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
075: */
076: public Object[] getChildren(Object parentElement) {
077: TestTreeElement element = (TestTreeElement) parentElement;
078: return element.children;
079: }
080:
081: /*
082: * (non-Javadoc)
083: *
084: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
085: */
086: public Object getParent(Object element) {
087: return ((TestTreeElement) element).parent;
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
094: */
095: public boolean hasChildren(Object element) {
096: return ((TestTreeElement) element).children.length > 0;
097: }
098:
099: /*
100: * (non-Javadoc)
101: *
102: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
103: */
104: public Object[] getElements(Object inputElement) {
105: return getChildren(inputElement);
106: }
107:
108: /*
109: * (non-Javadoc)
110: *
111: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
112: */
113: public void dispose() {
114: // Do nothing here
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
121: * java.lang.Object, java.lang.Object)
122: */
123: public void inputChanged(Viewer localViewer,
124: Object oldInput, Object newInput) {
125: // Do nothing here
126: }
127:
128: };
129: }
130:
131: /**
132: * @throws CoreException
133: * Test addition to the tree one element at a time.
134: */
135: public void testAddOneAtATime() {
136: openBrowser();
137:
138: for (int i = 0; i < ITERATIONS / 10; i++) {
139: TestTreeElement input = new TestTreeElement(0, null);
140: viewer.setInput(input);
141: input.createChildren(TEST_COUNT);
142: processEvents();
143: startMeasuring();
144: for (int j = 0; j < input.children.length; j++) {
145:
146: viewer.add(input, input.children[j]);
147: processEvents();
148:
149: }
150: stopMeasuring();
151: }
152:
153: commitMeasurements();
154: assertPerformance();
155: }
156:
157: /**
158: * @throws CoreException
159: * Test addition to the tree one element at a time.
160: */
161: public void testAddTen() throws CoreException {
162:
163: doTestAdd(10, TEST_COUNT, false);
164: }
165:
166: /**
167: * @throws CoreException
168: * Test addition to the tree one element at a time.
169: */
170: public void testAddFifty() throws CoreException {
171:
172: doTestAdd(50, TEST_COUNT, false);
173: }
174:
175: /**
176: * @throws CoreException
177: * Test addition to the tree one element at a time.
178: */
179: public void testAddHundred() throws CoreException {
180:
181: tagIfNecessary(
182: "JFace - Add 1000 items in 10 blocks to TreeViewer",
183: Dimension.ELAPSED_PROCESS);
184: setDegradationComment("<a href=https://bugs.eclipse.org/bugs/show_bug.cgi?id=101853>See Bug 101853</a> ");
185:
186: doTestAdd(100, TEST_COUNT, false);
187: }
188:
189: /**
190: * Run the test for one of the fast insertions.
191: *
192: * @param count
193: * @throws CoreException
194: */
195: protected void doTestAdd(final int increment, final int total,
196: final boolean preSort) throws CoreException {
197:
198: openBrowser();
199:
200: exercise(new TestRunnable() {
201: public void run() {
202:
203: TestTreeElement input = new TestTreeElement(0, null);
204: viewer.setInput(input);
205: input.createChildren(total);
206: if (preSort)
207: viewer.getSorter().sort(viewer, input.children);
208: Collection batches = new ArrayList();
209: int blocks = input.children.length / increment;
210: for (int j = 0; j < blocks; j = j + increment) {
211: Object[] batch = new Object[increment];
212: System.arraycopy(input.children, j * increment,
213: batch, 0, increment);
214: batches.add(batch);
215: }
216: processEvents();
217: Object[] batchArray = batches.toArray();
218: startMeasuring();
219:
220: // Measure more than one for the fast cases
221: for (int k = 0; k < batchArray.length; k++) {
222: viewer.add(input, (Object[]) batchArray[k]);
223: processEvents();
224: }
225:
226: stopMeasuring();
227:
228: }
229: }, MIN_ITERATIONS, ITERATIONS, JFacePerformanceSuite.MAX_TIME);
230:
231: commitMeasurements();
232: assertPerformance();
233:
234: }
235:
236: /**
237: * Test addition to the tree.
238: */
239: public void testAddThousand() throws CoreException {
240: doTestAdd(1000, 2000, false);
241: }
242:
243: /**
244: * @throws CoreException
245: * Test addition to the tree one element at a time.
246: */
247: public void testAddTwoThousand() throws CoreException {
248:
249: doTestAdd(2000, 4000, false);
250:
251: }
252:
253: /**
254: * @throws CoreException
255: * Test addition to the tree with the items presorted.
256: */
257: public void testAddHundredPreSort() throws CoreException {
258:
259: doTestAdd(100, 1000, true);
260: }
261:
262: /**
263: * @throws CoreException
264: * Test addition to the tree with the items presorted.
265: */
266: public void testAddThousandPreSort() throws CoreException {
267: tagIfNecessary(
268: "JFace - Add 2000 items in 2 blocks to TreeViewer",
269: Dimension.ELAPSED_PROCESS);
270:
271: doTestAdd(1000, 2000, true);
272: }
273:
274: }
|