blob: 2f85254ce470d550523f9b4ad04ce029875475da [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ui.tests.performance;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.TypeNameRequestor;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.test.performance.Dimension;
import org.eclipse.test.performance.PerformanceTestCase;
public class JdtPerformanceTestCase extends PerformanceTestCase {
private static class Requestor extends TypeNameRequestor {
}
public JdtPerformanceTestCase() {
super();
}
public JdtPerformanceTestCase(String name) {
super(name);
}
protected void joinBackgroudActivities() throws CoreException {
// Join Building
boolean interrupted= true;
while (interrupted) {
try {
Platform.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null);
interrupted= false;
} catch (InterruptedException e) {
interrupted= true;
}
}
// Join indexing
new SearchEngine().searchAllTypeNames(
null,
"XXXXXXXXX".toCharArray(), // make sure we search a concrete name. This is faster according to Kent
SearchPattern.R_EXACT_MATCH,
IJavaSearchConstants.CLASS,
SearchEngine.createJavaSearchScope(new IJavaElement[0]),
new Requestor(),
IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
null);
// Join jobs. Maximal wait 1 minute before all jobs have completed
if(!joinJobs(0, 1 * 60 * 1000, 500)) {
JavaPlugin.logErrorMessage("Performance test " + getName() + " started with running background activity");
}
}
private static boolean joinJobs(long minTime, long maxTime, long intervalTime) {
long startTime= System.currentTimeMillis() + minTime;
runEventQueue();
while (System.currentTimeMillis() < startTime)
runEventQueue(intervalTime);
long endTime= maxTime > 0 && maxTime < Long.MAX_VALUE ? System.currentTimeMillis() + maxTime : Long.MAX_VALUE;
boolean calm= allJobsQuiet();
while (!calm && System.currentTimeMillis() < endTime) {
runEventQueue(intervalTime);
calm= allJobsQuiet();
}
return calm;
}
private static void sleep(int intervalTime) {
try {
Thread.sleep(intervalTime);
} catch (InterruptedException e) {
}
}
private static boolean allJobsQuiet() {
IJobManager jobManager= Platform.getJobManager();
Job[] jobs= jobManager.find(null);
for (int i= 0; i < jobs.length; i++) {
Job job= jobs[i];
int state= job.getState();
if (state == Job.RUNNING || state == Job.WAITING)
return false;
}
return true;
}
private static void runEventQueue() {
IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null)
runEventQueue(window.getShell());
}
private static void runEventQueue(Shell shell) {
while (shell.getDisplay().readAndDispatch()) {
// do nothing
}
}
private static void runEventQueue(long minTime) {
long nextCheck= System.currentTimeMillis() + minTime;
while (System.currentTimeMillis() < nextCheck) {
runEventQueue();
sleep(1);
}
}
protected void finishMeasurements() {
stopMeasuring();
commitMeasurements();
assertPerformanceInRelativeBand(Dimension.ELAPSED_PROCESS, -100, +10);
}
}