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 Type
    Method
    Description
    static Timing
    Creates a Timing instance that discards timing information.
    void
    onTimed(String description, long milliseconds)
    Called after an operation completes with timing information.
    static Timing
    Creates a Timing instance that prints to standard output.
    default <T> T
    timed(String description, Callable<T> action)
    Executes a callable and times its execution.
    default void
    timedSafe(String description, SafeRunnable action)
    Executes a SafeRunnable and times its execution.
    default void
    timedVoid(String description, Runnable action)
    Executes a runnable and times its execution.
  • Method Details

    • onTimed

      void onTimed(String description, long milliseconds)
      Called after an operation completes with timing information. Implement this to log, record metrics, or otherwise handle timing data.
      Parameters:
      description - the operation description
      milliseconds - the execution time in milliseconds
    • timed

      default <T> T timed(String description, Callable<T> action)
      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

      default void timedVoid(String description, Runnable action)
      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

      default void timedSafe(String description, SafeRunnable action)
      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

      static Timing println()
      Creates a Timing instance that prints to standard output.
      Returns:
      a Timing that prints timing info
    • noop

      static Timing noop()
      Creates a Timing instance that discards timing information.
      Returns:
      a no-op Timing