85 lines
2.9 KiB
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collectionsindepth/Tester.java
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-12-15 11:47:04 -08:00
// Applies Test objects to lists of different collections.
2015-06-15 17:47:35 -07:00
import java.util.*;
public class Tester<C> {
public static int fieldWidth = 8;
public static TestParam[] defaultParams= TestParam.array(
10, 5000, 100, 5000, 1000, 5000, 10000, 500);
// Override this to modify pre-test initialization:
2015-12-15 11:47:04 -08:00
protected C initialize(int size) { return collection; }
protected C collection;
2015-06-15 17:47:35 -07:00
private String headline = "";
private List<Test<C>> tests;
private static String stringField() {
return "%" + fieldWidth + "s";
}
private static String numberField() {
return "%" + fieldWidth + "d";
}
private static int sizeWidth = 5;
private static String sizeField = "%" + sizeWidth + "s";
private TestParam[] paramList = defaultParams;
2015-12-15 11:47:04 -08:00
public Tester(C collection, List<Test<C>> tests) {
this.collection = collection;
2015-06-15 17:47:35 -07:00
this.tests = tests;
2015-12-15 11:47:04 -08:00
if(collection != null)
headline = collection.getClass().getSimpleName();
2015-06-15 17:47:35 -07:00
}
2015-12-15 11:47:04 -08:00
public Tester(C collection, List<Test<C>> tests,
2015-06-15 17:47:35 -07:00
TestParam[] paramList) {
2015-12-15 11:47:04 -08:00
this(collection, tests);
2015-06-15 17:47:35 -07:00
this.paramList = paramList;
}
public void setHeadline(String newHeadline) {
headline = newHeadline;
}
// Generic methods for convenience :
public static <C> void run(C cntnr, List<Test<C>> tests){
new Tester<>(cntnr, tests).timedTest();
}
public static <C> void run(C cntnr,
List<Test<C>> tests, TestParam[] paramList) {
new Tester<>(cntnr, tests, paramList).timedTest();
}
private void displayHeader() {
// Calculate width and pad with '-':
int width = fieldWidth * tests.size() + sizeWidth;
int dashLength = width - headline.length() - 1;
StringBuilder head = new StringBuilder(width);
for(int i = 0; i < dashLength/2; i++)
head.append('-');
head.append(' ');
head.append(headline);
head.append(' ');
for(int i = 0; i < dashLength/2; i++)
head.append('-');
System.out.println(head);
// Print column headers:
System.out.format(sizeField, "size");
for(Test<C> test : tests)
System.out.format(stringField(), test.name);
System.out.println();
}
2015-12-15 11:47:04 -08:00
// Run the tests for this collection:
2015-06-15 17:47:35 -07:00
public void timedTest() {
displayHeader();
for(TestParam param : paramList) {
System.out.format(sizeField, param.size);
for(Test<C> test : tests) {
C kontainer = initialize(param.size);
long start = System.nanoTime();
// Call the overriden method:
int reps = test.test(kontainer, param);
long duration = System.nanoTime() - start;
long timePerRep = duration / reps; // Nanoseconds
System.out.format(numberField(), timePerRep);
}
System.out.println();
}
}
2015-09-07 11:44:36 -06:00
}