Repository navigation
Expand file tree
/
Copy pathTimingRules.java
More file actions
42 lines (34 loc) · 1.52 KB
/
Copy pathTimingRules.java
File metadata and controls
42 lines (34 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package ru.javawebinar.topjava;
import org.junit.rules.ExternalResource;
import org.junit.rules.Stopwatch;
import org.junit.runner.Description;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
public class TimingRules {
private static final Logger log = LoggerFactory.getLogger("result");
private static final StringBuilder results = new StringBuilder();
// http://stackoverflow.com/questions/14892125/what-is-the-best-practice-to-determine-the-execution-time-of-the-bussiness-relev
public static final Stopwatch STOPWATCH = new Stopwatch() {
@Override
protected void finished(long nanos, Description description) {
String result = String.format("%-95s %7d", description.getDisplayName(), TimeUnit.NANOSECONDS.toMillis(nanos));
results.append(result).append('\n');
log.info(result + " ms\n");
}
};
// https://dzone.com/articles/applying-new-jdk-11-string-methods
private static final String DELIM = "-".repeat(103);
public static final ExternalResource SUMMARY = new ExternalResource() {
@Override
protected void before() throws Throwable {
results.setLength(0);
}
@Override
protected void after() {
log.info("\n" + DELIM +
"\nTest Duration, ms" +
"\n" + DELIM + "\n" + results + DELIM + "\n");
}
};
}