Skip to content

Commit

Permalink
Add a method for timing non-throwing functions. (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamnis authored and arteam committed Nov 28, 2017
1 parent 6a1c202 commit cf626fa
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions metrics-core/src/main/java/com/codahale/metrics/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
* A timer metric which aggregates timing durations and provides duration statistics, plus
Expand Down Expand Up @@ -106,6 +107,24 @@ public <T> T time(Callable<T> event) throws Exception {
}
}

/**
* Times and records the duration of event. Should not throw exceptions, for that use the
* {@link #time(Callable)} method.
*
* @param event a {@link Supplier} whose {@link Supplier#get()} method implements a process
* whose duration should be timed
* @param <T> the type of the value returned by {@code event}
* @return the value returned by {@code event}
*/
public <T> T timeSupplier(Supplier<T> event) {
final long startTime = clock.getTick();
try {
return event.get();
} finally {
update(clock.getTick() - startTime);
}
}

/**
* Times and records the duration of event.
*
Expand Down

0 comments on commit cf626fa

Please sign in to comment.