FEAT StopWatch procedural/functional time measurement
parent
0941bbf100
commit
ecefdbb1e1
|
@ -0,0 +1,71 @@
|
|||
package de.nclazz.commons;
|
||||
|
||||
import lombok.NonNull;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.util.function.LongConsumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class StopWatch {
|
||||
|
||||
private final Clock clock;
|
||||
private long tStart;
|
||||
private long tEnd;
|
||||
private long finalTime = -1;
|
||||
|
||||
private LongConsumer elapsedTimeConsumer;
|
||||
|
||||
|
||||
private StopWatch(Clock clock) {
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
public StopWatch onCompleted(LongConsumer consumer) {
|
||||
this.elapsedTimeConsumer = consumer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public long getTimeStart() {
|
||||
return this.tStart;
|
||||
}
|
||||
|
||||
public long getTimeEnd() {
|
||||
return this.tEnd;
|
||||
}
|
||||
|
||||
public long getElapsedTime() {
|
||||
return this.finalTime;
|
||||
}
|
||||
|
||||
public <T> T measure(Supplier<T> supplier) {
|
||||
this.tStart = this.clock.millis();
|
||||
|
||||
T result = supplier.get();
|
||||
|
||||
this.tEnd = this.clock.millis();
|
||||
this.finalTime = this.tEnd - this.tStart;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void measure(Runnable runnable) {
|
||||
measure(emptySupplier(runnable));
|
||||
}
|
||||
|
||||
private Supplier<Void> emptySupplier(Runnable runnable) {
|
||||
return () -> {
|
||||
runnable.run();
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
public static StopWatch defaultTimer() {
|
||||
return new StopWatch(Clock.systemDefaultZone());
|
||||
}
|
||||
|
||||
public static StopWatch forClock(@NonNull Clock clock) {
|
||||
return new StopWatch(clock);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package de.nclazz.commons;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
|
||||
/**
|
||||
* Mockup Clock that increments millis about 10 units,
|
||||
* whenever the {@link #millis()} method is called;
|
||||
*/
|
||||
public class MockupClock extends Clock {
|
||||
|
||||
private long t = -10;
|
||||
|
||||
@Override
|
||||
public long millis() {
|
||||
t += 10;
|
||||
return t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZoneId getZone() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Clock withZone(ZoneId zone) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant instant() {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package de.nclazz.commons;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class StopWatchTests {
|
||||
|
||||
@Test
|
||||
void testProceduralUsage() {
|
||||
StopWatch watch = StopWatch.forClock(new MockupClock());
|
||||
|
||||
String value = watch.measure(() -> "Hello World");
|
||||
|
||||
assertEquals("Hello World", value);
|
||||
assertEquals(10, watch.getElapsedTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFunctionalUsage() {
|
||||
|
||||
String value = StopWatch.forClock(new MockupClock())
|
||||
.onCompleted(elapsed -> assertEquals(10, elapsed))
|
||||
.measure(() -> "Hello World");
|
||||
|
||||
assertEquals("Hello World", value);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue