85 lines
2.8 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.
2016-01-25 18:05:55 -08:00
// Applies Test objects to lists of different collections
2015-06-15 17:47:35 -07:00
import java.util.*;
2016-01-25 18:05:55 -08:00
import java.time.*;
2015-06-15 17:47:35 -07:00
public class Tester<C> {
public static int fieldWidth = 8;
2016-01-25 18:05:55 -08:00
public static
TestParam[] defaultParams= TestParam.array(
2015-06-15 17:47:35 -07:00
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 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 :
2016-01-25 18:05:55 -08:00
public static
<C> void run(C cntnr, List<Test<C>> tests) {
2015-06-15 17:47:35 -07:00
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)
2016-01-25 18:05:55 -08:00
System.out.format(
"%" + fieldWidth + "s", test.name);
2015-06-15 17:47:35 -07:00
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);
2016-01-25 18:05:55 -08:00
Instant start = Instant.now();
2015-06-15 17:47:35 -07:00
// Call the overriden method:
int reps = test.test(kontainer, param);
2016-01-25 18:05:55 -08:00
Duration elapsed =
Duration.between(start, Instant.now());
Duration timePerRep = elapsed.dividedBy(reps);
System.out.format("%" + fieldWidth + "d",
timePerRep.toNanos());
2015-06-15 17:47:35 -07:00
}
System.out.println();
}
}
2015-09-07 11:44:36 -06:00
}