Example usage for org.apache.commons.lang.time StopWatch getTime

List of usage examples for org.apache.commons.lang.time StopWatch getTime

Introduction

In this page you can find the example usage for org.apache.commons.lang.time StopWatch getTime.

Prototype

public long getTime() 

Source Link

Document

Get the time on the stopwatch.

This is either the time between the start and the moment this method is called, or the amount of time between start and stop.

Usage

From source file:ubic.gemma.loader.genome.gene.ncbi.NcbiGeneLoader.java

/**
 * @param geneQueue/*from  w  w w .  j ava 2 s .  co  m*/
 */
void doLoad(final BlockingQueue<Gene> geneQueue) {
    StopWatch timer = new StopWatch();
    timer.start();
    int skipped = 0;
    while (!(converterDone.get() && geneQueue.isEmpty())) {
        Gene gene = null;
        try {
            // the converted genes.
            gene = geneQueue.poll();
            if (gene == null) {
                continue;
            }

            if (gene.getProducts().isEmpty()) {
                // // log.warn( gene + " has no products, skipping" ); // common!!!
                // skipped++;
            }

            persisterHelper.persistOrUpdate(gene);

            if (++loadedGeneCount % 1000 == 0 || timer.getTime() > 30 * 1000) {
                log.info("Processed " + loadedGeneCount + " genes. Queue has " + geneQueue.size()
                        + " items; last gene: " + gene);
                if (skipped > 0) {
                    log.info(skipped + " skipped because they had no gene products.");
                }
                timer.reset();
                timer.start();
            }

        } catch (Exception e) {
            log.error("Error while loading gene: " + gene + ": " + e.getMessage(), e);
            loaderDone.set(true);
            throw new RuntimeException(e);
        }
    }
    log.info("Loaded " + loadedGeneCount + " genes. ");
    loaderDone.set(true);
}

From source file:ubic.gemma.loader.genome.goldenpath.GoldenPathBioSequenceLoader.java

/**
 * @param bioSequences//ww  w .ja v  a2s . c  o  m
 */
void load(BlockingQueue<BioSequence> queue) {
    log.debug("Entering 'load' ");

    StopWatch timer = new StopWatch();
    timer.start();

    int count = 0;
    int cpt = 0;
    double secspt = 0.0;

    Collection<BioSequence> bioSequencesToPersist = new ArrayList<BioSequence>();
    try {
        while (!(producerDone && queue.isEmpty())) {
            BioSequence sequence = queue.poll();

            if (sequence == null) {
                continue;
            }

            sequence.getSequenceDatabaseEntry().setExternalDatabase(genbank);
            sequence.setTaxon(taxon);
            bioSequencesToPersist.add(sequence);
            if (++count % BATCH_SIZE == 0) {
                bioSequenceService.create(bioSequencesToPersist);
                bioSequencesToPersist.clear();
            }

            // just some timing information.
            if (count % 1000 == 0) {
                cpt++;
                timer.stop();
                double secsperthousand = timer.getTime() / 1000.0;
                secspt += secsperthousand;
                double meanspt = secspt / cpt;

                String progString = "Processed and loaded " + count + " sequences, last one was "
                        + sequence.getName() + " (" + secsperthousand + "s for last 1000, mean per 1000 ="
                        + String.format("%.1f", meanspt) + "s)";
                log.info(progString);
                timer.reset();
                timer.start();
            }

        }
    } catch (Exception e) {
        consumerDone = true;
        throw new RuntimeException(e);
    }

    // finish up.
    bioSequenceService.create(bioSequencesToPersist);

    log.info("Loaded total of " + count + " sequences");
    consumerDone = true;

}

From source file:ubic.gemma.loader.genome.ProbeSequenceParser.java

@Override
public void parse(InputStream is) throws IOException {

    if (is == null)
        throw new IllegalArgumentException("InputStream was null");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StopWatch timer = new StopWatch();
    timer.start();/*from   www .  jav a2 s  .c  o  m*/
    int nullLines = 0;
    String line = null;
    int linesParsed = 0;
    while ((line = br.readLine()) != null) {

        BioSequence newItem = parseOneLine(line);

        if (++linesParsed % PARSE_ALERT_FREQUENCY == 0 && timer.getTime() > PARSE_ALERT_TIME_FREQUENCY_MS) {
            String message = "Parsed " + linesParsed + " lines ";
            log.info(message);
            timer.reset();
            timer.start();
        }

        if (newItem == null) {
            nullLines++;
            continue;
        }

    }
    log.info("Parsed " + linesParsed + " lines. "
            + (nullLines > 0 ? nullLines + " yielded no parse result (they may have been filtered)." : ""));

    br.close();
}

From source file:ubic.gemma.loader.util.fetcher.AbstractFetcher.java

/**
 * @param future/*from ww w  . j a  va  2s.co  m*/
 * @return true if it finished normally, false if it was cancelled.
 */
protected boolean waitForDownload(FutureTask<Boolean> future) {
    StopWatch timer = new StopWatch();
    timer.start();
    long lastTime = timer.getTime();
    while (!future.isDone() && !future.isCancelled()) {
        try {
            Thread.sleep(INFO_UPDATE_INTERVAL);
        } catch (InterruptedException ie) {
            log.info("Cancelling download");
            boolean cancelled = future.cancel(true);
            if (cancelled) {
                log.info("Download stopped successfully.");
                return false;
            }
            throw new RuntimeException("Cancellation failed.");

        }

        if (log.isInfoEnabled() && timer.getTime() > (lastTime + 2000L)) {
            log.info("Waiting ... " + timer.getTime() + "ms elapsed....");
        }
    }
    return true;
}

From source file:ubic.gemma.loader.util.fetcher.AbstractFetcher.java

/**
 * @param future/* ww w.ja  v a 2s . co m*/
 * @param expectedSize
 * @param outputFileName
 * @return true if it finished normally, false if it was cancelled.
 */
protected boolean waitForDownload(FutureTask<Boolean> future, long expectedSize, File outputFile) {
    int iters = 0;
    long previousSize = 0;
    StopWatch idleTimer = new StopWatch();
    while (!future.isDone() && !future.isCancelled()) {
        try {
            Thread.sleep(INFO_UPDATE_INTERVAL);
        } catch (InterruptedException ie) {
            log.info("Cancelling download");
            boolean cancelled = future.cancel(true);
            if (cancelled) {
                return false;
            }

            // double check...
            if (future.isCancelled() || future.isDone()) {
                return false;
            }

            log.error("Cancellation of actual download might not have happend? Task says it was not cancelled: "
                    + future);

            return false;

        }

        /*
         * Avoid logging too much. If we're waiting for a long download, reduce frequency of updates.
         */
        if (outputFile.length() < expectedSize
                && (iters < NUMBER_OF_TIMES_TO_LOG_WAITING_BEFORE_REDUCING_VERBOSITY
                        || iters % NUMBER_OF_TIMES_TO_LOG_WAITING_BEFORE_REDUCING_VERBOSITY == 0)) {

            double percent = 100.00 * outputFile.length() / expectedSize;

            // can cause npe error, breaking hot deploy
            if (log != null && log.isInfoEnabled()) {
                log.info((outputFile.length() + (expectedSize > 0 ? "/" + expectedSize : "") + " bytes read ("
                        + String.format("%.1f", percent) + "%)"));
            }

            if (previousSize == outputFile.length()) {
                /*
                 * Possibly consider bailing after a while.
                 */
                if (idleTimer.getTime() > STALLED_BAIL_TIME_LIMIT) {
                    log.warn("Download does not seem to be happening, bailing");
                    return false;
                }
                if (idleTimer.getTime() == 0)
                    idleTimer.start();
            } else {
                idleTimer.reset();
                idleTimer.start();
            }
        }

        if (outputFile.length() >= expectedSize) {
            // no special action, it will finish soon enough.
        }

        previousSize = outputFile.length();

        iters++;
    }
    if (iters == 0)
        log.info("File with size " + outputFile.length() + " bytes.");
    return true;
}

From source file:ubic.gemma.loader.util.fetcher.FtpArchiveFetcher.java

/**
 * @param newDir//w w w .j  a  va 2s.  com
 * @param seekFile
 */
protected void unPack(final File toUnpack) {
    FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
        @Override
        @SuppressWarnings("synthetic-access")
        public Boolean call() {
            File extractedFile = new File(FileTools.chompExtension(toUnpack.getAbsolutePath()));
            /*
             * Decide if an existing file is plausibly usable. Err on the side of caution.
             */
            if (allowUseExisting && extractedFile.canRead() && extractedFile.length() >= toUnpack.length()
                    && !FileUtils.isFileNewer(toUnpack, extractedFile)) {
                log.warn("Expanded file exists, skipping re-expansion: " + extractedFile);
                return Boolean.TRUE;
            }

            if (expander != null) {
                expander.setSrc(toUnpack);
                expander.setDest(toUnpack.getParentFile());
                expander.perform();
            } else if (toUnpack.getAbsolutePath().toLowerCase().endsWith("zip")) {
                try {
                    FileTools.unZipFiles(toUnpack.getAbsolutePath());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            } else { // gzip.
                try {
                    FileTools.unGzipFile(toUnpack.getAbsolutePath());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            return Boolean.TRUE;
        }

    });
    ExecutorService executor = Executors.newSingleThreadExecutor();

    executor.execute(future);
    executor.shutdown();

    StopWatch s = new StopWatch();
    s.start();
    while (!future.isDone() && !future.isCancelled()) {
        try {
            Thread.sleep(INFO_UPDATE_INTERVAL);
        } catch (InterruptedException ie) {
            future.cancel(true);
            return;
        }
        log.info("Unpacking archive ... " + Math.floor(s.getTime() / 1000.0) + " seconds elapsed");
    }
}

From source file:ubic.gemma.loader.util.parser.BasicLineMapParser.java

@Override
public void parse(InputStream is) throws IOException {

    if (is == null)
        throw new IllegalArgumentException("InputStream was null");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    StopWatch timer = new StopWatch();
    timer.start();/*from   w ww .  j  av  a2  s .  co  m*/

    int nullLines = 0;
    String line = null;
    int linesParsed = 0;
    while ((line = br.readLine()) != null) {

        if (line.startsWith(COMMENTMARK)) {
            continue;
        }
        T newItem = parseOneLine(line);

        if (newItem == null) {
            nullLines++;
            continue;
        }

        K key = getKey(newItem);
        if (key == null) {
            throw new IllegalStateException("Got null key for item " + linesParsed);
        }
        put(key, newItem);

        if (++linesParsed % PARSE_ALERT_FREQUENCY == 0 && timer.getTime() > PARSE_ALERT_TIME_FREQUENCY_MS) {
            String message = "Parsed " + linesParsed + " lines, last had key " + key;
            log.info(message);
            timer.reset();
            timer.start();
        }

    }
    log.info("Parsed " + linesParsed + " lines. "
            + (nullLines > 0 ? nullLines + " yielded no parse result (they may have been filtered)." : ""));

    br.close();
}

From source file:ubic.gemma.loader.util.parser.BasicLineParser.java

@Override
public void parse(InputStream is) throws IOException {

    linesParsed = 0;/*from   w w w . j ava 2 s.c o  m*/
    int nullLines = 0;

    if (br == null) {
        if (is == null) {
            throw new IllegalArgumentException("Inputstream null");
        }

        if (is.available() == 0) {
            throw new IOException("No bytes available to read from inputStream");
        }
        br = new BufferedReader(new InputStreamReader(is));
    }

    String line = null;

    StopWatch timer = new StopWatch();
    timer.start();
    while ((line = br.readLine()) != null) {

        if (line.startsWith(COMMENTMARK)) {
            continue;
        }

        T newItem = parseOneLine(line);

        if (newItem != null) {
            addResult(newItem);
        } else {
            // if ( log.isDebugEnabled() ) log.debug( "Got null parse from " + line );
            nullLines++;
        }

        if (++linesParsed % PARSE_ALERT_FREQUENCY == 0 && timer.getTime() > PARSE_ALERT_TIME_FREQUENCY_MS) {
            String message = "Parsed " + linesParsed + " lines...";
            log.info(message);
            timer.reset();
            timer.start();
        }

    }

    if (linesParsed > MIN_PARSED_LINES_FOR_UPDATE && this.getResults() != null) {
        log.info("Parsed " + linesParsed + " lines, " + this.getResults().size() + " items");
    }
    if (nullLines > 0)
        log.info(nullLines + " yielded no parse result (they may have been filtered).");
}

From source file:ubic.gemma.loader.util.parser.LineMapParser.java

@Override
public void parse(InputStream is) throws IOException {

    if (is == null) {
        throw new IllegalArgumentException("Inputstream null");
    }/*  ww  w. ja v  a2 s  .c o  m*/

    if (is.available() == 0) {
        throw new IOException("No bytes available to read from inputStream");
    }

    linesParsed = 0;
    int nullLines = 0;
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    StopWatch timer = new StopWatch();
    timer.start();
    String line = null;

    while ((line = br.readLine()) != null) {

        if (line.startsWith(COMMENTMARK)) {
            continue;
        }

        // The returned object is just used to check if the was data in the line. Storing the data has to be taken
        // care of by the subclass.
        Object newItem = parseOneLine(line);

        if (newItem == null) {
            nullLines++;
        }

        if (++linesParsed % PARSE_ALERT_FREQUENCY == 0 && timer.getTime() > PARSE_ALERT_TIME_FREQUENCY_MS) {
            String message = "Parsed " + linesParsed + " lines...";
            log.info(message);
            timer.reset();
            timer.start();
        }

    }

    if (linesParsed > MIN_PARSED_LINES_FOR_UPDATE) {
        log.info("Parsed " + linesParsed + " lines, " + this.getResults().size() + " items");
    }
    if (nullLines > 0)
        log.info(nullLines + " yielded no parse result (they may have been filtered).");
}

From source file:ubic.gemma.loader.util.ParserAndLoaderTools.java

/**
 * User the loader to persist the collection.
 * /*from   w ww.  ja  v  a2  s .  c o m*/
 * @param loader
 * @param col
 */
public static final void loadDatabase(Persister loader, Collection<?> col) {
    assert (loader != null);
    assert (col != null);
    if (col.size() == 0)
        return;

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    loader.persist(col);

    stopWatch.stop();

    long time = stopWatch.getTime();
    if (time < 1000)
        log.info("Time taken: " + time + " ms.");
    else {
        log.info("Time taken: " + time / 1000 + " s.");
    }
}