Created
May 25, 2018 19:17
-
-
Save tzachz/bc91cfcfdd5575ecb10bb36a495118ef to your computer and use it in GitHub Desktop.
Temporal Coupling Example
This file contains hidden or 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
public class TestsExecutionRecorder { | |
private String namespace; | |
private Stopwatch stopwatch; | |
private long duration; | |
TestsExecutionRecorder(TestType testType) { | |
this.namespace = "test." + testType; | |
} | |
// MUST be called before testEnvLoadEnded() | |
public void startRecording() { | |
DevMetricReporter.reportMetric(namespace + ".execution.counter", 1); | |
this.stopwatch = Stopwatch.createStarted(); | |
} | |
// CAN be called before testEnvLoadEnded() - meaningless if called after it | |
public void setGroupName(String groupName) { | |
this.namespace += "." + groupName; | |
} | |
// MUST be called after startRecording() | |
public void testEnvLoadEnded() { | |
this.duration = stopwatch.elapsed(TimeUnit.SECONDS); | |
storeRecord(); | |
} | |
private void storeRecord() { | |
DevMetricReporter.reportMetric(this.namespace + ".execution.loadDuration", this.duration); | |
} | |
} | |
// USAGE: | |
final TestsExecutionRecorder testsExecutionRecorder = new TestsExecutionRecorder(TestType.Integration); | |
// more stuff... | |
testsExecutionRecorder.startRecording(); | |
// more code that may take a while... | |
final Optional<String> groupName = getGroupName(); | |
groupName.ifPresent(testsExecutionRecorder::setGroupName); | |
// more code that may take a while... | |
testsExecutionRecorder.testEnvLoadEnded(); | |
This file contains hidden or 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
public class TestsExecutionRecorder { | |
// no mutable fields | |
private final String namespace; | |
private final Stopwatch stopwatch; | |
TestsExecutionRecorder(TestType testType) { | |
namespace = "test." + testType; | |
DevMetricReporter.reportMetric(namespace + ".execution.counter", 1); | |
stopwatch = Stopwatch.createStarted(); | |
} | |
// only one public method | |
public void testEnvLoadEnded(Optional<String> groupName) { | |
final long duration = stopwatch.elapsed(TimeUnit.SECONDS); | |
final String prefix = groupName.map(g -> namespace + "." + g).orElse(namespace); | |
DevMetricReporter.reportMetric(prefix + ".execution.loadDuration", duration); | |
} | |
} | |
// USAGE: | |
// more stuff... | |
final TestsExecutionRecorder testsExecutionRecorder = new TestsExecutionRecorder(TestType.Integration); | |
// more code that may take a while... | |
final Optional<String> groupName = getGroupName(); | |
// more code that may take a while... | |
testsExecutionRecorder.testEnvLoadEnded(groupName); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment