private synchronized Duration elapsedErrorDuration() { if (errorStopwatch.isRunning()) { errorStopwatch.stop(); } long nanos = errorStopwatch.elapsed(TimeUnit.NANOSECONDS); return new Duration(nanos, TimeUnit.NANOSECONDS).convertTo(TimeUnit.SECONDS); }
public void start() { if (watch.isRunning()) { watch.reset().start(); } else { watch.start(); } }
@Override public void failed(Throwable t) { if (stopwatch.isRunning()) { long latencyNanos = stopwatch.elapsed(NANOSECONDS); recorder.recordFailure(t, latencyNanos); } else { recorder.recordSkip(t); } }
void transitionService(Service service, Service.State from, Service.State to) { Preconditions.checkNotNull(service); Preconditions.checkArgument(from != to); this.monitor.enter(); try { this.transitioned = true; if (!this.ready) { return; } Preconditions.checkState( this.servicesByState.remove(from, service), "Service %s not at the expected location in the state map %s", new Object[] {service, from}); Preconditions.checkState( this.servicesByState.put(to, service), "Service %s in the state map unexpectedly at %s", new Object[] {service, to}); Stopwatch stopwatch = (Stopwatch) this.startupTimers.get(service); if (from == Service.State.NEW) { stopwatch.start(); } if ((to.compareTo(Service.State.RUNNING) >= 0) && (stopwatch.isRunning())) { stopwatch.stop(); if (!(service instanceof ServiceManager.NoOpService)) { ServiceManager.logger.log( Level.FINE, "Started {0} in {1}.", new Object[] {service, stopwatch}); } } if (to == Service.State.FAILED) { fireFailedListeners(service); } if (this.states.count(Service.State.RUNNING) == this.numberOfServices) { fireHealthyListeners(); } else if (this.states.count(Service.State.TERMINATED) + this.states.count(Service.State.FAILED) == this.numberOfServices) { fireStoppedListeners(); } } finally { this.monitor.leave(); executeListeners(); } }
ImmutableMap<Service, Long> startupTimes() { this.monitor.enter(); List<Map.Entry<Service, Long>> loadTimes; try { loadTimes = Lists.newArrayListWithCapacity( this.states.size() - this.states.count(Service.State.NEW) + this.states.count(Service.State.STARTING)); for (Map.Entry<Service, Stopwatch> entry : this.startupTimers.entrySet()) { Service service = (Service) entry.getKey(); Stopwatch stopWatch = (Stopwatch) entry.getValue(); if ((!stopWatch.isRunning()) && (!this.servicesByState.containsEntry(Service.State.NEW, service)) && (!(service instanceof ServiceManager.NoOpService))) { loadTimes.add( Maps.immutableEntry( service, Long.valueOf(stopWatch.elapsed(TimeUnit.MILLISECONDS)))); } } } finally { this.monitor.leave(); } Collections.sort( loadTimes, Ordering.natural() .onResultOf( new Function() { public Long apply(Map.Entry<Service, Long> input) { return (Long) input.getValue(); } })); ImmutableMap.Builder<Service, Long> builder = ImmutableMap.builder(); for (Map.Entry<Service, Long> entry : loadTimes) { builder.put(entry); } return builder.build(); }
/** * false if started or paused, true otherwise (ie the timer is counting down, even if it is * expired) */ public boolean isNotPaused() { return stopwatch.isRunning(); }
/** pauses the timer, if running; no-op if not running */ public synchronized CountdownTimer pause() { if (stopwatch.isRunning()) stopwatch.stop(); return this; }
/** starts the timer, either initially or if {@link #pause()}d; no-op if already running */ public synchronized CountdownTimer start() { if (!stopwatch.isRunning()) stopwatch.start(); return this; }