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

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

Introduction

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

Prototype

public StopWatch() 

Source Link

Document

Constructor.

Usage

From source file:org.apache.eagle.alert.engine.sorter.StreamWindowBenchmarkTest.java

public void sendDESCOrderedEventsToWindow(StreamWindow window, StreamWindowRepository.StorageType storageType,
        int num) {
    LOGGER.info("Sending {} events to {} ({})", num, window.getClass().getSimpleName(), storageType);
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();//from  w ww . j  av a  2s  . c  o m
    int i = 0;
    while (i < num) {
        PartitionedEvent event = MockSampleMetadataFactory.createPartitionedEventGroupedByName("sampleStream_1",
                (window.startTime() + i));
        window.add(event);
        i++;
    }
    stopWatch.stop();
    performanceReport.put(num + "\tInsertTime\t" + storageType, stopWatch.getTime());
    LOGGER.info("Inserted {} events in {} ms", num, stopWatch.getTime());
    stopWatch.reset();
    stopWatch.start();
    window.flush();
    stopWatch.stop();
    performanceReport.put(num + "\tReadTime\t" + storageType, stopWatch.getTime());
}

From source file:org.apache.eagle.alert.engine.sorter.StreamWindowBenchmarkTest.java

private void benchmarkTest(StreamWindow window, StreamWindowRepository.StorageType storageType) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();//from   w  w w  .j  ava2  s  .com
    LOGGER.info("\n===== Benchmark Test for {} ({}) =====", window.getClass().getSimpleName(), storageType);
    metricReporter.report();
    sendDESCOrderedEventsToWindow(window, storageType, 1000);
    metricReporter.report();
    sendDESCOrderedEventsToWindow(window, storageType, 10000);
    metricReporter.report();
    sendDESCOrderedEventsToWindow(window, storageType, 100000);
    metricReporter.report();
    sendDESCOrderedEventsToWindow(window, storageType, 1000000);
    metricReporter.report();
    stopWatch.stop();
    LOGGER.info("\n===== Finished in total {} ms =====\n", stopWatch.getTime());
}

From source file:org.apache.eagle.jpm.mr.history.MRHistoryJobDailyReporter.java

private Map<String, Object> buildAlertData(String site, long startTime, long endTime) {
    StopWatch watch = new StopWatch();
    Map<String, Object> data = new HashMap<>();
    this.client = new EagleServiceClientImpl(config);
    String startTimeStr = DateTimeUtil.millisecondsToHumanDateWithSeconds(startTime);
    String endTimeStr = DateTimeUtil.millisecondsToHumanDateWithSeconds(endTime);
    LOG.info("Going to report job summery info for site {} from {} to {}", site, startTimeStr, endTimeStr);
    try {/*from   w ww  .j  a va 2s.  co  m*/
        watch.start();
        data.putAll(buildJobSummery(site, startTime, endTime));
        data.put(NUM_TOP_USERS_KEY, numTopUsers);
        data.put(JOB_OVERTIME_LIMIT_KEY, jobOvertimeLimit);
        data.put(ALERT_TITLE_KEY, String.format("[%s] Job Report for 12 Hours", site.toUpperCase()));
        data.put(REPORT_RANGE_KEY,
                String.format("%s ~ %s %s", startTimeStr, endTimeStr, DateTimeUtil.CURRENT_TIME_ZONE.getID()));
        data.put(EAGLE_JOB_LINK_KEY, String.format("http://%s:%d/#/site/%s/jpm/list?startTime=%s&endTime=%s",
                config.getString(SERVICE_HOST), config.getInt(SERVICE_PORT), site, startTimeStr, endTimeStr));
        watch.stop();
        LOG.info("Fetching DailyJobReport tasks {} seconds", watch.getTime() / DateTimeUtil.ONESECOND);
    } finally {
        try {
            client.close();
        } catch (IOException e) {
            LOG.info("fail to close eagle service client");
        }
    }
    return data;
}

From source file:org.apache.eagle.service.generic.GenericEntityServiceResource.java

public GenericServiceAPIResponseEntity updateDatabase(Statement<ModifyResult<String>> statement) {
    GenericServiceAPIResponseEntity<String> response = new GenericServiceAPIResponseEntity<>();
    Map<String, Object> meta = new HashMap<>();
    StopWatch stopWatch = new StopWatch();

    try {/*from  ww w  . j  a  v  a 2  s  .c  o  m*/
        stopWatch.start();
        DataStorage dataStorage = DataStorageManager.getDataStorageByEagleConfig();
        if (dataStorage == null) {
            LOG.error("Data storage is null");
            throw new IllegalDataStorageException("Data storage is null");
        }
        ModifyResult<String> result = statement.execute(dataStorage);
        if (result.isSuccess()) {
            List<String> keys = result.getIdentifiers();
            if (keys != null) {
                response.setObj(keys, String.class);
                meta.put(TOTAL_RESULTS, keys.size());
            } else {
                meta.put(TOTAL_RESULTS, 0);
            }
            meta.put(ELAPSEDMS, stopWatch.getTime());
            response.setMeta(meta);
            response.setSuccess(true);
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        response.setException(e);
    } finally {
        stopWatch.stop();
    }
    return response;
}

From source file:org.apache.eagle.service.jpm.MRJobExecutionResource.java

@GET
@Produces(MediaType.APPLICATION_JSON)/*from w  w  w  . j a  v a 2 s  .  c  o m*/
public GenericServiceAPIResponseEntity listJobs(@QueryParam("query") String query,
        @QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime,
        @QueryParam("pageSize") int pageSize, @QueryParam("startRowkey") String startRowkey,
        @QueryParam("treeAgg") boolean treeAgg, @QueryParam("timeSeries") boolean timeSeries,
        @QueryParam("intervalmin") long intervalmin, @QueryParam("top") int top,
        @QueryParam("filterIfMissing") boolean filterIfMissing, @QueryParam("parallel") int parallel,
        @QueryParam("metricName") String metricName, @QueryParam("verbose") Boolean verbose)
        throws ParseException {
    GenericServiceAPIResponseEntity response = new GenericServiceAPIResponseEntity();

    List<TaggedLogAPIEntity> jobs = new ArrayList<>();
    List<JobExecutionAPIEntity> finishedJobs = new ArrayList<>();
    Set<String> jobIds = new HashSet<>();
    final Map<String, Object> meta = new HashMap<>();
    StopWatch stopWatch = new StopWatch();

    stopWatch.start();
    String jobQuery = String.format(query, Constants.JPA_JOB_EXECUTION_SERVICE_NAME);
    GenericServiceAPIResponseEntity<JobExecutionAPIEntity> res = resource.search(jobQuery, startTime, endTime,
            pageSize, startRowkey, treeAgg, timeSeries, intervalmin, top, filterIfMissing, parallel, metricName,
            verbose);
    if (res.isSuccess() && res.getObj() != null) {
        long maxFinishedTime = DateTimeUtil.humanDateToSeconds(endTime) * DateTimeUtil.ONESECOND;
        for (JobExecutionAPIEntity o : res.getObj()) {
            if (o.getEndTime() <= maxFinishedTime) {
                finishedJobs.add(o);
                jobIds.add(o.getTags().get(MRJobTagName.JOB_ID.toString()));
            }
        }
        jobQuery = String.format(query, Constants.JPA_RUNNING_JOB_EXECUTION_SERVICE_NAME);
        GenericServiceAPIResponseEntity<org.apache.eagle.jpm.mr.runningentity.JobExecutionAPIEntity> runningRes = resource
                .search(jobQuery, startTime, endTime, pageSize, startRowkey, treeAgg, timeSeries, intervalmin,
                        top, filterIfMissing, parallel, metricName, verbose);
        if (runningRes.isSuccess() && runningRes.getObj() != null) {
            for (org.apache.eagle.jpm.mr.runningentity.JobExecutionAPIEntity o : runningRes.getObj()) {
                String key = o.getTags().get(MRJobTagName.JOB_ID.toString());
                if (!ResourceUtils.isDuplicate(jobIds, key)) {
                    jobs.add(o);
                }
            }
            jobs.addAll(finishedJobs);
        }
    }
    stopWatch.stop();
    if (res.isSuccess()) {
        response.setSuccess(true);
    } else {
        response.setSuccess(false);
        response.setException(new Exception(res.getException()));
    }
    meta.put(TOTAL_RESULTS, jobs.size());
    meta.put(ELAPSEDMS, stopWatch.getTime());
    response.setObj(jobs);
    response.setMeta(meta);
    return response;

}

From source file:org.apache.eagle.service.jpm.MRJobExecutionResource.java

@GET
@Path("search")
@Produces(MediaType.APPLICATION_JSON)//from   www  .j  av a 2 s.  co m
public GenericServiceAPIResponseEntity searchJobsById(@QueryParam("jobId") String jobId,
        @QueryParam("jobDefId") String jobDefId, @QueryParam("site") String site) {
    GenericServiceAPIResponseEntity response = new GenericServiceAPIResponseEntity();
    if ((jobId == null && jobDefId == null) || site == null) {
        response.setException(
                new IllegalArgumentException("Error: (jobId == null && jobDefId == null) || site == null"));
        response.setSuccess(false);
        return response;
    }

    List<TaggedLogAPIEntity> jobs = new ArrayList<>();
    Set<String> jobIds = new HashSet<>();
    String condition = buildCondition(jobId, jobDefId, site);

    if (condition == null) {
        response.setException(new Exception("Search condition is empty"));
        response.setSuccess(false);
        return response;
    }
    LOG.debug("search condition=" + condition);

    final Map<String, Object> meta = new HashMap<>();
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    String queryFormat = "%s[%s]{*}";
    String queryString = String.format(queryFormat, Constants.JPA_JOB_EXECUTION_SERVICE_NAME, condition);
    GenericServiceAPIResponseEntity<TaggedLogAPIEntity> res = ResourceUtils.getQueryResult(queryString, null,
            null);
    if (res.isSuccess() && res.getObj() != null) {
        for (TaggedLogAPIEntity o : res.getObj()) {
            jobs.add(o);
            jobIds.add(o.getTags().get(MRJobTagName.JOB_ID.toString()));
        }
    }
    queryString = String.format(queryFormat, Constants.JPA_RUNNING_JOB_EXECUTION_SERVICE_NAME, condition);
    res = ResourceUtils.getQueryResult(queryString, null, null);
    if (res.isSuccess() && res.getObj() != null) {
        for (TaggedLogAPIEntity o : res.getObj()) {
            String key = o.getTags().get(MRJobTagName.JOB_ID.toString());
            if (!ResourceUtils.isDuplicate(jobIds, key)) {
                jobs.add(o);
            }
        }
    }
    if (jobs.size() > 0) {
        Collections.sort(jobs, new Comparator<TaggedLogAPIEntity>() {
            @Override
            public int compare(TaggedLogAPIEntity o1, TaggedLogAPIEntity o2) {
                return o1.getTimestamp() > o2.getTimestamp() ? 1
                        : (o1.getTimestamp() == o2.getTimestamp() ? 0 : -1);
            }
        });
    }
    stopWatch.stop();
    if (res.isSuccess()) {
        response.setSuccess(true);
    } else {
        response.setSuccess(false);
        response.setException(new Exception(res.getException()));
    }
    meta.put(TOTAL_RESULTS, jobs.size());
    meta.put(ELAPSEDMS, stopWatch.getTime());
    response.setObj(jobs);
    response.setMeta(meta);
    return response;
}

From source file:org.apache.eagle.storage.jdbc.entity.impl.JdbcEntityReaderImpl.java

@Override
@SuppressWarnings("unchecked")
public <E extends Object> List<E> query(CompiledQuery query) throws Exception {
    QueryCriteriaBuilder criteriaBuilder = new QueryCriteriaBuilder(query, this.jdbcEntityDefinition);
    Criteria criteria = criteriaBuilder.build();
    String displaySql = SqlBuilder.buildQuery(criteria).getDisplayString();

    if (LOG.isDebugEnabled())
        LOG.debug("Querying: " + displaySql);

    RecordMapper<E> recordMapper;//w ww  .  java 2s .com
    if (query.isHasAgg() && !query.isTimeSeries()) {
        recordMapper = (RecordMapper<E>) new AggreagteRecordMapper(query, jdbcEntityDefinition);
    } else {
        recordMapper = new EntityRecordMapper(jdbcEntityDefinition);
    }
    final StopWatch stopWatch = new StopWatch();
    List<E> result;
    try {
        stopWatch.start();
        TorqueStatementPeerImpl peer = ConnectionManagerFactory.getInstance().getStatementExecutor();
        result = peer.delegate().doSelect(criteria, recordMapper);
        LOG.info(String.format("Read %s records in %s ms (sql: %s)", result.size(), stopWatch.getTime(),
                displaySql));
        if (result.size() > 0 && query.isTimeSeries()) {
            result = Lists.newArrayList((E) timeseriesAggregate(result, query));
        }
    } catch (Exception ex) {
        LOG.error("Failed to query by: " + displaySql + ", due to: " + ex.getMessage(), ex);
        throw new IOException("Failed to query by: " + displaySql, ex);
    } finally {
        stopWatch.stop();
    }
    return result;
}

From source file:org.apache.eagle.storage.jdbc.entity.impl.JdbcEntityReaderImpl.java

@Override
public <E> List<E> query(List<String> ids) throws Exception {
    PrimaryKeyCriteriaBuilder criteriaBuilder = new PrimaryKeyCriteriaBuilder(ids,
            this.jdbcEntityDefinition.getJdbcTableName());
    Criteria criteria = criteriaBuilder.build();
    String displaySql = SqlBuilder.buildQuery(criteria).getDisplayString();
    if (LOG.isDebugEnabled())
        LOG.debug("Querying: " + displaySql);
    EntityRecordMapper recordMapper = new EntityRecordMapper(jdbcEntityDefinition);
    final StopWatch stopWatch = new StopWatch();
    List<E> result;/*from w ww  .  j av a2  s. c  o  m*/
    try {
        stopWatch.start();
        TorqueStatementPeerImpl peer = ConnectionManagerFactory.getInstance().getStatementExecutor();
        criteria.addSelectColumn(new ColumnImpl(jdbcEntityDefinition.getJdbcTableName(), "*"));
        result = peer.delegate().doSelect(criteria, recordMapper);
        LOG.info(String.format("Read %s records in %s ms (sql: %s)", result.size(), stopWatch.getTime(),
                displaySql));
    } catch (Exception ex) {
        LOG.error("Failed to query by: " + displaySql + ", due to: " + ex.getMessage(), ex);
        throw new IOException("Failed to query by: " + displaySql, ex);
    } finally {
        stopWatch.stop();
    }
    return result;
}

From source file:org.apache.eagle.storage.jdbc.entity.impl.JdbcEntityUpdaterImpl.java

@Override
public int update(List<E> entities) throws Exception {
    ConnectionManager cm = ConnectionManagerFactory.getInstance();
    TorqueStatementPeerImpl<E> peer = cm.getStatementExecutor(this.jdbcEntityDefinition.getJdbcTableName());
    Connection connection = cm.getConnection();
    connection.setAutoCommit(false);//from   w ww .ja v a  2  s  .  c o m

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    int num = 0;
    try {
        for (E entity : entities) {
            String primaryKey = entity.getEncodedRowkey();
            if (primaryKey == null) {
                primaryKey = ConnectionManagerFactory.getInstance().getStatementExecutor()
                        .getPrimaryKeyBuilder().build(entity);
                entity.setEncodedRowkey(primaryKey);
            }
            PrimaryKeyCriteriaBuilder pkBuilder = new PrimaryKeyCriteriaBuilder(
                    Collections.singletonList(primaryKey), this.jdbcEntityDefinition.getJdbcTableName());
            Criteria selectCriteria = pkBuilder.build();
            if (LOG.isDebugEnabled())
                LOG.debug("Updating by query: " + SqlBuilder.buildQuery(selectCriteria).getDisplayString());
            ColumnValues columnValues = JdbcEntitySerDeserHelper.buildColumnValues(entity,
                    this.jdbcEntityDefinition);
            num += peer.delegate().doUpdate(selectCriteria, columnValues, connection);
        }
        if (LOG.isDebugEnabled())
            LOG.debug("Committing updates");
        connection.commit();
    } catch (Exception ex) {
        LOG.error("Failed to update, rolling back", ex);
        connection.rollback();
        throw ex;
    } finally {
        stopWatch.stop();
        if (LOG.isDebugEnabled())
            LOG.debug("Closing connection");
        connection.close();
    }
    LOG.info(String.format("Updated %s records in %s ms", num, stopWatch.getTime()));
    return num;
}

From source file:org.apache.eagle.storage.jdbc.entity.impl.JdbcEntityWriterImpl.java

@Override
public List<String> write(List<E> entities) throws Exception {
    List<String> keys = new ArrayList<String>();
    if (LOG.isDebugEnabled())
        LOG.debug("Writing " + entities.size() + " entities");
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();/*from  w w  w.ja  v  a 2  s  .c o  m*/
    Connection connection = ConnectionManagerFactory.getInstance().getConnection();
    // set auto commit false and commit by hands for 3x~5x better performance
    connection.setAutoCommit(false);

    try {
        TorqueStatementPeerImpl<E> peer = connectionManager
                .getStatementExecutor(this.jdbcEntityDefinition.getJdbcTableName());
        for (E entity : entities) {
            entity.setEncodedRowkey(peer.getPrimaryKeyBuilder().build(entity));
            ColumnValues columnValues = JdbcEntitySerDeserHelper.buildColumnValues(entity,
                    this.jdbcEntityDefinition);

            ObjectKey key = null;
            try {
                // TODO: implement batch insert for better performance
                key = peer.delegate().doInsert(columnValues, connection);

                if (key != null) {
                    keys.add((String) key.getValue());
                } else {
                    keys.add(entity.getEncodedRowkey());
                }
            } catch (ClassCastException ex) {
                assert key != null;
                throw new RuntimeException(
                        "Key is not in type of String (VARCHAR) , but JdbcType (java.sql.Types): "
                                + key.getJdbcType() + ", value: " + key.getValue(),
                        ex);
            } catch (ConstraintViolationException e) {
                //this message will be different in each DB type ...using duplicate keyword to catch for broader set of DBs. moreover we are already inside ConstraintViolationException exception, do we even need this check?
                if (e.getMessage().toLowerCase().contains("duplicate")) {
                    String primaryKey = entity.getEncodedRowkey();
                    if (primaryKey == null) {
                        primaryKey = ConnectionManagerFactory.getInstance().getStatementExecutor()
                                .getPrimaryKeyBuilder().build(entity);
                        entity.setEncodedRowkey(primaryKey);
                    }
                    PrimaryKeyCriteriaBuilder pkBuilder = new PrimaryKeyCriteriaBuilder(
                            Collections.singletonList(primaryKey),
                            this.jdbcEntityDefinition.getJdbcTableName());
                    Criteria selectCriteria = pkBuilder.build();
                    if (LOG.isDebugEnabled())
                        LOG.debug("Updating by query: "
                                + SqlBuilder.buildQuery(selectCriteria).getDisplayString());
                    peer.delegate().doUpdate(selectCriteria, columnValues, connection);
                    keys.add(primaryKey);
                }
            }
        }

        // Why not commit in finally: give up all if any single entity throws exception to make sure consistency guarantee
        if (LOG.isDebugEnabled()) {
            LOG.debug("Committing writing");
        }
        connection.commit();
    } catch (Exception ex) {
        LOG.error("Failed to write records, rolling back", ex);
        connection.rollback();
        throw ex;
    } finally {
        stopWatch.stop();
        if (LOG.isDebugEnabled())
            LOG.debug("Closing connection");
        connection.close();
    }

    LOG.info(String.format("Wrote %s records in %s ms (table: %s)", keys.size(), stopWatch.getTime(),
            this.jdbcEntityDefinition.getJdbcTableName()));
    return keys;
}