com.google.common.base.Stopwatch.elapsedTime()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(291)

本文整理了Java中com.google.common.base.Stopwatch.elapsedTime()方法的一些代码示例,展示了Stopwatch.elapsedTime()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Stopwatch.elapsedTime()方法的具体详情如下:
包路径:com.google.common.base.Stopwatch
类名称:Stopwatch
方法名:elapsedTime

Stopwatch.elapsedTime介绍

[英]Returns the current elapsed time shown on this stopwatch, expressed in the desired time unit, with any fraction rounded down.

Note that the overhead of measurement can be more than a microsecond, so it is generally not useful to specify TimeUnit#NANOSECONDSprecision here.
[中]返回此秒表上显示的当前运行时间,以所需的时间单位表示,任何分数向下舍入。
请注意,测量的开销可能超过一微秒,因此在此处指定时间单位#纳秒精度通常没有用处。

代码示例

代码示例来源:origin: forcedotcom/phoenix

logger.error(String.format("Time taken to process [%s] events was [%s] seconds",events.size(),watch.stop().elapsedTime(TimeUnit.SECONDS)));
if( transaction != null ) {
  transaction.close();

代码示例来源:origin: org.sonatype.sisu/sisu-guava

/**
 * Returns the current elapsed time shown on this stopwatch, expressed
 * in milliseconds, with any fraction rounded down. This is identical to
 * {@code elapsedTime(TimeUnit.MILLISECONDS}.
 */
public long elapsedMillis() {
 return elapsedTime(MILLISECONDS);
}

代码示例来源:origin: org.sonatype.sisu/sisu-guava

public long elapsedNanos() {
 return stopwatch.elapsedTime(NANOSECONDS);
}

代码示例来源:origin: jcgay/maven-profiler

private static long totalTime(Map<Artifact, Stopwatch> times) {
  long totalTime = 0;
  for (Stopwatch stopwatch : filter(times.values(), notNull())) {
    totalTime += stopwatch.elapsedTime(TimeUnit.NANOSECONDS);
  }
  return totalTime;
}

代码示例来源:origin: org.apache.omid/omid-tso-server

public void timerStop(String name) {
  if (flag) {
    LOG.warn("timerStop({}) called after publish. Measurement was ignored. {}", name, Throwables.getStackTraceAsString(new Exception()));
    return;
  }
  Stopwatch activeStopwatch = timers.get(name);
  if (activeStopwatch == null) {
    throw new IllegalStateException(
        String.format("There is no %s timer in the %s monitoring context.", name, this));
  }
  activeStopwatch.stop();
  elapsedTimeMsMap.put(name, activeStopwatch.elapsedTime(TimeUnit.NANOSECONDS));
  timers.remove(name);
}

代码示例来源:origin: org.apache.omid/omid-tso-server-hbase1.x

public void timerStop(String name) {
  if (flag) {
    LOG.warn("timerStop({}) called after publish. Measurement was ignored. {}", name, Throwables.getStackTraceAsString(new Exception()));
    return;
  }
  Stopwatch activeStopwatch = timers.get(name);
  if (activeStopwatch == null) {
    throw new IllegalStateException(
        String.format("There is no %s timer in the %s monitoring context.", name, this));
  }
  activeStopwatch.stop();
  elapsedTimeMsMap.put(name, activeStopwatch.elapsedTime(TimeUnit.NANOSECONDS));
  timers.remove(name);
}

代码示例来源:origin: org.apache.omid/tso-server

public void timerStop(String name) {
  if (flag) {
    LOG.warn("timerStop({}) called after publish. Measurement was ignored. {}", name, Throwables.getStackTraceAsString(new Exception()));
    return;
  }
  Stopwatch activeStopwatch = timers.get(name);
  if (activeStopwatch == null) {
    throw new IllegalStateException(
        String.format("There is no %s timer in the %s monitoring context.", name, this));
  }
  activeStopwatch.stop();
  elapsedTimeMsMap.put(name, activeStopwatch.elapsedTime(TimeUnit.NANOSECONDS));
  timers.remove(name);
}

代码示例来源:origin: stackoverflow.com

Stopwatch st = new Stopwatch();

// Do something. In your case Upload image !

double time = st.elapsedTime(); // the result in millis

// Print time

代码示例来源:origin: stackoverflow.com

Stopwatch st = new Stopwatch();
// Do smth. here
double time = st.elapsedTime(); // the result in millis

代码示例来源:origin: springernature/omelet

@Override
public void run() {
  Stopwatch sw = new Stopwatch();
  sw.start();
  LOGGER.info("Please wait while we are building your testData");
  StringBuilder sb = new StringBuilder();
  while(keepRunning){
    sb.append(".");
    LOGGER.info(sb.toString());
    sleep(1);
  }
  LOGGER.info("Time taken to build data in seconds is:"+sw.elapsedTime(TimeUnit.SECONDS));
  sw.stop();
  
}

代码示例来源:origin: caskdata/cdap

@Override
 public void run(DatasetContext context) throws Exception {
  Stopwatch stopwatch = new Stopwatch().start();
  Table table = LoggingStoreTableUtil.getMetadataTable(context, datasetManager);
  for (DeletedEntry entry : toDeleteRows) {
   if (stopwatch.elapsedTime(TimeUnit.SECONDS) >= cutOffTransactionTimeout) {
    break;
   }
   table.delete(entry.getRowKey());
   deletedEntries.add(entry);
  }
  stopwatch.stop();
  LOG.info("Deleted {} metadata entries in {} ms", deletedEntries.size(), stopwatch.elapsedMillis());
 }
});

代码示例来源:origin: co.cask.cdap/cdap-watchdog

@Override
 public void run(DatasetContext context) throws Exception {
  Stopwatch stopwatch = new Stopwatch().start();
  Table table = LoggingStoreTableUtil.getMetadataTable(context, datasetManager);
  for (DeletedEntry entry : toDeleteRows) {
   if (stopwatch.elapsedTime(TimeUnit.SECONDS) >= cutOffTransactionTimeout) {
    break;
   }
   table.delete(entry.getRowKey());
   deletedEntries.add(entry);
  }
  stopwatch.stop();
  LOG.info("Deleted {} metadata entries in {} ms", deletedEntries.size(), stopwatch.elapsedMillis());
 }
});

代码示例来源:origin: cdapio/cdap

public void waitForLeader(long duration, TimeUnit unit) throws InterruptedException, TimeoutException {
 Stopwatch timer = new Stopwatch().start();
 do {
  if (!leader.get()) {
   unit.sleep(duration / 10);
  }
 } while (!leader.get() && timer.elapsedTime(unit) < duration);
 if (!leader.get()) {
  throw new TimeoutException("Timed out waiting to become leader");
 }
}

代码示例来源:origin: co.cask.cdap/cdap-data-fabric

@Override
public int read(Collection<? super StreamEventOffset> events, int maxEvents,
        long timeout, TimeUnit unit, ReadFilter readFilter) throws IOException, InterruptedException {
 int eventsRead = 0;
 Stopwatch stopwatch = new Stopwatch();
 stopwatch.start();
 while (eventsRead < maxEvents && !(emptySources.isEmpty() && eventSources.isEmpty())) {
  if (!emptySources.isEmpty()) {
   prepareEmptySources(readFilter);
  }
  eventsRead += read(events, readFilter);
  if (eventSources.isEmpty() && stopwatch.elapsedTime(unit) >= timeout) {
   break;
  }
 }
 return (eventsRead == 0 && emptySources.isEmpty() && eventSources.isEmpty()) ? -1 : eventsRead;
}

代码示例来源:origin: jclouds/legacy-jclouds

public void testQueueDeletedRecentlyRetriesWhen59SleepsAndTries() {
 SQSErrorRetryHandler retry = new SQSErrorRetryHandler(createMock(AWSUtils.class),
    createMock(BackoffLimitedRetryHandler.class), ImmutableSet.<String> of(), 60, 100);
 HttpCommand command = createHttpCommandForFailureCount(59);
 Stopwatch watch = new Stopwatch().start();
 assertTrue(retry.shouldRetryRequestOnError(command, response, error));
 assertEquals(command.getFailureCount(), 60);
 // allow for slightly inaccurate system timers
 assertTrue(watch.stop().elapsedTime(TimeUnit.MILLISECONDS) >= 98);
}

代码示例来源:origin: jclouds/legacy-jclouds

public void testQueueDeletedRecentlyRetriesWhen60DoesntTry() {
 SQSErrorRetryHandler retry = new SQSErrorRetryHandler(createMock(AWSUtils.class),
    createMock(BackoffLimitedRetryHandler.class), ImmutableSet.<String> of(), 60, 100);
 HttpCommand command = createHttpCommandForFailureCount(60);
 Stopwatch watch = new Stopwatch().start();
 assertFalse(retry.shouldRetryRequestOnError(command, response, error));
 assertEquals(command.getFailureCount(), 61);
 assertTrue(watch.stop().elapsedTime(TimeUnit.MILLISECONDS) < 100);
}

代码示例来源:origin: cdapio/cdap

public void waitForKey(int keyId, long duration, TimeUnit unit) throws InterruptedException, TimeoutException {
 if (keyManager instanceof WaitableDistributedKeyManager) {
  WaitableDistributedKeyManager waitKeyManager = (WaitableDistributedKeyManager) keyManager;
  Stopwatch timer = new Stopwatch().start();
  boolean hasKey = false;
  do {
   hasKey = waitKeyManager.hasKey(keyId);
   if (!hasKey) {
    unit.sleep(duration / 10);
   }
  } while (!hasKey && timer.elapsedTime(unit) < duration);
  if (!hasKey) {
   throw new TimeoutException("Timed out waiting for key " + keyId);
  }
 }
}

代码示例来源:origin: cdapio/cdap

private void waitForEntry(SharedResourceCache<String> cache, String key, String expectedValue,
              long timeToWaitMillis) throws InterruptedException {
  String value = cache.get(key);
  boolean isPresent = expectedValue.equals(value);

  Stopwatch watch = new Stopwatch().start();
  while (!isPresent && watch.elapsedTime(TimeUnit.MILLISECONDS) < timeToWaitMillis) {
   TimeUnit.MILLISECONDS.sleep(200);
   value = cache.get(key);
   isPresent = expectedValue.equals(value);
  }

  if (!isPresent) {
   throw new RuntimeException("Timed out waiting for expected value '" + expectedValue + "' in cache");
  }
 }
}

代码示例来源:origin: cdapio/cdap

public void waitForCurrentKey(long duration, TimeUnit unit) throws InterruptedException, TimeoutException {
  if (keyManager instanceof WaitableDistributedKeyManager) {
   WaitableDistributedKeyManager waitKeyManager = (WaitableDistributedKeyManager) keyManager;
   Stopwatch timer = new Stopwatch().start();
   boolean hasKey = false;
   do {
    hasKey = waitKeyManager.getCurrentKey() != null;
    if (!hasKey) {
     unit.sleep(duration / 10);
    }
   } while (!hasKey && timer.elapsedTime(unit) < duration);
   if (!hasKey) {
    throw new TimeoutException("Timed out waiting for current key to be set");
   }
  }
 }
}

代码示例来源:origin: jclouds/legacy-jclouds

protected ServiceStats trackAvailabilityOfProcessOnNode(Statement process, String processName, NodeMetadata node) {
 ServiceStats stats = new ServiceStats();
 Stopwatch watch = new Stopwatch().start();
 ExecResponse exec = client.runScriptOnNode(node.getId(), process, runAsRoot(false).wrapInInitScript(false));
 stats.backgroundProcessMilliseconds = watch.elapsedTime(TimeUnit.MILLISECONDS);
 watch.reset().start();
 
 HostAndPort socket = null;
 try {
   socket = openSocketFinder.findOpenSocketOnNode(node, 8080, 60, TimeUnit.SECONDS);
 } catch (NoSuchElementException e) {
   throw new NoSuchElementException(format("%s%n%s%s", e.getMessage(), exec.getOutput(), exec.getError()));
 }
 stats.socketOpenMilliseconds = watch.elapsedTime(TimeUnit.MILLISECONDS);
 getAnonymousLogger().info(format("<< %s on node(%s)[%s] %s", processName, node.getId(), socket, stats));
 return stats;
}

相关文章