Skip to content

Commit

Permalink
Enable JUnit 4 in performance tests via PeformanceTestRunner eclipse-…
Browse files Browse the repository at this point in the history
…platform#903

* Allow test() method in PerformanceTestRunner to throw exceptions
* Provide an additional run() method in PerformanceTestRunner that does
not rely on a JUnit 3 TestCase but a class and a test method name to be
passed
* Demonstrate migration of performance tests to JUnit 4 at the example
of ProjectSnapshotPerfManualTest

Contributes to
eclipse-platform#903
  • Loading branch information
HeikoKlare authored and Michael5601 committed Feb 12, 2024
1 parent 9ec5274 commit 199975a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createInWorkspace;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.net.URI;
Expand All @@ -31,9 +32,8 @@
import org.eclipse.core.runtime.Path;
import org.eclipse.core.tests.harness.PerformanceTestRunner;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Measure speed of a project "Import with Snapshot" operation compared
Expand All @@ -44,8 +44,11 @@
* to be available on a slow file system (bigSiteLocation). Modify
* bigSiteLocation to suit your needs, then run-as > JUnit Plug-in Test.
*/
@RunWith(JUnit4.class)
public class ProjectSnapshotPerfManualTest extends ResourceTest {
public class ProjectSnapshotPerfManualTest {

@Rule
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();

/** big site default volume (windows) */
public static final String bigSiteDevice = "c:";

Expand All @@ -58,14 +61,6 @@ public class ProjectSnapshotPerfManualTest extends ResourceTest {
/** location of refresh snapshot file */
private static final String REFRESH_SNAPSHOT_FILE_LOCATION = ".settings/resource-index.zip";

public ProjectSnapshotPerfManualTest() {
super();
}

public ProjectSnapshotPerfManualTest(String name) {
super(name);
}

protected int countChildren(File root) {
String[] children = root.list();
if (children == null) {
Expand Down Expand Up @@ -107,14 +102,10 @@ public void testSnapshotImportPerformance() throws Exception {
// open the project, timing the initial refresh
new PerformanceTestRunner() {
@Override
protected void test() {
try {
project.open(null);
} catch (CoreException e) {
fail("Original open", e);
}
protected void test() throws CoreException {
project.open(null);
}
}.run(new ProjectSnapshotPerfManualTest("Original open"), 1, 1);
}.run(getClass(), "Original open", 1, 1);

// dump the snapshot refresh info
createInWorkspace(project.getFolder(DIR_NAME));
Expand All @@ -131,36 +122,28 @@ protected void test() {
project.create(description, null);
new PerformanceTestRunner() {
@Override
protected void test() {
try {
project.loadSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null);
project.open(IResource.NONE, null);
} catch (CoreException e) {
fail("Snapshot open", e);
}
protected void test() throws CoreException {
project.loadSnapshot(IProject.SNAPSHOT_TREE, snapshotLocation, null);
project.open(IResource.NONE, null);
}
}.run(new ProjectSnapshotPerfManualTest("Snapshot open"), 1, 1);
}.run(getClass(), "Snapshot open", 1, 1);

// now refresh the project, verifying zero resource delta
// (except for the creation of .settings/resource-index.zip)
final ResourceDeltaVerifier[] verifier = new ResourceDeltaVerifier[1];
new PerformanceTestRunner() {
@Override
protected void test() {
try {
verifier[0] = new ResourceDeltaVerifier();
ResourcesPlugin.getWorkspace().addResourceChangeListener(verifier[0]);
verifier[0].reset();
IFolder settings = project.getFolder(DIR_NAME);
IFile snapshot = project.getFile(REFRESH_SNAPSHOT_FILE_LOCATION);
verifier[0].addExpectedChange(settings, IResourceDelta.CHANGED, 0);
verifier[0].addExpectedChange(snapshot, IResourceDelta.ADDED, 0);
project.refreshLocal(IResource.DEPTH_INFINITE, null);
} catch (CoreException e) {
fail("Forced refresh only", e);
}
protected void test() throws CoreException {
verifier[0] = new ResourceDeltaVerifier();
ResourcesPlugin.getWorkspace().addResourceChangeListener(verifier[0]);
verifier[0].reset();
IFolder settings = project.getFolder(DIR_NAME);
IFile snapshot = project.getFile(REFRESH_SNAPSHOT_FILE_LOCATION);
verifier[0].addExpectedChange(settings, IResourceDelta.CHANGED, 0);
verifier[0].addExpectedChange(snapshot, IResourceDelta.ADDED, 0);
project.refreshLocal(IResource.DEPTH_INFINITE, null);
}
}.run(new ProjectSnapshotPerfManualTest("Forced refresh only"), 1, 1);
}.run(getClass(), "Forced refresh only", 1, 1);
verifier[0].verifyDelta(null);
assertTrue("1.0 " + verifier[0].getMessage(), verifier[0].isDeltaValid());

Expand All @@ -174,17 +157,14 @@ protected void test() {
project.create(description, null);
new PerformanceTestRunner() {
@Override
protected void test() {
try {
project.open(null);
} catch (CoreException e) {
fail("Second refresh open", e);
}
protected void test() throws CoreException {
project.open(null);
}
}.run(new ProjectSnapshotPerfManualTest("Second refresh open"), 1, 1);
}.run(getClass(), "Second refresh open", 1, 1);

// delete project to avoid getting content deleted in tearDown()
project.close(null);
project.delete(false, false, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void setRegressionReason(String comment) {
/**
* Implemented by subclasses to perform the work to be measured.
*/
protected abstract void test();
protected abstract void test() throws CoreException;

/**
* Executes the performance test the given number of times. Use the outer time
Expand Down Expand Up @@ -67,6 +67,36 @@ public final void run(TestCase testCase, int outer, int inner) {
public final void run(TestCase testCase, String localName, int outer, int inner) {
Performance perf = Performance.getDefault();
PerformanceMeter meter = perf.createPerformanceMeter(perf.getDefaultScenarioId(testCase));
try {
runTest(meter, localName, outer, inner);
} catch (Exception e) {
CoreTest.fail("Failed performance test", e);
}
}

/**
* Executes the performance test the given number of times. Use the outer time
* to execute the test several times in order to obtain a normalized average.
* Use the inner loop for very fast tests that would otherwise be difficult to
* measure due to Java's poor timer granularity. The inner loop is not needed
* for long tests that typically take more than a second to execute.
*
* @param testClass The test class that is currently executed (used to
* obtain an appropriate meter)
* @param testMethodName The test method name (or some other identifier) that is
* currently executed (used to obtain an appropriate
* meter)
* @param outer The number of repetitions of the test.
* @param inner The number of repetitions within the performance timer.
*/
public final void run(Class<?> testClass, String testMethodName, int outer, int inner) throws CoreException {
Performance perf = Performance.getDefault();
PerformanceMeter meter = perf.createPerformanceMeter(perf.getDefaultScenarioId(testClass, testMethodName));
runTest(meter, null, outer, inner);
}

private void runTest(PerformanceMeter meter, String localName, int outer, int inner) throws CoreException {
Performance perf = Performance.getDefault();
if (regressionReason != null) {
perf.setComment(meter, Performance.EXPLAINS_DEGRADATION_COMMENT, regressionReason);
}
Expand All @@ -81,15 +111,13 @@ public final void run(TestCase testCase, String localName, int outer, int inner)
tearDown();
}
if (localName != null) {
Performance.getDefault().tagAsSummary(meter, localName, Dimension.ELAPSED_PROCESS);
perf.tagAsSummary(meter, localName, Dimension.ELAPSED_PROCESS);
}
if (fingerprintName != null) {
perf.tagAsSummary(meter, fingerprintName, Dimension.ELAPSED_PROCESS);
}
meter.commit();
perf.assertPerformance(meter);
} catch (CoreException e) {
CoreTest.fail("Failed performance test", e);
} finally {
meter.dispose();
}
Expand Down

0 comments on commit 199975a

Please sign in to comment.