-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Time Watcher #552
Merged
Merged
Time Watcher #552
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
582d86c
Time Watcher
0a26010
fixed Javadoc "its"
c4e3854
Added #finished(long nanos, Description description)
ac19598
fixed indentation in Javadoc code
cbcb29a
changes
96e051b
getNanos() is a single thread confinement. Volatiles not needed!
3c122b6
using record objects
9ec6a3f
standard JUnit class fields
ecb4de6
fixed grammar "own -> its own"
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package org.junit.rules; | ||
|
||
import org.junit.internal.AssumptionViolatedException; | ||
import org.junit.runner.Description; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* The Stopwatch Rule notifies one of own protected methods of the time spent by a test.<p/> | ||
* Override them to get the time in nanoseconds. For example, this class will keep logging the | ||
* time spent by each passing, failing and skipped test: | ||
* | ||
* <pre> | ||
* public static class StopwatchTest { | ||
* private static final Logger logger = Logger.getLogger(""); | ||
* | ||
* private static void logInfo(String testName, String status, long nanos) { | ||
* logger.info(String.format("Test %s %s, spent %d microseconds", | ||
* testName, status, Stopwatch.toMicros(nanos))); | ||
* } | ||
* | ||
* @Rule | ||
* public Stopwatch stopwatch= new Stopwatch() { | ||
* @Override | ||
* protected void succeeded(long nanos, Description description) { | ||
* logInfo(description.getMethodName(), "succeeded", nanos); | ||
* } | ||
* | ||
* @Override | ||
* protected void failed(long nanos, Throwable e, Description description) { | ||
* logInfo(description.getMethodName(), "failed", nanos); | ||
* } | ||
* | ||
* @Override | ||
* protected void skipped(long nanos, AssumptionViolatedException e, Description description) { | ||
* logInfo(description.getMethodName(), "skipped", nanos); | ||
* } | ||
* | ||
* @Override | ||
* protected void finished(long nanos, Description description) { | ||
* logInfo(description.getMethodName(), "finished", nanos); | ||
* } | ||
* }; | ||
* | ||
* @Test | ||
* public void succeeds() { | ||
* } | ||
* | ||
* @Test | ||
* public void fails() { | ||
* fail(); | ||
* } | ||
* | ||
* @Test | ||
* public void skips() { | ||
* assumeTrue(false); | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* @author tibor17 | ||
* @since 4.12 | ||
*/ | ||
public class Stopwatch extends TestWatcher { | ||
private volatile long startNanos = 0L; | ||
private volatile long endNanos = 0L; | ||
|
||
/** | ||
* Invoked when a test succeeds | ||
*/ | ||
protected void succeeded(long nanos, Description description) { | ||
} | ||
|
||
/** | ||
* Invoked when a test fails | ||
*/ | ||
protected void failed(long nanos, Throwable e, Description description) { | ||
} | ||
|
||
/** | ||
* Invoked when a test is skipped due to a failed assumption. | ||
*/ | ||
protected void skipped(long nanos, AssumptionViolatedException e, Description description) { | ||
} | ||
|
||
/** | ||
* Invoked when a test method finishes (whether passing or failing) | ||
*/ | ||
protected void finished(long nanos, Description description) { | ||
} | ||
|
||
/** | ||
* @param nanos time in nanoseconds | ||
* @return time converted to microseconds | ||
*/ | ||
public static long toMicros(long nanos) { | ||
return TimeUnit.NANOSECONDS.toMicros(nanos); | ||
} | ||
|
||
/** | ||
* @param nanos time in nanoseconds | ||
* @return time converted to milliseconds | ||
*/ | ||
public static long toMillis(long nanos) { | ||
return TimeUnit.NANOSECONDS.toMillis(nanos); | ||
} | ||
|
||
/** | ||
* @param nanos time in nanoseconds | ||
* @return time converted to seconds | ||
*/ | ||
public static long toSeconds(long nanos) { | ||
return TimeUnit.NANOSECONDS.toSeconds(nanos); | ||
} | ||
|
||
private long getNanos() { | ||
return endNanos - startNanos; | ||
} | ||
|
||
private void starting() { | ||
startNanos = System.nanoTime(); | ||
} | ||
|
||
private void stopping() { | ||
endNanos = System.nanoTime(); | ||
} | ||
|
||
@Override final protected void succeeded(Description description) { | ||
stopping(); | ||
succeeded(getNanos(), description); | ||
} | ||
|
||
@Override final protected void failed(Throwable e, Description description) { | ||
stopping(); | ||
failed(getNanos(), e, description); | ||
} | ||
|
||
@Override final protected void skipped(AssumptionViolatedException e, Description description) { | ||
stopping(); | ||
skipped(getNanos(), e, description); | ||
} | ||
|
||
@Override final protected void starting(Description description) { | ||
starting(); | ||
} | ||
|
||
@Override final protected void finished(Description description) { | ||
finished(getNanos(), description); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/test/java/org/junit/tests/experimental/rules/StopwatchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package org.junit.tests.experimental.rules; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.internal.AssumptionViolatedException; | ||
import org.junit.rules.Stopwatch; | ||
import org.junit.runner.Description; | ||
import org.junit.runner.JUnitCore; | ||
import org.junit.runner.Result; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assume.assumeTrue; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* @author tibor17 | ||
* @since 4.12 | ||
*/ | ||
public class StopwatchTest { | ||
public static enum TestStatus {SUCCEEDED, FAILED, SKIPPED } | ||
|
||
public static abstract class AbstractStopwatchTest { | ||
public static long timeSpent; | ||
public static long timeSpentIfFinished; | ||
public static String testName; | ||
public static String testNameIfFinished; | ||
public static TestStatus status; | ||
|
||
@Rule | ||
public final Stopwatch stopwatch = new Stopwatch() { | ||
@Override | ||
protected void succeeded(long nanos, Description description) { | ||
timeSpent = nanos; | ||
status = TestStatus.SUCCEEDED; | ||
testName = description.getMethodName(); | ||
} | ||
|
||
@Override | ||
protected void failed(long nanos, Throwable e, Description description) { | ||
timeSpent = nanos; | ||
status = TestStatus.FAILED; | ||
testName = description.getMethodName(); | ||
} | ||
|
||
@Override | ||
protected void skipped(long nanos, AssumptionViolatedException e, Description description) { | ||
timeSpent = nanos; | ||
status = TestStatus.SKIPPED; | ||
testName = description.getMethodName(); | ||
} | ||
|
||
@Override | ||
protected void finished(long nanos, Description description) { | ||
timeSpentIfFinished = nanos; | ||
testNameIfFinished = description.getMethodName(); | ||
} | ||
}; | ||
} | ||
|
||
public static class SuccessfulTest extends AbstractStopwatchTest { | ||
@Test | ||
public void successfulTest() { | ||
} | ||
} | ||
|
||
public static class FailedTest extends AbstractStopwatchTest { | ||
@Test | ||
public void failedTest() { | ||
fail(); | ||
} | ||
} | ||
|
||
public static class SkippedTest extends AbstractStopwatchTest { | ||
@Test | ||
public void skippedTest() { | ||
assumeTrue(false); | ||
} | ||
} | ||
|
||
@Before | ||
public void init() { | ||
AbstractStopwatchTest.testName = null; | ||
AbstractStopwatchTest.testNameIfFinished = null; | ||
AbstractStopwatchTest.status = null; | ||
AbstractStopwatchTest.timeSpent = 0; | ||
AbstractStopwatchTest.timeSpentIfFinished = 0; | ||
} | ||
|
||
@Test | ||
public void succeeded() { | ||
Result result = JUnitCore.runClasses(SuccessfulTest.class); | ||
assertEquals(0, result.getFailureCount()); | ||
assertThat(AbstractStopwatchTest.testName, is("successfulTest")); | ||
assertThat(AbstractStopwatchTest.testName, is(AbstractStopwatchTest.testNameIfFinished)); | ||
assertThat(AbstractStopwatchTest.status, is(TestStatus.SUCCEEDED)); | ||
assertTrue("timeSpent > 0", AbstractStopwatchTest.timeSpent > 0); | ||
assertThat(AbstractStopwatchTest.timeSpent, is(AbstractStopwatchTest.timeSpentIfFinished)); | ||
} | ||
|
||
@Test | ||
public void failed() { | ||
Result result = JUnitCore.runClasses(FailedTest.class); | ||
assertEquals(1, result.getFailureCount()); | ||
assertThat(AbstractStopwatchTest.testName, is("failedTest")); | ||
assertThat(AbstractStopwatchTest.testName, is(AbstractStopwatchTest.testNameIfFinished)); | ||
assertThat(AbstractStopwatchTest.status, is(TestStatus.FAILED)); | ||
assertTrue("timeSpent > 0", AbstractStopwatchTest.timeSpent > 0); | ||
assertThat(AbstractStopwatchTest.timeSpent, is(AbstractStopwatchTest.timeSpentIfFinished)); | ||
} | ||
|
||
@Test | ||
public void skipped() { | ||
Result result = JUnitCore.runClasses(SkippedTest.class); | ||
assertEquals(0, result.getFailureCount()); | ||
assertThat(AbstractStopwatchTest.testName, is("skippedTest")); | ||
assertThat(AbstractStopwatchTest.testName, is(AbstractStopwatchTest.testNameIfFinished)); | ||
assertThat(AbstractStopwatchTest.status, is(TestStatus.SKIPPED)); | ||
assertTrue("timeSpent > 0", AbstractStopwatchTest.timeSpent > 0); | ||
assertThat(AbstractStopwatchTest.timeSpent, is(AbstractStopwatchTest.timeSpentIfFinished)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think extracting these static members into an own class, say
Recording
, would make the whole test easier to read. That way, you could simply assign a new instance to a single static field in the@Before
method. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem to modify this style of code.
thx.