List of usage examples for com.google.common.base Stopwatch start
public Stopwatch start()
From source file:jobs.ComputeStratifiedFrequencies.java
@Override public void doJob() throws Exception { Logger.info("Frequency computation started..."); Stopwatch stopwatch = Stopwatch.createUnstarted(); stopwatch.start(); int now = Integer.parseInt((String) play.Play.configuration.get("analysis.year")); int y1 = now - 1; Logger.info("Previous year: " + y1); Logger.info("Reading index..."); Directory directory = FSDirectory.open(VirtualFile.fromRelativePath("/indexes/index-" + y1).getRealFile()); DirectoryReader ireader = DirectoryReader.open(directory); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47); this.isearcher = new IndexSearcher(ireader); this.parser = new QueryParser(Version.LUCENE_47, "contents", analyzer); //Retrieve all the phrases in the database, and compute Logger.info("Retrieving phrases..."); List<Phrase> phrases = Phrase.findAll(); int total = phrases.size(); int counter = 0; Map<Long, Double> frequencies = new HashMap<Long, Double>(); for (Phrase phrase : phrases) { Stopwatch time = Stopwatch.createUnstarted(); time.start();// ww w . j a v a 2 s. co m counter++; Logger.info("i: " + counter + "/" + total + " (" + phrase.value + ")"); int frequency = query(phrase.value); time.stop(); Logger.info("- Query time: " + time.elapsed(TimeUnit.MILLISECONDS)); //Try to save it to debug frequencies.put(phrase.id, (double) frequency); } // Phrase.em().flush(); // Phrase.em().clear(); counter = 0; for (Long id : frequencies.keySet()) { Phrase phrase = Phrase.findById(id); phrase.frequency1y = frequencies.get(id); phrase.save(); counter++; Logger.info("Counter: " + counter); if (counter % 1000 == 0) { Phrase.em().flush(); Phrase.em().clear(); } } ireader.close(); directory.close(); Logger.info("Job done."); stopwatch.stop(); Utils.emailAdmin("Stratified index built", "Job finished in " + stopwatch.elapsed(TimeUnit.MINUTES) + " minutes."); }
From source file:org.apache.eagle.alert.coordinator.ExclusiveExecutor.java
public void execute(String path, final Runnable r, int timeoutMillis) throws TimeoutException { final AtomicBoolean executed = new AtomicBoolean(false); Stopwatch watch = Stopwatch.createUnstarted(); watch.start(); LeaderSelectorListener listener = new LeaderSelectorListenerAdapter() { @Override//from w w w . j av a 2 s .co m public void takeLeadership(CuratorFramework client) throws Exception { // this callback will get called when you are the leader // do whatever leader work you need to and only exit // this method when you want to relinquish leadership LOG.info("this is leader node right now.."); executed.set(true); try { r.run(); } catch (Throwable t) { LOG.warn("failed to run exclusive executor", t); } LOG.info("leader node executed done!.."); } @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { LOG.info( String.format("leader selector state change listener, new state: %s", newState.toString())); } }; selector = new LeaderSelector(client, path, listener); // selector.autoRequeue(); // not required, but this is behavior that you // will probably expect selector.start(); // wait for given times while (watch.elapsed(TimeUnit.MILLISECONDS) < timeoutMillis) { //about 3 minutes waiting if (!executed.get()) { try { Thread.sleep(ACQUIRE_LOCK_WAIT_INTERVAL_MS); } catch (InterruptedException e) { // ignored } continue; } else { break; } } watch.stop(); if (!executed.get()) { throw new TimeoutException(String.format( "Get exclusive lock for operation on path %s failed due to wait too much time: %d ms", path, watch.elapsed(TimeUnit.MILLISECONDS))); } LOG.info("Exclusive operation done with execution time (lock plus operation) {} ms !", watch.elapsed(TimeUnit.MILLISECONDS)); }
From source file:org.apache.drill.exec.store.AffinityCalculator.java
/** * For a given RowGroup, calculate how many bytes are available on each on drillbit endpoint * * @param rowGroup the RowGroup to calculate endpoint bytes for *//*from ww w .j a va 2 s . co m*/ public void setEndpointBytes(ParquetGroupScan.RowGroupInfo rowGroup) { Stopwatch watch = new Stopwatch(); watch.start(); String fileName = rowGroup.getPath(); if (!blockMapMap.containsKey(fileName)) { buildBlockMap(fileName); } ImmutableRangeMap<Long, BlockLocation> blockMap = blockMapMap.get(fileName); HashMap<String, Long> hostMap = new HashMap<>(); HashMap<DrillbitEndpoint, Long> endpointByteMap = new HashMap(); long start = rowGroup.getStart(); long end = start + rowGroup.getLength(); Range<Long> rowGroupRange = Range.closedOpen(start, end); // Find submap of ranges that intersect with the rowGroup ImmutableRangeMap<Long, BlockLocation> subRangeMap = blockMap.subRangeMap(rowGroupRange); // Iterate through each block in this submap and get the host for the block location for (Map.Entry<Range<Long>, BlockLocation> block : subRangeMap.asMapOfRanges().entrySet()) { String[] hosts; Range<Long> blockRange = block.getKey(); try { hosts = block.getValue().getHosts(); } catch (IOException ioe) { throw new RuntimeException("Failed to get hosts for block location", ioe); } Range<Long> intersection = rowGroupRange.intersection(blockRange); long bytes = intersection.upperEndpoint() - intersection.lowerEndpoint(); // For each host in the current block location, add the intersecting bytes to the corresponding endpoint for (String host : hosts) { DrillbitEndpoint endpoint = getDrillBitEndpoint(host); if (endpointByteMap.containsKey(endpoint)) { endpointByteMap.put(endpoint, endpointByteMap.get(endpoint) + bytes); } else { if (endpoint != null) endpointByteMap.put(endpoint, bytes); } } } rowGroup.setEndpointBytes(endpointByteMap); rowGroup.setMaxBytes(endpointByteMap.size() > 0 ? Collections.max(endpointByteMap.values()) : 0); logger.debug("Row group ({},{}) max bytes {}", rowGroup.getPath(), rowGroup.getStart(), rowGroup.getMaxBytes()); watch.stop(); logger.debug("Took {} ms to set endpoint bytes", watch.elapsed(TimeUnit.MILLISECONDS)); }
From source file:com.netflix.servo.monitor.DurationTimer.java
/** * Returns a stopwatch that has been started and will automatically * record its result to this timer when stopped. Every time this method is called * the number of active tasks for the timer will be incremented. * The number will be decremented when the stopwatch is stopped. *//*from w w w . j a v a2 s . c o m*/ public Stopwatch start() { Stopwatch s = new DurationStopwatch(); s.start(); return s; }
From source file:jobs.SaveIndexJob.java
@Override public void doJob() throws Exception { Logger.info("Saving index in DB..."); MorphiaFixtures.delete(MorphiaPhrase.class); Stopwatch stopwatch = Stopwatch.createUnstarted(); stopwatch.start(); //TODO do not hardcode the date Directory directoryNow = FSDirectory .open(VirtualFile.fromRelativePath("/indexes/index-2013").getRealFile()); DirectoryReader ireaderNow = DirectoryReader.open(directoryNow); Terms termsNow = SlowCompositeReaderWrapper.wrap(ireaderNow).terms("contents"); TermsEnum iteratorNow = termsNow.iterator(null); BytesRef byteRef;//from w w w . j a v a 2 s . c om int counter = 0; while ((byteRef = iteratorNow.next()) != null) { counter++; String term = new String(byteRef.bytes, byteRef.offset, byteRef.length); int frequency = iteratorNow.docFreq(); //Saves only the terms with high frequency //Removes the terms with a _ (from shingle index) if (frequency > FREQ_TRESHOLD && !term.contains("_") && !term.matches(".*\\d\\s.*") && !term.matches(".*\\s\\d.*")) { new MorphiaPhrase(term, frequency).save(); Logger.info("Term Now (" + counter + "): " + term + " - freq: " + frequency); } } ireaderNow.close(); directoryNow.close(); //Compares against the index of previous year Directory directoryThen = FSDirectory .open(VirtualFile.fromRelativePath("/indexes/index-2012").getRealFile()); DirectoryReader ireaderThen = DirectoryReader.open(directoryThen); Terms termsThen = SlowCompositeReaderWrapper.wrap(ireaderThen).terms("contents"); TermsEnum iteratorThen = termsThen.iterator(null); BytesRef byteRefThen; counter = 0; while ((byteRefThen = iteratorThen.next()) != null) { counter++; String term = new String(byteRefThen.bytes, byteRefThen.offset, byteRefThen.length); int frequency = iteratorThen.docFreq(); //Saves only the terms with high frequency //Removes the terms with a _ (from shingle index) if (frequency > FREQ_TRESHOLD && !term.contains("_") && !term.matches(".*\\d\\s.*") && !term.matches(".*\\s\\d.*")) { MorphiaPhrase phrase = MorphiaPhrase.find("value", term).first(); if (phrase != null) { phrase.frequencyThen = frequency; phrase.save(); //TODO compute OIF here Logger.info("Term Then (" + counter + "): " + term + " - freq: " + frequency); } } } ireaderThen.close(); directoryThen.close(); stopwatch.stop(); Utils.emailAdmin("Indexing in DB done. ", "Job finished in " + stopwatch.elapsed(TimeUnit.MINUTES) + " minutes."); Logger.info("index done and saved."); }
From source file:org.apache.drill.exec.physical.impl.xsort.BatchGroup.java
public void addBatch(VectorContainer newContainer) throws IOException { assert fs != null; assert path != null; if (outputStream == null) { outputStream = fs.create(path);/* ww w . j a v a2s.c om*/ } int recordCount = newContainer.getRecordCount(); WritableBatch batch = WritableBatch.getBatchNoHVWrap(recordCount, newContainer, false); VectorAccessibleSerializable outputBatch = new VectorAccessibleSerializable(batch, allocator); Stopwatch watch = new Stopwatch(); watch.start(); outputBatch.writeToStream(outputStream); newContainer.zeroVectors(); logger.debug("Took {} us to spill {} records", watch.elapsed(TimeUnit.MICROSECONDS), recordCount); spilledBatches++; }
From source file:jobs.EvaluateRetrieval.java
@Override public void doJob() throws Exception { Path filePath = VirtualFile.fromRelativePath("/data/mesh_disease_terms.txt").getRealFile().toPath(); Charset charset = Charset.defaultCharset(); List<String> lines = Files.readAllLines(filePath, charset); Stopwatch stopwatch = Stopwatch.createUnstarted(); stopwatch.start(); int total = lines.size(); int counter = 0; //TODO just to store the resutls and printing file StringBuilder sb = new StringBuilder(); sb.append("PMID\tMESH_ID\tMESH_TERM\n"); for (String line : lines) { String[] splits = line.split("\t"); String id = splits[0];/*from w w w.jav a 2 s.co m*/ String term = splits[1]; String originalTerm = splits[1]; counter++; Logger.info("Term: " + term + "(" + counter + "/" + total + ")"); if (term.contains(",")) { Pattern p = Pattern.compile("(.*), (.*)"); Matcher m = p.matcher(term); if (m.find()) { String post = m.group(1); String pre = m.group(2); term = pre + " " + post; Logger.info("Term modified: " + term); } } Directory directory = FSDirectory.open(VirtualFile.fromRelativePath("/index").getRealFile()); DirectoryReader ireader = DirectoryReader.open(directory); //TODO Query analyzer - can be changed and switched Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47); IndexSearcher isearcher = new IndexSearcher(ireader); //Maybe different type of parser? QueryParser parser = new QueryParser(Version.LUCENE_47, "contents", analyzer); // Logger.info("Query: " + term); if (!term.contains("(")) { //TODO different syntax and operators Query query = parser.parse("\"" + term.replace("/", "\\/") + "\""); //Logger.info("query: " + query.toString()); ScoreDoc[] hits = isearcher.search(query, null, 10000).scoreDocs; //Logger.info("results: " + hits.length); int freq = hits.length; if (freq > 0) { for (int i = 0; i < hits.length; i++) { Document hitDoc = isearcher.doc(hits[i].doc); //Logger.info(hitDoc.get("pmid") + " - " + hits[i].score); sb.append(hitDoc.get("pmid")).append("\t").append(id).append("\t").append(originalTerm) .append("\n"); } } } ireader.close(); directory.close(); } stopwatch.stop(); Logger.info("Time to index the documents: " + stopwatch.elapsed(TimeUnit.SECONDS)); File file = VirtualFile.fromRelativePath("/data/annotatedArticles.txt").getRealFile(); FileUtils.writeStringToFile(file, sb.toString()); Logger.info("File saved: " + file.getAbsolutePath()); }
From source file:com.espirit.moddev.cli.Cli.java
/** * Start the cli application.//w ww .jav a2s. com * * @param args the input arguments */ public void execute(final String[] args) { setLoggingSystemProperties(); final ExceptionHandler exceptionHandler = new ExceptionHandler(this, CliConstants.FS_CLI.value(), args); //Order of listeners registered is important! listeners.add(exceptionHandler); //Make sure that in case of error there will be a System-exit(1) listeners.add(new SystemExitListener()); try { final CliBuilder<Command> builder = getDefaultCliBuilder(); final Command command = parseCommandLine(args, builder); Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); executeCommand(exceptionHandler, command); stopwatch.stop(); double milliseconds = stopwatch.elapsedTime(TimeUnit.MILLISECONDS); LOGGER.info("Time: " + (milliseconds / CliConstants.ONE_SECOND_IN_MILLIS.valueAsInt()) + "s"); } catch (Exception e) { //NOSONAR fireErrorOccurredEvent(new CliErrorEvent(this, e)); } }
From source file:com.netflix.servo.monitor.BucketTimer.java
/** {@inheritDoc} */ @Override// w w w . ja v a 2 s .co m public Stopwatch start() { Stopwatch s = new TimedStopwatch(this); s.start(); return s; }
From source file:org.apache.drill.exec.physical.impl.xsort.BatchGroup.java
private VectorContainer getBatch() throws IOException { assert fs != null; assert path != null; if (inputStream == null) { inputStream = fs.open(path);// w w w .j a v a 2 s . c o m } VectorAccessibleSerializable vas = new VectorAccessibleSerializable(allocator); Stopwatch watch = new Stopwatch(); watch.start(); vas.readFromStream(inputStream); VectorContainer c = vas.get(); // logger.debug("Took {} us to read {} records", watch.elapsed(TimeUnit.MICROSECONDS), c.getRecordCount()); spilledBatches--; currentContainer.zeroVectors(); Iterator<VectorWrapper<?>> wrapperIterator = c.iterator(); for (VectorWrapper w : currentContainer) { TransferPair pair = wrapperIterator.next().getValueVector().makeTransferPair(w.getValueVector()); pair.transfer(); } currentContainer.setRecordCount(c.getRecordCount()); c.zeroVectors(); return c; }