blob: efa91f97e2f76f6e7f7ec3d8a6fa001eec8c7b2f [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 1998, 2012 Oracle and/or its affiliates. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Oracle - initial impl
******************************************************************************/
package example;
import java.util.ArrayList;
import java.util.List;
/**
* Generic performance test.
* This allows executing a timed run and measures and logs the performance.
* Logs the executions per run (minute), logs average and % stdev.
*/
public class PerformanceTest {
public static int REPEATS = 5;
public static int RUN_TIME = 10000; //10 seconds.
/**
* Measure the performance of the run.
* Repeat the run REPEATS (5) times,
* and measure the number of execution in RUN_TIME (60s).
*/
public static PerformanceResult executeRun(String name, Runnable runnable) {
System.out.println("Starting run: " + name);
List<Integer> results = new ArrayList<Integer>();
// Repeat the test and baseline for the number of repeats.
for (int index = 0; index < REPEATS; index++) {
long startTime, endTime;
int executions = 0;
System.gc();
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {}
startTime = System.currentTimeMillis();
endTime = startTime;
// Count how many times the test can be invoked in the run time.
// This allows for the test run time to be easily changed.
while ((startTime + RUN_TIME) >= endTime) {
runnable.run();
executions++;
endTime = System.currentTimeMillis();
}
results.add(executions);
System.out.println("Done run: " + index + " for: " + name);
}
System.out.println("Completed run: " + name);
PerformanceResult result = new PerformanceResult();
result.testName = name;
result.runRepeats = REPEATS;
result.runTime = RUN_TIME;
result.average = averageResults(results);
result.max = maxResults(results);
result.min = minResults(results);
result.standardDeviation = standardDeviationResults(results);
System.out.println("");
System.out.println(result);
System.out.println("");
System.out.println("");
return result;
}
/**
* Compute the max of the results.
*/
public static int maxResults(List<Integer> times) {
int testMax = 0;
for (int index = 0; index < times.size(); index++) {
int time = (int)times.get(index);
if (time > testMax) {
testMax = time;
}
}
return testMax;
}
/**
* Compute the min of the results.
*/
public static int minResults(List<Integer> times) {
int testMin = 0;
for (int index = 0; index < times.size(); index++) {
int time = (int)times.get(index);
if ((testMin == 0) || (time < testMin)) {
testMin = time;
}
}
return testMin;
}
/**
* Filter max and min from results.
*/
public static List<Integer> filterMaxMinResults(List<Integer> times) {
List filteredTimes = new ArrayList(times);
if (filteredTimes.size() > 3) {
filteredTimes.remove((Integer)maxResults(times));
filteredTimes.remove((Integer)minResults(times));
}
return filteredTimes;
}
/**
* Compute the average of the results rejecting the min and max.
*/
public static double averageResults(List<Integer> allTimes) {
// Compute the average reject the min and max to improve consistency.
List<Integer> times = filterMaxMinResults(allTimes);
double testAverage = 0;
for (int index = 0; index < times.size(); index++) {
int time = (int)times.get(index);
testAverage = testAverage + time;
}
testAverage = testAverage / times.size();
return testAverage;
}
/**
* Compute the standard deviation of the results rejecting the min and max.
*/
public static double standardDeviationResults(List<Integer> allTimes) {
// Compute the average reject the min and max to improve consistency.
double testAverage = averageResults(allTimes);
// Compute the standard deviation reject the min and max to improve consistency.
List<Integer> times = filterMaxMinResults(allTimes);
double testStandardDeviation = 0;
for (int index = 0; index < times.size(); index++) {
int time = (int)times.get(index);
testStandardDeviation = testStandardDeviation + Math.pow(time - testAverage, 2);
}
testStandardDeviation = testStandardDeviation / times.size();
testStandardDeviation = Math.sqrt(testStandardDeviation);
// As percent of average
testStandardDeviation = (testStandardDeviation / testAverage) * 100;
return testStandardDeviation;
}
}