Package com.guinetik.corefun
Interface Timing
public interface Timing
Interface for timing the execution of operations.
Timing provides methods for measuring execution time with pluggable
logging. Implement onTimed(String, long) to receive timing results.
This is useful for performance monitoring, profiling, and debugging.
Key Features
- Automatic timing - Execution time measured in milliseconds
- Pluggable reporting - Implement
onTimed(java.lang.String, long)for custom handling - Exception safe - Timing reported even if operation throws
- Multiple operation types - Support for Callable, Runnable, SafeRunnable
Example Usage
public class MyService implements Timing {
@Override
public void onTimed(String description, long milliseconds) {
logger.info("{} took {}ms", description, milliseconds);
}
public User loadUser(long id) {
return timed("Load user " + id, () -> userRepository.findById(id));
}
public void sendNotification() {
timedVoid("Send notification", () -> notificationService.send());
}
}
Metrics Collection
// Report to metrics system
Timing metricsTimer = (desc, ms) ->
metrics.recordTiming("operation." + desc, ms, TimeUnit.MILLISECONDS);
// Use the timer
metricsTimer.timed("database.query", () -> db.execute(query));
- Since:
- 0.1.0
- Author:
- Guinetik <guinetik@gmail.com>
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic Timingnoop()Creates a Timing instance that discards timing information.voidCalled after an operation completes with timing information.static Timingprintln()Creates a Timing instance that prints to standard output.default <T> TExecutes a callable and times its execution.default voidtimedSafe(String description, SafeRunnable action) Executes a SafeRunnable and times its execution.default voidExecutes a runnable and times its execution.
-
Method Details
-
onTimed
Called after an operation completes with timing information. Implement this to log, record metrics, or otherwise handle timing data.- Parameters:
description- the operation descriptionmilliseconds- the execution time in milliseconds
-
timed
Executes a callable and times its execution.- Type Parameters:
T- the return type- Parameters:
description- description of the operation (for logging)action- the operation to execute- Returns:
- the result of the callable
- Throws:
SafeException- if the operation throws an exception
-
timedVoid
Executes a runnable and times its execution.- Parameters:
description- description of the operation (for logging)action- the operation to execute- Throws:
SafeException- if the operation throws an exception
-
timedSafe
Executes a SafeRunnable and times its execution.- Parameters:
description- description of the operation (for logging)action- the operation to execute- Throws:
SafeException- if the operation throws an exception
-
println
Creates a Timing instance that prints to standard output.- Returns:
- a Timing that prints timing info
-
noop
Creates a Timing instance that discards timing information.- Returns:
- a no-op Timing
-