Skip to content

Commit

Permalink
adding a static timer method; you can quickly time an arbitrary block…
Browse files Browse the repository at this point in the history
… of code (using a lambda for example)
  • Loading branch information
toby-weston-db committed Mar 20, 2014
1 parent cb9be20 commit 0ca650f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

package com.google.code.tempusfugit.temporal;

import com.google.code.tempusfugit.Factory;
import com.google.code.tempusfugit.concurrency.Callable;

import java.util.function.Function;
import java.util.function.Supplier;

/**
* Stop watch implementations should time the difference between construction (or a call to {@link #reset}) and a call to
* {@link #lap}. They should allow for multiple calls to {@link #lap} but may preserve a class invariant that {@link #lap}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/google/code/tempusfugit/temporal/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package com.google.code.tempusfugit.temporal;

import com.google.code.tempusfugit.ClassInvariantViolation;
import com.google.code.tempusfugit.concurrency.Callable;
import com.google.code.tempusfugit.concurrency.annotations.Not;
import com.google.code.tempusfugit.concurrency.annotations.ThreadSafe;

import java.util.Date;
import java.util.function.Function;

import static com.google.code.tempusfugit.temporal.Duration.millis;

Expand All @@ -32,6 +34,16 @@ public final class Timer implements StopWatch {
private Date started;
private Date stopped;

public static Duration time(Runnable timed) {
Timer timer = new Timer(new RealClock());
try {
timed.run();
} finally {
timer.lap();
}
return timer.elapsedTime();
}

/**
* Constructs and starts a stop watch.
* @since 1.2
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/google/code/tempusfugit/temporal/TimerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
package com.google.code.tempusfugit.temporal;

import com.google.code.tempusfugit.ClassInvariantViolation;
import com.google.code.tempusfugit.concurrency.ThreadUtils;
import org.junit.Test;

import java.util.function.Function;
import java.util.function.Supplier;

import static com.google.code.tempusfugit.concurrency.ThreadUtils.sleep;
import static com.google.code.tempusfugit.temporal.Duration.millis;
import static com.google.code.tempusfugit.temporal.Duration.seconds;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.number.OrderingComparison.greaterThan;

public class TimerTest {

Expand Down Expand Up @@ -88,4 +97,10 @@ public void shouldRecordElapsedTimeBetweenStartAndStop() {
assertThat(timer.elapsedTime(), is(millis(20)));
}

@Test
public void timeSomething() {
Duration duration = Timer.time(() -> sleep(millis(25)));
assertThat(duration, is(both(greaterThan(millis(25))).and(lessThan(millis(45)))));
}

}

0 comments on commit 0ca650f

Please sign in to comment.