Skip to content
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

Add a method for timing non-throwing functions. #1224

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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