FunctionUtilities { public static <T> T time(String name, Supplier<T> supplier) { long st = System.currentTimeMillis(); try { T r = supplier.get(); long end = System.currentTimeMillis(); LOG.info("duration [{}]: {}", name, (end - st)); return r; } catch (Exception e) { String msg = String.format("Unexpected error when timing [%s]: %s", name, e.getMessage()); LOG.error(msg, e); return null; } }  static void discardResult(Object x); static T time(String name, Supplier<T> supplier); static void time(String name, Runnable runner); static BiFunction<X, Y, Z> alwaysBi(Z result); static Function<? super X, Y> always(Y result);  }
@Test public void timeChecksHowLongAFunctionRunsFor() { String result = FunctionUtilities.time("foo", Unchecked.supplier(() -> { Thread.sleep(500); return "a"; })); assertEquals("a", result); }