Java Code Examples for org.apache.commons.lang.time.StopWatch#getTime()

The following examples show how to use org.apache.commons.lang.time.StopWatch#getTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultProcedureOperator.java    From ymate-platform-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public void execute() throws Exception {
    if (!this.executed) {
        StopWatch _time = new StopWatch();
        _time.start();
        try {
            __doExecute();
            // 执行过程未发生异常将标记已执行,避免重复执行
            this.executed = true;
        } finally {
            _time.stop();
            this.expenseTime = _time.getTime();
            //
            if (this.getConnectionHolder().getDataSourceCfgMeta().isShowSQL()) {
                _LOG.info(ExpressionUtils.bind("[${sql}]${param}[${count}][${time}]")
                        .set("sql", StringUtils.defaultIfBlank(this.sql, "@NULL"))
                        .set("param", __doSerializeParameters())
                        .set("count", "N/A")
                        .set("time", this.expenseTime + "ms").getResult());
            }
        }
    }
}
 
Example 2
Source File: MCRSolrIndexer.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Rebuilds solr's metadata index.
 *
 * @param list
 *            list of identifiers of the objects to index
 * @param solrClient
 *            solr server to index
 */
public static void rebuildMetadataIndex(List<String> list, SolrClient solrClient) {
    LOGGER.info("Re-building Metadata Index");
    if (list.isEmpty()) {
        LOGGER.info("Sorry, no documents to index");
        return;
    }

    StopWatch swatch = new StopWatch();
    swatch.start();
    int totalCount = list.size();
    LOGGER.info("Sending {} objects to solr for reindexing", totalCount);

    MCRXMLMetadataManager metadataMgr = MCRXMLMetadataManager.instance();
    MCRSolrIndexStatistic statistic = null;
    HashMap<MCRObjectID, MCRContent> contentMap = new HashMap<>((int) (BULK_SIZE * 1.4));
    int i = 0;
    for (String id : list) {
        i++;
        try {
            LOGGER.debug("Preparing \"{}\" for indexing", id);
            MCRObjectID objId = MCRObjectID.getInstance(id);
            MCRContent content = metadataMgr.retrieveContent(objId);
            contentMap.put(objId, content);
            if (i % BULK_SIZE == 0 || totalCount == i) {
                MCRSolrIndexHandler indexHandler = MCRSolrIndexHandlerFactory.getInstance()
                    .getIndexHandler(contentMap);
                indexHandler.setCommitWithin(BATCH_AUTO_COMMIT_WITHIN_MS);
                indexHandler.setSolrServer(solrClient);
                statistic = indexHandler.getStatistic();
                submitIndexHandler(indexHandler);
                contentMap = new HashMap<>((int) (BULK_SIZE * 1.4));
            }
        } catch (Exception ex) {
            LOGGER.error("Error creating index thread for object {}", id, ex);
        }
    }
    long durationInMilliSeconds = swatch.getTime();
    if (statistic != null) {
        statistic.addTime(durationInMilliSeconds);
    }
}
 
Example 3
Source File: AbstractOperator.java    From ymate-platform-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public void execute() throws Exception {
    if (!this.executed) {
        StopWatch _time = new StopWatch();
        _time.start();
        int _effectCounts = 0;
        try {
            _effectCounts = __doExecute();
            // 执行过程未发生异常将标记已执行,避免重复执行
            this.executed = true;
        } finally {
            _time.stop();
            this.expenseTime = _time.getTime();
            //
            if (_LOG.isInfoEnabled()) {
                DataSourceCfgMeta _meta = this.connectionHolder.getDataSourceCfgMeta();
                if (_meta.isShowSQL()) {
                    String _logStr = ExpressionUtils.bind("[${sql}]${param}[${count}][${time}]")
                            .set("sql", StringUtils.defaultIfBlank(this.sql, "@NULL"))
                            .set("param", __doSerializeParameters())
                            .set("count", _effectCounts + "")
                            .set("time", this.expenseTime + "ms").getResult();
                    StringBuilder _stackSB = new StringBuilder(_logStr);
                    if (_meta.isStackTraces()) {
                        String[] _tracePackages = StringUtils.split(_meta.getStackTracePackage(), "|");
                        StackTraceElement[] _stacks = new Throwable().getStackTrace();
                        if (_stacks != null && _stacks.length > 0) {
                            int _depth = _meta.getStackTraceDepth() <= 0 ? _stacks.length : (_meta.getStackTraceDepth() > _stacks.length ? _stacks.length : _meta.getStackTraceDepth());
                            if (_depth > 0) {
                                for (int _idx = 0; _idx < _depth; _idx++) {
                                    if (_tracePackages != null && _tracePackages.length > 0) {
                                        if (StringUtils.contains(_stacks[_idx].getClassName(), "$$EnhancerByCGLIB$$") || !StringUtils.startsWithAny(_stacks[_idx].getClassName(), _tracePackages)) {
                                            continue;
                                        }
                                    }
                                    _stackSB.append("\n\t--> ").append(_stacks[_idx]);
                                }
                            }
                        }
                    }
                    _LOG.info(_stackSB.toString());
                }
            }
        }
    }
}
 
Example 4
Source File: ConsumerDriver.java    From ameliant-tools with Apache License 2.0 4 votes vote down vote up
@Override
public void drive() {
    // A Consumer is not thread-safe
    // {@see http://kafka.apache.org/090/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html}
    // {@see http://kafka.apache.org/090/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html#multithreaded}
    try (KafkaConsumer<byte[], byte[]> consumer = new KafkaConsumer<>(consumerDefinition.getKafkaConfig())) {

        String topic = consumerDefinition.getTopic();
        log.info("Subscribing to {}", topic);
        if (consumerRebalanceListener == null) {
            consumer.subscribe(Collections.singletonList(topic));
        } else {
            consumer.subscribe(Collections.singletonList(topic), consumerRebalanceListener);
        }

        long messagesToReceive = consumerDefinition.getMessagesToReceive();
        log.info("Expecting {} messages", messagesToReceive);

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

        do {
            ConsumerRecords<byte[], byte[]> records = consumer.poll(consumerDefinition.getPollTimeout());
            if (records == null) {
                throw new IllegalStateException("null ConsumerRecords polled");
            } else {
                if (records.count() == 0) {
                    try {
                        log.info("No records fetched, pausing");
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    if (log.isTraceEnabled()) {
                        log.trace("Fetched {} records", records.count());
                    }
                    for (ConsumerRecord<byte[], byte[]> record : records) {
                        recordsFetched += 1;
                        applyReceiveDelay();
                        if (recordsFetched % consumerDefinition.getReportReceivedEvery() == 0) {
                            log.info("Received {} messages", recordsFetched);
                        }
                    }
                }
            }

            if (isShutdownRequested()) {
                break;
            }
            stopWatch.split();
        } while ((recordsFetched < messagesToReceive)
                && (stopWatch.getSplitTime() < consumerDefinition.getTestRunTimeout()));

        stopWatch.stop();
        if (isShutdownRequested()) {
            log.info("Shutting down");
        } else {
            long runTime = stopWatch.getTime();
            log.info("Done. Consumer received {} msgs in {} ms", messagesToReceive, runTime);

            double averageThroughput = (1000d / runTime) * messagesToReceive;
            log.info("Average throughput: {} msg/s", averageThroughput);
        }

    } finally {
        log.debug("Consumer closed");
        if (completionLatch != null) {
            completionLatch.countDown();
        }
    }
}