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

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

Introduction

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

Prototype

@CheckReturnValue
public long elapsed(TimeUnit desiredUnit) 

Source Link

Document

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

Usage

From source file:org.apache.tez.log.LogParser.java

public static void main(String[] args) throws IOException {
    Preconditions.checkArgument(args.length == 1, "Please provide the file to be parsed");
    File inputFile = new File(args[0]);
    Preconditions.checkArgument(inputFile.exists(),
            "Please provide valid file. " + inputFile + " does not exist");

    Stopwatch sw = Stopwatch.createStarted();

    LogParser parser = new LogParser(inputFile);

    parser.process();//w  w w . j a  va2s. c  o  m
    System.out.println();

    IAnalyzer vertexMappingAnalyzer = parser.getAnalyzers().get(VertexMappingAnalyzer.class.getName());
    IAnalyzer vertexFinishedAnalyzer = parser.getAnalyzers().get(VertexFinishedAnalyzer.class.getName());
    if (vertexMappingAnalyzer != null && vertexFinishedAnalyzer != null) {
        System.out.println("Vertices that haven't finished");
        System.out.println("*******************************");
        Map<String, String> vertexMapping = (Map<String, String>) vertexMappingAnalyzer.getResult();
        Map<VertexFinishedAnalyzer.VertexFinished, String> vertexFinishedMap = (Map<VertexFinishedAnalyzer.VertexFinished, String>) vertexFinishedAnalyzer
                .getResult();

        for (Map.Entry<String, String> e : vertexMapping.entrySet()) {
            boolean found = false;
            for (Map.Entry<VertexFinishedAnalyzer.VertexFinished, String> fe : vertexFinishedMap.entrySet()) {
                if (fe.getKey().vertexId.equalsIgnoreCase(e.getKey())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                System.out.println(e.getKey() + " is not in finished map list. " + e.getValue());
            }
        }
    }

    /**
     * In case shuffle-blamed-for details is there, co-relate with rack details
     */
    IAnalyzer shuffleBlamedFor = parser.getAnalyzers().get(ShuffleBlamedForAnalyzer.class.getName());
    IAnalyzer rackResolver = parser.getAnalyzers().get(RackResolverExtractor.class.getName());
    if (shuffleBlamedFor != null && rackResolver != null) {
        // machine --> rack mapping
        Map<String, String> rackMap = (Map<String, String>) rackResolver.getResult();

        ShuffleBlamedForAnalyzer.ShuffleBlamedForResult result = (ShuffleBlamedForAnalyzer.ShuffleBlamedForResult) shuffleBlamedFor
                .getResult();

        parser.addAdditionalAnalysis("Source machine details..", true);
        for (String srcMachine : result.getSrcMachines()) {
            //machine:45454, containerPriority= 8, containerResources=<memory:3584, vCores:1>
            String machine = srcMachine.substring(0, srcMachine.indexOf(":"));
            parser.addAdditionalAnalysis(machine + " --> " + rackMap.get(machine));
        }

        parser.addAdditionalAnalysis("");
        parser.addAdditionalAnalysis("");
        parser.addAdditionalAnalysis("Fetcher machine details..", true);
        for (String fetcherMachine : result.getFetcherMachines()) {
            //machine:45454, containerPriority= 8, containerResources=<memory:3584, vCores:1>
            String machine = fetcherMachine.substring(0, fetcherMachine.indexOf(":"));
            parser.addAdditionalAnalysis(machine + " --> " + rackMap.get(machine));
        }
    }

    /**
     * For containers timeouts. Relate ContainerTimeoutAnalyzer and NodesAnalyzer
     *
     */
    IAnalyzer containerTimeoutAnalyzer = parser.getAnalyzers().get(ContainerTimeoutAnalyzer.class.getName());
    IAnalyzer nodesAnalyzer = parser.getAnalyzers().get(NodesAnalyzer.class.getName());
    if (nodesAnalyzer != null && containerTimeoutAnalyzer != null) {
        List<String> containersWithTimeout = (List<String>) containerTimeoutAnalyzer.getResult();

        // Node --> <attempt, container>
        Map<String, Map<String, String>> nodesResult = (Map<String, Map<String, String>>) nodesAnalyzer
                .getResult();

        parser.addAdditionalAnalysis("");
        parser.addAdditionalAnalysis("Container time outs and attempt/node details", true);
        for (String container : containersWithTimeout) {
            for (Map.Entry<String, Map<String, String>> nodeEntry : nodesResult.entrySet()) {
                Map<String, String> attemptToContainer = nodeEntry.getValue();
                for (Map.Entry<String, String> attemptEntry : attemptToContainer.entrySet()) {
                    if (attemptEntry.getValue().equalsIgnoreCase(container)) {
                        parser.addAdditionalAnalysis(
                                container + " --> " + nodeEntry.getKey() + " --> " + attemptEntry.getKey());
                    }
                }
            }
        }
        parser.addAdditionalAnalysis("");
    }

    /**
     * Task attempts not finished
     */
    IAnalyzer taskAttemptStarted = parser.getAnalyzers().get(TaskAttemptStartedAnalyzer.class.getName());
    IAnalyzer taskAttemptFinished = parser.getAnalyzers().get(TaskAttemptFinishedAnalyzer.class.getName());
    if (taskAttemptFinished != null && taskAttemptStarted != null) {
        Map<String, TaskAttemptStartedAnalyzer.TaskAttemptStarted> started = (Map<String, TaskAttemptStartedAnalyzer.TaskAttemptStarted>) taskAttemptStarted
                .getResult();
        Map<String, TaskAttemptFinishedAnalyzer.TaskAttemptFinished> finished = (Map<String, TaskAttemptFinishedAnalyzer.TaskAttemptFinished>) taskAttemptFinished
                .getResult();

        parser.addAdditionalAnalysis(
                "List of unfinished tasks!! started=" + started.size() + ", " + "finished=" + finished.size(),
                true);
        for (String task : started.keySet()) {
            //check if this task is in finished keys
            if (!finished.keySet().contains(task)) {
                parser.addAdditionalAnalysis(task + " is not in finished list");
            }
        }
    }

    /**
     * For swimlanes (not including killed tasks)
     */
    /*
    TODO: Need to work on this.
            
            
    IAnalyzer nodeAnalyzer = parser.getAnalyzers()
        .get(NodesAnalyzer.class.getName());
    IAnalyzer taFinishedAnalyzer = parser.getAnalyzers()
        .get(TaskAttemptFinishedAnalyzer.class.getName());
    if (nodeAnalyzer != null && taFinishedAnalyzer != null) {
      // machine --> task --> container
      Map<String, Map<String, String>> nodes =
          (Map<String, Map<String, String>>) nodeAnalyzer.getResult();
      // taskIDStr --> taskAttemptFinished
      Map<String, TaskAttemptFinishedAnalyzer.TaskAttemptFinished> taFinishedMap =
          (Map<String, TaskAttemptFinishedAnalyzer.TaskAttemptFinished>)
      taFinishedAnalyzer.getResult();
            
      //Dirty hack to get all DAG
      Set<String> allDags = Sets.newHashSet();
      for(Map.Entry<String, Map<String, String>> entry : nodes.entrySet()) {
        for (Map.Entry<String, String> taskEntry : entry.getValue().entrySet()) {
          String taskId = taskEntry.getKey();
          //attempt_1478350923850_0006_7
          allDags.add(taskId.substring(0, 28));
        }
      }
            
      // Construct a map of machine_container --> List<TaskAttemptId> from analyzer dataset.
      final Map<String, TreeSet<TaskAttemptFinishedAnalyzer.TaskAttemptFinished>> mapping = Maps.newHashMap();
      long minTime = Long.MAX_VALUE;
      long maxTime = Long.MIN_VALUE;
      for(String dag : allDags) {
        for (Map.Entry<String, Map<String, String>> entry : nodes.entrySet()) {
          for (Map.Entry<String, String> taskEntry : entry.getValue().entrySet()) {
    String machine = entry.getKey();
            
    String taskId = taskEntry.getKey();
    String containerId = taskEntry.getValue();
            
    if (!taskId.contains("1478350923850_0006_9")) {
      continue;
    }
            
    String machineContainer = machine + "_" + containerId;
    TreeSet<TaskAttemptFinishedAnalyzer.TaskAttemptFinished> attempts = mapping.get
        (machineContainer);
            
    if (attempts == null) {
      attempts = new TreeSet<>(
          new Comparator<TaskAttemptFinishedAnalyzer.TaskAttemptFinished>() {
            @Override public int compare(TaskAttemptFinishedAnalyzer.TaskAttemptFinished o1,
                TaskAttemptFinishedAnalyzer.TaskAttemptFinished o2) {
              if (Long.parseLong(o1.startTime) < Long.parseLong(o2.startTime)) {
                return -1;
              } else if (Long.parseLong(o1.startTime) > Long.parseLong(o2.startTime)) {
                return 1;
              } else {
                return 0;
              }
            }
          });
      mapping.put(machineContainer, attempts);
    }
            
    //Check if the attempt id is available in finished maps
    if (taFinishedMap.containsKey(taskId)) {
      TaskAttemptFinishedAnalyzer.TaskAttemptFinished attempt = taFinishedMap.get(taskId);
      attempts.add(attempt);
      if (Long.parseLong(attempt.finishTime) >= maxTime) {
        maxTime = Long.parseLong(attempt.finishTime);
      } else if (Long.parseLong(attempt.startTime) <= minTime) {
        minTime = Long.parseLong(attempt.startTime);
      }
    }
          }
        }
      }
            
      // draw SVG
      System.out.println("MinTime: " + minTime + ". maxTime: " + maxTime);
      SVGUtils svg = new SVGUtils(minTime, maxTime, new TreeSet(mapping.keySet()));
      int yOffset = 1;
      for(Map.Entry<String, TreeSet<TaskAttemptFinishedAnalyzer.TaskAttemptFinished>> entry :
          mapping.entrySet()) {
        for (TaskAttemptFinishedAnalyzer.TaskAttemptFinished task : entry.getValue()) {
          //draw lines
          svg.drawStep(task.vertexId, Long.parseLong(task.startTime), Long.parseLong(task
          .timeTaken), yOffset, "LightGreen");
        }
        yOffset++;
      }
            
      svg.saveFileStr("/tmp/test.svg");
      System.out.println("Wrote to /tmp/test.svg");
            
      // Now generate the swimlane.
            
            
    }
    */

    System.out.println();
    parser.writeAnalysis();

    System.out.println("Time taken " + (sw.elapsed(TimeUnit.SECONDS)) + " seconds");
    sw.stop();
}

From source file:com.google.devtools.build.android.AndroidResourceMerger.java

/** Merges all secondary resources with the primary resources. */
public static MergedAndroidData mergeData(final ParsedAndroidData primary, final Path primaryManifest,
        final List<? extends SerializedAndroidData> direct,
        final List<? extends SerializedAndroidData> transitive, final Path resourcesOut, final Path assetsOut,
        @Nullable final PngCruncher cruncher, final VariantType type, @Nullable final Path symbolsOut,
        @Nullable AndroidResourceClassWriter rclassWriter, AndroidDataDeserializer deserializer)
        throws MergingException {
    Stopwatch timer = Stopwatch.createStarted();
    final ListeningExecutorService executorService = MoreExecutors
            .listeningDecorator(Executors.newFixedThreadPool(15));
    try (Closeable closeable = ExecutorServiceCloser.createWith(executorService)) {
        AndroidDataMerger merger = AndroidDataMerger.createWithPathDeduplictor(executorService, deserializer);
        UnwrittenMergedAndroidData merged = mergeData(executorService, transitive, direct, primary,
                primaryManifest, type != VariantType.LIBRARY, deserializer);
        timer.reset().start();/*from  w ww.j  av a  2 s  .  c  o  m*/
        if (symbolsOut != null) {
            AndroidDataSerializer serializer = AndroidDataSerializer.create();
            merged.serializeTo(serializer);
            serializer.flushTo(symbolsOut);
            logger.fine(
                    String.format("serialize merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
            timer.reset().start();
        }
        if (rclassWriter != null) {
            merged.writeResourceClass(rclassWriter);
            logger.fine(String.format("write classes finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
            timer.reset().start();
        }
        AndroidDataWriter writer = AndroidDataWriter.createWith(resourcesOut.getParent(), resourcesOut,
                assetsOut, cruncher, executorService);
        return merged.write(writer);
    } catch (IOException e) {
        throw MergingException.wrapException(e).build();
    } finally {
        logger.fine(String.format("write merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    }
}

From source file:javalibs.Out.java

public String timer_millis(Stopwatch sw) {
    return sw.elapsed(TimeUnit.MILLISECONDS) + " milliseconds";
}

From source file:javalibs.Out.java

public String timer_secs(Stopwatch sw) {
    return sw.elapsed(TimeUnit.SECONDS) + " seconds";
}

From source file:javalibs.Out.java

public String timer_mins(Stopwatch sw) {
    return sw.elapsed(TimeUnit.MINUTES) + " minutes";
}

From source file:org.cinchapi.concourse.server.ManagedConcourseServer.java

/**
 * Install a Concourse Server in {@code directory} using {@code installer}.
 * //from   w w w . j a v a2 s .com
 * @param installer
 * @param directory
 * @return the server install directory
 */
private static String install(String installer, String directory) {
    try {
        Files.createDirectories(Paths.get(directory));
        Path binary = Paths.get(directory + File.separator + TARGET_BINARY_NAME);
        Files.deleteIfExists(binary);
        Files.copy(Paths.get(installer), binary);
        ProcessBuilder builder = new ProcessBuilder(Lists.newArrayList("sh", binary.toString()));
        builder.directory(new File(directory));
        builder.redirectErrorStream();
        Process process = builder.start();
        // The concourse-server installer prompts for an admin password in
        // order to make optional system wide In order to get around this
        // prompt, we have to "kill" the process, otherwise the server
        // install will hang.
        Stopwatch watch = Stopwatch.createStarted();
        while (watch.elapsed(TimeUnit.SECONDS) < 1) {
            continue;
        }
        watch.stop();
        process.destroy();
        TerminalFactory.get().restore();
        String application = directory + File.separator + "concourse-server"; // the install directory for the
                                                                              // concourse-server application
        process = Runtime.getRuntime().exec("ls " + application);
        List<String> output = Processes.getStdOut(process);
        if (!output.isEmpty()) {
            configure(application);
            log.info("Successfully installed server in {}", application);
            return application;
        } else {
            throw new RuntimeException(MessageFormat.format(
                    "Unsuccesful attempt to " + "install server at {0} " + "using binary from {1}", directory,
                    installer));
        }

    } catch (Exception e) {
        throw Throwables.propagate(e);
    }

}

From source file:com.spotify.heroic.HeroicCore.java

static boolean awaitLifeCycles(final String op, final CoreComponent primary, final Duration await,
        final List<LifeCycleNamedHook<AsyncFuture<Void>>> hooks)
        throws InterruptedException, ExecutionException {
    log.info("[{}] {} lifecycle(s)...", op, hooks.size());

    final AsyncFramework async = primary.async();

    final List<AsyncFuture<Void>> futures = new ArrayList<>();
    final List<Pair<AsyncFuture<Void>, LifeCycleHook<AsyncFuture<Void>>>> pairs = new ArrayList<>();

    for (final LifeCycleNamedHook<AsyncFuture<Void>> hook : hooks) {
        log.trace("[{}] {}", op, hook.id());

        final AsyncFuture<Void> future;

        try {// w  w  w .  j  a v a  2s .co  m
            future = hook.get();
        } catch (Exception e) {
            futures.add(async.failed(e));
            break;
        }

        if (log.isTraceEnabled()) {
            final Stopwatch w = Stopwatch.createStarted();

            future.onFinished(() -> {
                log.trace("[{}] {}, took {}us", op, hook.id(), w.elapsed(TimeUnit.MICROSECONDS));
            });
        }

        futures.add(future);
        pairs.add(Pair.of(future, hook));
    }

    try {
        async.collect(futures).get(await.getDuration(), await.getUnit());
    } catch (final TimeoutException e) {
        log.error("Operation timed out");

        for (final Pair<AsyncFuture<Void>, LifeCycleHook<AsyncFuture<Void>>> pair : pairs) {
            if (!pair.getLeft().isDone()) {
                log.error("{}: did not finish in time: {}", op, pair.getRight());
            }
        }

        return false;
    }

    log.info("[{}] {} lifecycle(s) done", op, hooks.size());
    return true;
}

From source file:qa.qcri.nadeef.core.utils.CSVTools.java

/**
 * Append CSV file content into a database table.
 * @param tableName target table name./* w  ww.  j  a  va  2s  . co  m*/
 * @param dbConfig DB connection config.
 * @param file CSV file.
 * @return new created table name.
 */
public static HashSet<Integer> append(DBConfig dbConfig, SQLDialectBase dialectManager, String tableName,
        File file) throws Exception {
    Preconditions.checkNotNull(dbConfig);
    Preconditions.checkNotNull(dialectManager);

    Stopwatch stopwatch = Stopwatch.createStarted();
    HashSet<Integer> result = Sets.newHashSet();
    try {
        boolean hasTableExist = DBMetaDataTool.isTableExist(dbConfig, tableName);

        // Create table
        if (!hasTableExist) {
            throw new IllegalArgumentException("Table " + tableName + " does not exist.");
        }

        // get the current max tid.
        int startTid = DBMetaDataTool.getMaxTid(dbConfig, tableName) + 1;

        // load the data
        int size = 0;

        if (dialectManager.supportBulkLoad()) {
            size = dialectManager.bulkLoad(dbConfig, tableName, file.toPath(), true);
        } else {
            size = dialectManager.fallbackLoad(dbConfig, tableName, file, true);
        }

        logger.info("Appended " + size + " bytes in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms.");
        stopwatch.stop();

        // build the tid set.
        int endTid = DBMetaDataTool.getMaxTid(dbConfig, tableName);
        for (int i = startTid; i <= endTid; i++) {
            result.add(i);
        }

    } catch (Exception ex) {
        logger.error("Cannot load file " + file.getName(), ex);
    }
    return result;
}

From source file:qa.qcri.nadeef.core.util.CSVTools.java

/**
 * Append CSV file content into a database table.
 * @param tableName target table name.//from   w  w w .  j  a  v a 2 s .c  o  m
 * @param dbConfig DB connection config.
 * @param file CSV file.
 * @return new created table name.
 */
public static HashSet<Integer> append(DBConfig dbConfig, SQLDialectBase dialectManager, String tableName,
        File file) throws Exception {
    Preconditions.checkNotNull(dbConfig);
    Preconditions.checkNotNull(dialectManager);

    Tracer tracer = Tracer.getTracer(CSVTools.class);
    Stopwatch stopwatch = Stopwatch.createStarted();
    HashSet<Integer> result = Sets.newHashSet();
    try {
        boolean hasTableExist = DBMetaDataTool.isTableExist(dbConfig, tableName);

        // Create table
        if (!hasTableExist) {
            throw new IllegalArgumentException("Table " + tableName + " does not exist.");
        }

        // get the current max tid.
        int startTid = DBMetaDataTool.getMaxTid(dbConfig, tableName) + 1;

        // load the data
        int size = 0;

        if (dialectManager.supportBulkLoad()) {
            size = dialectManager.bulkLoad(dbConfig, tableName, file.toPath(), true);
        } else {
            size = dialectManager.fallbackLoad(dbConfig, tableName, file, true);
        }

        tracer.info("Appended " + size + " bytes in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms.");
        stopwatch.stop();

        // build the tid set.
        int endTid = DBMetaDataTool.getMaxTid(dbConfig, tableName);
        for (int i = startTid; i <= endTid; i++) {
            result.add(i);
        }

    } catch (Exception ex) {
        tracer.err("Cannot load file " + file.getName(), ex);
    }
    return result;
}

From source file:com.jaredjstewart.RandomStockPriceGenerator.java

@Override
public void run() {
    int putCounter = 0;
    Stopwatch stopWatch = Stopwatch.createStarted();

    while (stopWatch.elapsed(TimeUnit.SECONDS) < 2) {
        String tickerSymbol = getRandomTickerSymbol();
        putRandomStockPriceInGeode(tickerSymbol);
        putCounter++;/*from   w w  w.j a  va2s .  c  o m*/
    }
    stopWatch.stop();
    System.out.println("==============");
    System.out.format("RandomStockPriceGenerator made %d updates in the last two seconds\n", putCounter);
}