Skip to content

Commit

Permalink
Measure and log duration of individual tests in output.
Browse files Browse the repository at this point in the history
  • Loading branch information
J08nY committed Aug 19, 2024
1 parent bd1531f commit fd7d7ef
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
* An absctract basis of a TextTestWriter, which outputs in a human readable format, into console.
Expand Down Expand Up @@ -58,13 +60,13 @@ private String testString(Test t, String prefix, int index) {

String line = "";
if (root) {
char[] charLine = new char[BASE_WIDTH + 24];
new String(new char[BASE_WIDTH + 24]).replace("\0", "━").getChars(0, charLine.length - 1, charLine, 0);
char[] charLine = new char[BASE_WIDTH + 26];
new String(new char[BASE_WIDTH + 26]).replace("\0", "━").getChars(0, charLine.length - 1, charLine, 0);
charLine[0] = '■';
charLine[4] = '┳';
charLine[BASE_WIDTH + 1] = '┳';
charLine[BASE_WIDTH + 13] = '┳';
charLine[BASE_WIDTH + 23] = '┓';
charLine[BASE_WIDTH + 25] = '┓';
line = new String(charLine) + System.lineSeparator();
}

Expand Down Expand Up @@ -96,6 +98,24 @@ private String testString(Test t, String prefix, int index) {
}
out.append(Colors.colored(String.format("%-9s", result.getValue().name()), Colors.Attribute.BOLD, valueColor));
out.append(" ┃ ");
long totalNanos = t.getDuration();
double totalSeconds = totalNanos / 1e9;
double totalMillis = totalNanos / 1e6;
double totalMicros = totalNanos / 1e3;
if (totalSeconds < 1) {
if (totalMillis < 1) {
if (totalMicros < 1) {
out.append(String.format("%7d ns", totalNanos));
} else {
out.append(String.format("%6.2f µs", totalMicros));
}
} else {
out.append(String.format("%6.2f ms", totalMillis));
}
} else {
out.append(String.format("%6.2f s", totalSeconds));
}
out.append(" ┃ ");

if (compound) {
CompoundTest test = (CompoundTest) t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private Element testElement(Test t, int index) {
if (index != -1) {
testElem.setAttribute("index", String.valueOf(index));
}
testElem.setAttribute("duration", String.valueOf(t.getDuration()));

return testElem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private Map<String, Object> testObject(Test t, int index) {
if (index != -1) {
testObj.put("index", index);
}
testObj.put("duration", t.getDuration());

return testObj;
}
Expand Down
8 changes: 8 additions & 0 deletions common/src/main/java/cz/crcs/ectester/common/test/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public abstract class Test implements Testable, Cloneable {
protected boolean hasRun;
protected boolean hasStarted;
protected long duration;
protected Result result;

public Result getResult() {
Expand Down Expand Up @@ -48,6 +49,10 @@ public boolean hasStarted() {
return hasStarted;
}

public long getDuration() {
return duration;
}

@Override
public void reset() {
hasRun = false;
Expand All @@ -68,7 +73,10 @@ public void run() {
return;
try {
hasStarted = true;
long elapsed = -System.nanoTime();
runSelf();
elapsed += System.nanoTime();
duration = elapsed;
hasRun = true;
} catch (TestException e) {
result = new Result(Value.ERROR, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class FileTestWriter extends BaseFileTestWriter {

private static final Pattern PREFIX = Pattern.compile("(text|xml|yaml|yml):.+");
private static final Pattern PREFIX = Pattern.compile("(text|txt|xml|yaml|yml):.+");

public FileTestWriter(String defaultFormat, boolean systemOut, String[] files) throws ParserConfigurationException, FileNotFoundException {
super(defaultFormat, systemOut, files);
Expand All @@ -37,6 +37,7 @@ protected TestWriter createWriter(String format, PrintStream out) throws ParserC
}
switch (format) {
case "text":
case "txt":
return new TextTestWriter(out);
case "xml":
return new XMLTestWriter(out);
Expand Down

0 comments on commit fd7d7ef

Please sign in to comment.