001: /*******************************************************************************
002: * Copyright (c) 2005, 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.jface.tests.performance;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.layout.FillLayout;
014: import org.eclipse.swt.widgets.Display;
015: import org.eclipse.swt.widgets.List;
016: import org.eclipse.swt.widgets.Shell;
017: import org.eclipse.ui.tests.performance.BasicPerformanceTest;
018: import org.eclipse.ui.tests.performance.TestRunnable;
019:
020: /**
021: * The ListPopulationTest is the test for simple
022: * SWT lists.
023: *
024: */
025: public class ListPopulationTest extends BasicPerformanceTest {
026:
027: List list;
028:
029: public ListPopulationTest(String testName, int tagging) {
030: super (testName, tagging);
031: }
032:
033: public ListPopulationTest(String testName) {
034: super (testName);
035: }
036:
037: protected void openBrowser() {
038: Display fDisplay = Display.getCurrent();
039: if (fDisplay == null) {
040: fDisplay = new Display();
041: }
042: Shell shell = new Shell(fDisplay);
043: shell.setSize(500, 500);
044: shell.setLayout(new FillLayout());
045: list = new List(shell, SWT.NONE);
046: shell.open();
047: // processEvents();
048: }
049:
050: public void testSmallAdd() throws Throwable {
051: addBench(100);
052: }
053:
054: public void testSmallSetItems() throws Throwable {
055: setItemsBench(100);
056: }
057:
058: public void testMediumAdd() throws Throwable {
059: addBench(5000);
060: }
061:
062: public void testMediumSetItems() throws Throwable {
063: setItemsBench(5000);
064: }
065:
066: public void testLargeAdd() throws Throwable {
067: addBench(50000);
068: }
069:
070: public void testLargeSetItems() throws Throwable {
071: setItemsBench(50000);
072: }
073:
074: /**
075: * Test the time for adding elements using add.
076: * @throws Throwable
077: */
078: public void addBench(int count) throws Throwable {
079: openBrowser();
080: final String[] items = getItems(count);
081:
082: exercise(new TestRunnable() {
083: public void run() {
084: list.removeAll();
085: startMeasuring();
086: for (int j = 0; j < items.length; j++) {
087: list.add(items[j]);
088: }
089: processEvents();
090: stopMeasuring();
091: }
092: });
093:
094: commitMeasurements();
095: assertPerformance();
096: }
097:
098: /**
099: * Test the time for adding elements using setItem.
100: * @throws Throwable
101: */
102: public void setItemsBench(int count) throws Throwable {
103: openBrowser();
104: final String[] items = getItems(count);
105: exercise(new TestRunnable() {
106: public void run() {
107: list.removeAll();
108: startMeasuring();
109: list.setItems(items);
110: processEvents();
111: stopMeasuring();
112: }
113: });
114:
115: commitMeasurements();
116: assertPerformance();
117: }
118:
119: /**
120: * Get count number of items.
121: * @param count
122: * @return
123: */
124: private String[] getItems(int count) {
125: String[] items = new String[count];
126: for (int j = 0; j < items.length; j++) {
127: items[j] = "Element " + String.valueOf(j);
128:
129: }
130: return items;
131: }
132:
133: }
|