T
- the perfometer input type.public abstract class Perfometer<T> extends Object
Utility class to measure the worst case execution time and average
execution time with high precision. Here an example measuring the
worst case execution time of List.add(int, Object)
for diverse list implementations.
Perfometer<Class<? extends List>> insertPerf = new Perfometer<>("java.util.List#add(int, Object)") { List<Object> list; Random random; protected void initialize() throws Exception { list = getInput().newInstance(); random = new Random(-1); // Use seed to ensure same execution path. } protected void run(boolean measure) { Object obj = new Object(); int i = random.nextInt(list.size() + 1); if (measure) list.add(i, obj); } protected void validate() { // Optional. assert list.size() == getNbrOfIterations(); } } ... public void testExecutionTime() { insertPerf.measure(java.util.ArrayList.class, 10000).print(); insertPerf.measure(java.util.LinkedList.class, 10000).print(); insertPerf.measure(javolution.util.FastTable.class, 10000).print(); } ... > [INFO] java.util.List#add(int, Object) (10000) for java.util.ArrayList: 590.21450 ns (avg), 8443.0000 ns (wcet#9369) > [INFO] java.util.List#add(int, Object) (10000) for java.util.LinkedList: 4849.8313 ns (avg), 26536.000 ns (wcet#9863) > [INFO] java.util.List#add(int, Object) (10000) for javolution.util.FastTable: 217.26300 ns (avg), 534.00000 ns (wcet#8864)
Modifier and Type | Field and Description |
---|---|
static Configurable<Integer> |
DURATION_MS
Hold the measurement duration in milliseconds (default 1000 ms).
|
static Configurable<Boolean> |
SKIP
Indicates if perfometer measurements should be skipped (
e.g.
|
Constructor and Description |
---|
Perfometer(String description)
Creates a perfometer having the specified description.
|
Modifier and Type | Method and Description |
---|---|
double |
getAvgTimeInSeconds()
Returns the average execution time in seconds.
|
String |
getDescription()
Returns this perfometer description.
|
T |
getInput()
Returns this perfometer current inputs.
|
int |
getNbrOfIterations()
Returns this perfometer current number of iterations performed.
|
double[] |
getTimesInSeconds()
Returns the execution times in seconds.
|
double |
getWCETinSeconds()
Returns the worst case execution time in seconds.
|
int |
getWorstCaseNumber()
Returns the iteration number having the slowest execution time.
|
protected abstract void |
initialize()
Performs the initialization.
|
Perfometer<T> |
measure(T input,
int nbrOfIterations)
Measures the worst case execution time and average execution time.
|
void |
print()
Outputs the result.
|
void |
printDetails()
Outputs the measurements in nanoseconds.
|
protected abstract void |
run(boolean measure)
Runs the code being benchmarked.
|
protected void |
validate()
Validates the final result (after all iterations are completed).
|
public static final Configurable<Integer> DURATION_MS
public static final Configurable<Boolean> SKIP
-Djavolution.test.Perfometer#SKIP=true
to skip
performance measurements).
When skipped, measure(T, int)
and print()
don't do anything.public Perfometer(String description)
description
- the description of the code being measured.public double getAvgTimeInSeconds()
public String getDescription()
public T getInput()
public int getNbrOfIterations()
public double[] getTimesInSeconds()
public Perfometer<T> measure(T input, int nbrOfIterations)
input
- the test input.nbrOfIterations
- the number of iterations performed on which
the average will be calculated.public void print()
public void printDetails()
public double getWCETinSeconds()
public int getWorstCaseNumber()
protected abstract void initialize() throws Exception
Exception
protected abstract void run(boolean measure) throws Exception
measure
- false
when calibration is performed;
true
otherwise.Exception
protected void validate()
Copyright © 2005-2013 Javolution. All Rights Reserved.