Skip to content

Commit

Permalink
Improve performance test framework by adding time and size factors
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-jts committed Jan 31, 2025
1 parent d58ca0c commit 027df7d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
53 changes: 44 additions & 9 deletions modules/core/src/test/java/test/jts/perf/PerformanceTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@
* A base class for classes implementing performance tests
* to be run by the {@link PerformanceTestRunner}.
* <p>
* In a subclass of this class,
* all public methods which start with <code>run</code> are
* executed as performance tests.
* The {@link #setUp()} is called at the start of test class execution,
* and {@link #tearDown()} is called at the end.
* These allow creating and release resources needed for testing
* (e.g. a database connection).
* <p>
* Multiple test runs with different run sizes may be made.
* Within each run, each <code>run</code> method is executed
* the specified number of iterations.
* The time to run the method is printed for each one.
* Subclasses provide performance tests as
* public methods which start with <code>run</code>.
* Each test is executed once per test run.
* The number of runs is determined by the length
* of the array provided to {@link #setRunSize(int[])}.
* The array entry for each run specifies a size for the run.
* The size is provided at the start of each run via {@link #startRun(int)}.
* It can be used to determine the size of data to generate for the run.
* <p>
* Within a run, each <code>run</code> test method is executed
* for the number of iterations specified by {@link #setRunIterations(int).
* This allows running tests of fast operations long enough for accurate timing.
* A performance report is printed after each test.
* <p>
* {@link #endRun()} is called at the end of the run.
*
* @author Martin Davis
*
Expand All @@ -40,14 +52,19 @@ public PerformanceTestCase(String name)
this.name = name;
}

/**
* Gets the name of this test case.
*
* @return the name of the test case
*/
public String getName()
{
return name;
}

/**
* Sets the size(s) for the runs of the test.
* The default is 1 iteration.
* Sets the size(s) for the runs of the test(s).
* The default is one run with a size of 1.
*
* @param runSize a list of the sizes for the test runs
*/
Expand All @@ -57,18 +74,31 @@ protected void setRunSize(int[] runSize)
runTime = new long[runSize.length];
}

/**
* Gets the array of run sizes.
*
* @return the array of run sizes
*/
public int[] getRunSize()
{
return runSize;
}

/**
* Gets the run times for the final run method in each run.
* This allows comparing run times across different run sizes
* (e.g. to compute a time factor).
*
* @return the run times
*/
public long[] getRunTime()
{
return runTime;
}

/**
* Sets the number of iterations to execute the test methods in each test run.
* The default is 1 iteration.
*
* @param runIter the number of iterations to execute.
*/
Expand All @@ -77,6 +107,11 @@ protected void setRunIterations(int runIter)
this.runIter = runIter;
}

/**
* Gets the number of iterations for the run methods.
*
* @return the number of iterations
*/
public int getRunIterations()
{
return runIter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public class PerformanceTestRunner
{
private static final String RUN_PREFIX = "run";

private static final String INIT_METHOD = "init";

public static void run(Class clz)
{
PerformanceTestRunner runner = new PerformanceTestRunner();
Expand All @@ -57,6 +55,9 @@ private void runInternal(Class clz)

// do the run
test.setUp();
//-- initial times are zero (factor is not printed)
long[] runTimePrev = new long[runMethod.length];

for (int runNum = 0; runNum < runSize.length; runNum++)
{
int size = runSize[runNum];
Expand All @@ -67,8 +68,10 @@ private void runInternal(Class clz)
runMethod[i].invoke(test);
}
long time = sw.getTime();
System.out.println(runMethod[i].getName()
+ " : " + sw.getTimeString());
long timePrev = runTimePrev[i];
int sizePrev = runNum > 0 ? runSize[runNum-1] : -1;
reportRun(runMethod[i].getName(), sw.getTimeString(), size, time, sizePrev, timePrev);
runTimePrev[i] = time;
test.setTime(runNum, time);
}
test.endRun();
Expand All @@ -84,6 +87,17 @@ private void runInternal(Class clz)
}


private void reportRun(String name, String timeString, int size, long time, int sizePrev, long timePrev) {
String factorStr = "";
if (sizePrev > 0 && timePrev > 0) {
double sizeFactor = size / (double) sizePrev;
double timeFactor = time / (double) timePrev;
factorStr = String.format( " ( %.1fx - size %.1fx)", timeFactor, sizeFactor);
}
System.out.println(name
+ " : " + timeString + factorStr);
}

private static Method[] findMethods(Class clz, String methodPrefix)
{
List runMeths = new ArrayList();
Expand Down

0 comments on commit 027df7d

Please sign in to comment.