Example usage for com.google.common.base Stopwatch isRunning

List of usage examples for com.google.common.base Stopwatch isRunning

Introduction

In this page you can find the example usage for com.google.common.base Stopwatch isRunning.

Prototype

boolean isRunning

To view the source code for com.google.common.base Stopwatch isRunning.

Click Source Link

Usage

From source file:GuavaStopwatch.java

private static void demonstrateStopwatch() {
    Stopwatch stopWatch = new Stopwatch();
    System.out.println("Is stopwatch running? " + stopWatch.isRunning());

    stopWatch.start();//  w  ww . ja  v  a 2s  .  c o  m

    for (int i = 0; i < 1000; i++) {
        System.out.println("Is stopwatch running? " + stopWatch.isRunning());
    }

    stopWatch.stop();
    System.out.println("Our loop took : " + stopWatch.elapsedTime(TimeUnit.MILLISECONDS) + " milliseconds");
}

From source file:com.facebook.buck.distributed.DistBuildSlaveTimingStatsTracker.java

public void stopTimer(SlaveEvents event) {
    Stopwatch watch = Preconditions.checkNotNull(watches.get(event));
    Preconditions.checkState(watch.isRunning(), "Stopwatch for %s has already been stopped.", event);
    watch.stop();/*from  w  w  w  .  ja v a  2  s.  co m*/
}

From source file:com.facebook.buck.distributed.DistBuildSlaveTimingStatsTracker.java

public long getElapsedTimeMs(SlaveEvents event) {
    Stopwatch watch = Preconditions.checkNotNull(watches.get(event));
    Preconditions.checkState(!watch.isRunning(), "Stopwatch for %s is still running.", event);
    return watch.elapsed(TimeUnit.MILLISECONDS);
}

From source file:com.facebook.buck.distributed.build_slave.BuildSlaveTimingStatsTracker.java

public synchronized void stopTimer(SlaveEvents event) {
    Stopwatch watch = Objects.requireNonNull(watches.get(event));
    Preconditions.checkState(watch.isRunning(), "Stopwatch for %s has already been stopped.", event);
    watch.stop();/*from w w  w .  j a v a  2 s.  com*/
}

From source file:com.facebook.buck.distributed.build_slave.BuildSlaveTimingStatsTracker.java

public synchronized long getElapsedTimeMs(SlaveEvents event) {
    Stopwatch watch = Objects.requireNonNull(watches.get(event));
    Preconditions.checkState(!watch.isRunning(), "Stopwatch for %s is still running.", event);
    return watch.elapsed(TimeUnit.MILLISECONDS);
}

From source file:org.apache.kudu.client.DeadlineTracker.java

/**
 * Creates a new tracker, using the specified stopwatch, and starts it right now.
 * The stopwatch is reset if it was already running.
 * @param stopwatch Specific Stopwatch to use
 *///from   w w  w . j a v a  2s  .c  o  m
public DeadlineTracker(Stopwatch stopwatch) {
    if (stopwatch.isRunning()) {
        stopwatch.reset();
    }
    this.stopwatch = stopwatch.start();
}

From source file:com.arpnetworking.tsdaggregator.perf.FilePerfTestBase.java

/**
 * Runs a test.//  w  w w .  j a v a 2  s  .c om
 *
 * @param pipelineConfigurationFile Pipeline configuration file.
 * @param duration Timeout period.
 */
protected void benchmark(final File pipelineConfigurationFile, final Duration duration) {
    LOGGER.debug(String.format("Launching pipeline; configuration=%s", pipelineConfigurationFile));

    // Create custom "canary" sink
    final CountDownLatch latch = new CountDownLatch(1);
    final Stopwatch timer = Stopwatch.createUnstarted();
    final ListeningSink sink = new ListeningSink(new Function<Collection<AggregatedData>, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable final Collection<AggregatedData> input) {
            if (input != null) {
                final AggregatedData datum = Iterables.getFirst(input, null);
                if (datum != null && TestFileGenerator.CANARY.equals(datum.getFQDSN().getMetric())
                        && timer.isRunning()) {
                    timer.stop();
                    latch.countDown();
                }
            }
            return null;
        }
    });

    // Load the specified stock configuration
    final PipelineConfiguration stockPipelineConfiguration = new StaticConfiguration.Builder()
            .addSource(new JsonNodeFileSource.Builder().setFile(pipelineConfigurationFile).build())
            .setObjectMapper(PipelineConfiguration.createObjectMapper(_injector)).build()
            .getRequiredAs(PipelineConfiguration.class);

    // Add the custom "canary" sink
    final List<Sink> benchmarkSinks = Lists.newArrayList(stockPipelineConfiguration.getSinks());
    benchmarkSinks.add(sink);

    // Create the custom configuration
    final PipelineConfiguration benchmarkPipelineConfiguration = OvalBuilder.<PipelineConfiguration, PipelineConfiguration.Builder>clone(
            stockPipelineConfiguration).setSinks(benchmarkSinks).build();

    // Instantiate the pipeline
    final Pipeline pipeline = new Pipeline(benchmarkPipelineConfiguration);

    // Execute the pipeline until the canary flies the coop
    try {
        timer.start();
        pipeline.launch();

        if (!latch.await(duration.getMillis(), TimeUnit.MILLISECONDS)) {
            LOGGER.error("Test timed out");
            throw new RuntimeException("Test timed out");
        }
    } catch (final InterruptedException e) {
        Thread.interrupted();
        throw new RuntimeException("Test interrupted");
    } finally {
        pipeline.shutdown();
    }
}

From source file:org.sonatype.sisu.bl.support.DefaultBundle.java

/**
 * Starts application and waits for it to boot. if successfully started sets the state to running.
 * <p/>// ww  w.j  a  va2  s.  c om
 * {@inheritDoc}
 *
 * @throws Exception if a problem occurred during startup of application, wait period or it could not determine if
 *                   application is started in specified timeout
 * @see Bundle#start()
 */
@Override
public void doStart() {
    bootingTime = Time.millis(0);
    final Stopwatch bootingWatch = Stopwatch.createUnstarted();
    try {
        startApplication();
        running = true;
        getRunningBundles().add(this);
        bootingWatch.start();
        waitForBoot();
    } catch (RuntimeException e) {
        doStop();
        throw e;
    } finally {
        if (bootingWatch.isRunning()) {
            bootingWatch.stop();
        }
        bootingTime = Time.millis(bootingWatch.elapsed(TimeUnit.MILLISECONDS));
    }
}

From source file:org.opendaylight.controller.netconf.persist.impl.ConfigPusherImpl.java

/**
 * First calls {@link #getOperationServiceWithRetries(java.util.Set, String)} in order to wait until
 * expected capabilities are present, then tries to push configuration. If {@link ConflictingVersionException}
 * is caught, whole process is retried - new service instance need to be obtained from the factory. Closes
 * {@link NetconfOperationService} after each use.
 *//*from   w  w w .j a va2  s  .c  o m*/
private synchronized EditAndCommitResponse pushConfigWithConflictingVersionRetries(
        ConfigSnapshotHolder configSnapshotHolder) throws NetconfDocumentedException {
    ConflictingVersionException lastException;
    Stopwatch stopwatch = Stopwatch.createUnstarted();
    do {
        String idForReporting = configSnapshotHolder.toString();
        SortedSet<String> expectedCapabilities = checkNotNull(configSnapshotHolder.getCapabilities(),
                "Expected capabilities must not be null - %s, check %s", idForReporting,
                configSnapshotHolder.getClass().getName());
        try (NetconfOperationService operationService = getOperationServiceWithRetries(expectedCapabilities,
                idForReporting)) {
            if (!stopwatch.isRunning()) {
                stopwatch.start();
            }
            return pushConfig(configSnapshotHolder, operationService);
        } catch (ConflictingVersionException e) {
            lastException = e;
            LOG.info("Conflicting version detected, will retry after timeout");
            sleep();
        }
    } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < conflictingVersionTimeoutMillis);
    throw new IllegalStateException("Max wait for conflicting version stabilization timeout after "
            + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms", lastException);
}

From source file:org.opendaylight.controller.config.persist.impl.ConfigPusherImpl.java

private synchronized boolean pushConfigWithConflictingVersionRetries(ConfigSnapshotHolder configSnapshotHolder)
        throws ConfigSnapshotFailureException {
    ConflictingVersionException lastException;
    Stopwatch stopwatch = Stopwatch.createUnstarted();
    do {/*w  ww  . j a  va  2  s. co m*/
        //TODO wait untill all expected modules are in yangStoreService, do we even need to with yangStoreService instead on netconfOperationService?
        String idForReporting = configSnapshotHolder.toString();
        SortedSet<String> expectedCapabilities = checkNotNull(configSnapshotHolder.getCapabilities(),
                "Expected capabilities must not be null - %s, check %s", idForReporting,
                configSnapshotHolder.getClass().getName());

        // wait max time for required capabilities to appear
        waitForCapabilities(expectedCapabilities, idForReporting);
        try {
            if (!stopwatch.isRunning()) {
                stopwatch.start();
            }
            return pushConfig(configSnapshotHolder);
        } catch (ConflictingVersionException e) {
            lastException = e;
            LOG.info("Conflicting version detected, will retry after timeout");
            sleep();
        }
    } while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < conflictingVersionTimeoutMillis);
    throw new IllegalStateException("Max wait for conflicting version stabilization timeout after "
            + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms", lastException);
}