Package org.junit.rules
Class Stopwatch
- java.lang.Object
-
- org.junit.rules.Stopwatch
-
- All Implemented Interfaces:
TestRule
public class Stopwatch extends java.lang.Object implements TestRule
The Stopwatch Rule notifies one of its own protected methods of the time spent by a test.Override them to get the time in nanoseconds. For example, this class will keep logging the time spent by each passed, failed, skipped, and finished test:
public static class StopwatchTest { private static final Logger logger = Logger.getLogger(""); private static void logInfo(Description description, String status, long nanos) { String testName = description.getMethodName(); logger.info(String.format("Test %s %s, spent %d microseconds", testName, status, TimeUnit.NANOSECONDS.toMicros(nanos))); } @Rule public Stopwatch stopwatch = new Stopwatch() { @Override protected void succeeded(long nanos, Description description) { logInfo(description, "succeeded", nanos); } @Override protected void failed(long nanos, Throwable e, Description description) { logInfo(description, "failed", nanos); } @Override protected void skipped(long nanos, AssumptionViolatedException e, Description description) { logInfo(description, "skipped", nanos); } @Override protected void finished(long nanos, Description description) { logInfo(description, "finished", nanos); } }; @Test public void succeeds() { } @Test public void fails() { fail(); } @Test public void skips() { assumeTrue(false); } }An example to assert runtime:@Test public void performanceTest() throws InterruptedException { long delta = 30; Thread.sleep(300L); assertEquals(300d, stopwatch.runtime(MILLISECONDS), delta); Thread.sleep(500L); assertEquals(800d, stopwatch.runtime(MILLISECONDS), delta); }- Since:
- 4.12
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classStopwatch.Clockprivate classStopwatch.InternalWatcher
-
Field Summary
Fields Modifier and Type Field Description private Stopwatch.Clockclockprivate longendNanosprivate longstartNanos
-
Constructor Summary
Constructors Constructor Description Stopwatch()Stopwatch(Stopwatch.Clock clock)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Statementapply(Statement base, Description description)Modifies the method-runningStatementto implement this test-running rule.protected voidfailed(long nanos, java.lang.Throwable e, Description description)Invoked when a test failsprotected voidfinished(long nanos, Description description)Invoked when a test method finishes (whether passing or failing)private longgetNanos()longruntime(java.util.concurrent.TimeUnit unit)Gets the runtime for the test.protected voidskipped(long nanos, AssumptionViolatedException e, Description description)Invoked when a test is skipped due to a failed assumption.private voidstarting()private voidstopping()protected voidsucceeded(long nanos, Description description)Invoked when a test succeeds
-
-
-
Field Detail
-
clock
private final Stopwatch.Clock clock
-
startNanos
private volatile long startNanos
-
endNanos
private volatile long endNanos
-
-
Constructor Detail
-
Stopwatch
public Stopwatch()
-
Stopwatch
Stopwatch(Stopwatch.Clock clock)
-
-
Method Detail
-
runtime
public long runtime(java.util.concurrent.TimeUnit unit)
Gets the runtime for the test.- Parameters:
unit- time unit for returned runtime- Returns:
- runtime measured during the test
-
succeeded
protected void succeeded(long nanos, Description description)Invoked when a test succeeds
-
failed
protected void failed(long nanos, java.lang.Throwable e, Description description)Invoked when a test fails
-
skipped
protected void skipped(long nanos, AssumptionViolatedException e, Description description)Invoked when a test is skipped due to a failed assumption.
-
finished
protected void finished(long nanos, Description description)Invoked when a test method finishes (whether passing or failing)
-
getNanos
private long getNanos()
-
starting
private void starting()
-
stopping
private void stopping()
-
apply
public final Statement apply(Statement base, Description description)
Description copied from interface:TestRuleModifies the method-runningStatementto implement this test-running rule.- Specified by:
applyin interfaceTestRule- Parameters:
base- TheStatementto be modifieddescription- ADescriptionof the test implemented inbase- Returns:
- a new statement, which may be the same as
base, a wrapper aroundbase, or a completely new Statement.
-
-