/** * Registers the final duration of a stopped timer with the RequestLog. * * @param stoppedPhase The phase that has been stopped, and whose duration needs to be stored */ public static void registerTime(TimedPhase stoppedPhase) { RequestLog.REGISTRY.timer(stoppedPhase.getName()).update(stoppedPhase.getDuration(), stoppedPhase.getUnit()); }
/** * Stops the timer, and registers the timer with the RequestLog. * <p> * This is primarily meant to be used by the try-with-resources block, which both stops the timer and registers it * with the RequestLog, though it can of course be called manually as well. If you want to stop the timer, but * don't want to register the timer just yet, then see {@link TimedPhase#stop}. * * @see TimedPhase#stop() * @see TimedPhase#registerTime() */ @Override public void close() { stop(); registerTime(); } }
/** * Start a stopwatch. * Time is accumulated if the stopwatch is already registered * * @param timePhaseName the name of this stopwatch * * @return The stopwatch */ public static TimedPhase startTiming(String timePhaseName) { RequestLog current = RLOG.get(); TimedPhase timePhase = current.times.get(timePhaseName); if (timePhase == null) { // If it was the first phase in general, create logging context as well if (current.info == null) { current.init(); } timePhase = new TimedPhase(timePhaseName); current.times.put(timePhaseName, timePhase); } current.mostRecentTimer = timePhase; return timePhase.start(); }
/** * Check if a stopwatch is currently running. * * @param timePhaseName the name of this stopwatch * * @return whether this stopwatch is started */ public static boolean isRunning(String timePhaseName) { RequestLog current = RLOG.get(); TimedPhase timePhase = current.times.get(timePhaseName); return timePhase != null && timePhase.isRunning(); }
/** * Pause a stopwatch. * * @param timePhaseName the name of this stopwatch */ public static void stopTiming(String timePhaseName) { TimedPhase timePhase = RLOG.get().times.get(timePhaseName); if (timePhase == null) { LOG.warn("Tried to stop non-existent phase: {}", timePhaseName); return; } timePhase.close(); }
/** * Stop the most recent stopwatch. * * @deprecated Stopping a timer based on context is brittle, and prone to unexpected changes. Timers should be * stopped explicitly, or started in a try-with-resources block */ @Deprecated public static void stopMostRecentTimer() { try { stopTiming(RLOG.get().mostRecentTimer.getName()); } catch (NullPointerException ignored) { LOG.warn("Stopping timing failed because mostRecentTimer wasn't registered."); } }
/** * Start the phase. * * @return This phase after being started */ public TimedPhase start() { if (isRunning()) { LOG.warn("Tried to start timer that is already running: {}", name); } else { start = System.nanoTime(); } return this; }
/** * Stop the phase. * <p> * This method just stops the timer. It does not register the time with the {@link RequestLog}. To register * the timer, invoke {@link TimedPhase#registerTime()}. To do both with a single method call, see * {@link TimedPhase#close()} * * @see TimedPhase#registerTime() * @see TimedPhase#close() */ public void stop() { if (!isRunning()) { LOG.warn("Tried to stop timer that has not been started: {}", name); return; } duration += System.nanoTime() - start; start = 0; }