@Test public void getExclusiveDurationMustReturnTheExclusiveDurationOfTheTimerDataWhenItIsASqlData() { InvocationSequenceData invocationData = new InvocationSequenceData(); SqlStatementData timerData = new SqlStatementData(new Timestamp(System.currentTimeMillis()), 10L, 20L, 30L); timerData.calculateExclusiveMin(300d); timerData.setExclusiveDuration(300d); timerData.setCount(1); invocationData.setSqlStatementData(timerData); double exclusiveDuration = DiagnosisHelper.getExclusiveDuration(invocationData); assertThat("Exclusive duration must be 300.", exclusiveDuration, is(timerData.getExclusiveDuration())); } }
/** * {@inheritDoc} */ @Override public IAggregatedData<SqlStatementData> getClone(SqlStatementData sqlStatementData) { AggregatedSqlStatementData clone = new AggregatedSqlStatementData(); clone.setPlatformIdent(sqlStatementData.getPlatformIdent()); clone.setSensorTypeIdent(sqlStatementData.getSensorTypeIdent()); clone.setPreparedStatement(sqlStatementData.isPreparedStatement()); clone.setSql(sqlStatementData.getSql()); clone.setDatabaseProductName(sqlStatementData.getDatabaseProductName()); clone.setDatabaseProductVersion(sqlStatementData.getDatabaseProductVersion()); clone.setDatabaseUrl(sqlStatementData.getDatabaseUrl()); if (includeParameters && (null != sqlStatementData.getParameterValues())) { clone.setParameterValues(new ArrayList<>(sqlStatementData.getParameterValues())); } return clone; }
switch (enumId) { case STATEMENT: String sql = data.getSql().replaceAll("[\r\n]+", " "); return new StyledString(sql); case INVOCATION_AFFILLIATION: int percentage = (int) (data.getInvocationAffiliationPercentage() * 100); int invocations = 0; if (null != data.getInvocationParentsIdSet()) { invocations = data.getInvocationParentsIdSet().size(); return new StyledString(Long.toString(data.getCount())); case AVERAGE: return new StyledString(NumberFormatter.formatDouble(data.getAverage(), timeDecimalPlaces)); case MIN: return new StyledString(NumberFormatter.formatDouble(data.getMin(), timeDecimalPlaces)); case MAX: return new StyledString(NumberFormatter.formatDouble(data.getMax(), timeDecimalPlaces)); case DURATION: return new StyledString(NumberFormatter.formatDouble(data.getDuration(), timeDecimalPlaces)); case PREPARED: if (data.isPreparedStatement()) { return new StyledString("true"); } else {
/** * {@inheritDoc} */ @Override protected void processData(DefaultData defaultData, EntityManager entityManager) { SqlStatementData sqlStatementData = (SqlStatementData) defaultData; sqlStatementData.setExclusiveCount(1L); sqlStatementData.setExclusiveDuration(sqlStatementData.getDuration()); sqlStatementData.calculateExclusiveMax(sqlStatementData.getDuration()); sqlStatementData.calculateExclusiveMin(sqlStatementData.getDuration()); }
/** * {@inheritDoc} */ @Override public Object getAggregationKey(SqlStatementData sqlStatementData) { final int prime = 31; int result = 0; result = (prime * result) + (sqlStatementData.isPreparedStatement() ? 1231 : 1237); result = (prime * result) + ((sqlStatementData.getSql() == null) ? 0 : sqlStatementData.getSql().hashCode()); result = (prime * result) + ((sqlStatementData.getDatabaseUrl() == null) ? 0 : sqlStatementData.getDatabaseUrl().hashCode()); if (includeParameters) { result = (prime * result) + ((sqlStatementData.getParameterValues() == null) ? 0 : sqlStatementData.getParameterValues().hashCode()); } return result; }
/** * Returns template for {@link SqlStatementData}. * * @param sqlStatementData * Source object. * @param id * Should id be inserted into template. * @param sql * Should SQL query be inserted into template. * @param parameters * Should parameters be inserted into template. * @return Template object. */ protected SqlStatementData getTemplate(SqlStatementData sqlStatementData, boolean id, boolean sql, boolean parameters) { SqlStatementData template = OccurrenceFinderFactory.getEmptyTemplate(sqlStatementData); if (id && (0 != sqlStatementData.getId())) { template.setId(sqlStatementData.getId()); } if (sql && (null != sqlStatementData.getSql())) { template.setSql(sqlStatementData.getSql()); } if (parameters && (null != sqlStatementData.getParameterValues())) { template.setParameterValues(sqlStatementData.getParameterValues()); } return template; }
@Test private void musteReturnAnObjectWithAPairLongStringIfTheTimerDataHasSqlData() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 108L); SqlStatementData sqlStatementData = new SqlStatementData(new Timestamp(10), 10, 10, 108L); sqlStatementData.setCount(1); sqlStatementData.setSql("blahblahblah"); invocationSequenceData.setSqlStatementData(sqlStatementData); TimerData timerData = new TimerData(); invocationSequenceData.setTimerData(timerData); Pair<Long, String> aggregationKey = (Pair<Long, String>) DiagnosisDataAggregator.getInstance().getAggregationKey(invocationSequenceData); assertThat("The string of the pair must be the sql data", aggregationKey.getSecond(), is(sqlStatementData.getSql())); }
assertThat(sqlData.getPlatformIdent(), is(platformId)); assertThat(sqlData.getMethodIdent(), is(methodId)); assertThat(sqlData.getSensorTypeIdent(), is(sensorTypeId)); assertThat(sqlData.getTimeStamp(), is(not(nullValue()))); assertThat(sqlData.getCount(), is(1L)); assertThat(sqlData.getDuration(), is(secondTimerValue - firstTimerValue)); assertThat(sqlData.getMin(), is(secondTimerValue - firstTimerValue)); assertThat(sqlData.getMax(), is(secondTimerValue - firstTimerValue)); assertThat(sqlData.getSql(), is((String) parameters[0])); assertThat(sqlData.isCharting(), is(false));
/** * * @return One {@link SqlStatementData} with random values. */ private static SqlStatementData getSqlStatementInstance() { Random random = new Random(); SqlStatementData sqlData = new SqlStatementData(new Timestamp(random.nextLong()), random.nextLong(), random.nextLong(), random.nextLong(), "New Sql String"); sqlData.setCount(random.nextLong()); sqlData.setCpuDuration(random.nextDouble()); sqlData.calculateCpuMax(random.nextDouble()); sqlData.calculateCpuMin(random.nextDouble()); sqlData.setDuration(random.nextDouble()); sqlData.setExclusiveCount(random.nextLong()); sqlData.setExclusiveDuration(random.nextDouble()); sqlData.calculateExclusiveMax(random.nextDouble()); sqlData.calculateExclusiveMin(random.nextDouble()); sqlData.setId(random.nextLong()); sqlData.addInvocationParentId(random.nextLong()); sqlData.setPreparedStatement(true); return sqlData; }
/** * {@inheritDoc} */ @Override public void secondAfterBody(ICoreService coreService, long methodId, long sensorTypeId, Object object, Object[] parameters, Object result, boolean exception, RegisteredSensorConfig rsc) { // NOCHK:8-params double endTime = timeStack.pop().doubleValue(); double startTime = timeStack.pop().doubleValue(); if (threadLast.get().booleanValue()) { threadLast.set(Boolean.FALSE); double duration = endTime - startTime; String sql = parameters[0].toString(); Timestamp timestamp = new Timestamp(System.currentTimeMillis() - Math.round(duration)); long platformId = platformManager.getPlatformId(); SqlStatementData sqlData = new SqlStatementData(timestamp, platformId, sensorTypeId, methodId); sqlData.setPreparedStatement(false); sqlData.setSql(strConstraint.crop(sql)); sqlData.setDuration(duration); sqlData.calculateMin(duration); sqlData.calculateMax(duration); sqlData.setCount(1L); // populate the connection meta data. connectionMetaDataStorage.populate(sqlData, statementReflectionCache.getConnection(object.getClass(), object)); coreService.addDefaultData(sqlData); } }
@Test private void musteReturnAnInstanceWithDataBaseSourceTypeDataIfHasSqlData() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 108L); SqlStatementData sqlStatementData = new SqlStatementData(new Timestamp(10), 10, 10, 108L); sqlStatementData.setCount(1); sqlStatementData.setSql("blahblahblah"); invocationSequenceData.setSqlStatementData(sqlStatementData); AggregatedDiagnosisData aggregatedDiagnosisData = DiagnosisDataAggregator.getInstance().getAggregatedDiagnosisData(invocationSequenceData); assertThat("The object must have DATABASE as source type", aggregatedDiagnosisData.getSourceType(), is(SourceType.DATABASE)); }
case DATABASE_URL: if (rawMode) { if (StringUtils.isNotEmpty(data.getDatabaseUrl())) { return new StyledString(data.getDatabaseUrl()); } else { return new StyledString("Unknown"); return new StyledString(NumberFormatter.formatTimeWithMillis(data.getTimeStamp())); } else { return emptyStyledString; String sql = TextFormatter.clearLineBreaks(data.getSqlWithParameterValues()); return new StyledString(sql); } else { String sql = TextFormatter.clearLineBreaks(data.getSql()); return new StyledString(sql); return new StyledString(Long.toString(data.getCount())); case AVERAGE: return new StyledString(NumberFormatter.formatDouble(data.getAverage())); case MIN: return new StyledString(NumberFormatter.formatDouble(data.getMin())); case MAX: return new StyledString(NumberFormatter.formatDouble(data.getMax())); case DURATION: return new StyledString(NumberFormatter.formatDouble(data.getDuration())); case PREPARED: if (rawMode) { return new StyledString(Boolean.toString(data.isPreparedStatement()));
/** * @return Returns {@link AbstractWriteDataProcessor} for caching the {@link SqlStatementData} * view. */ @Bean @Lazy @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) public AbstractWriteDataProcessor getSqlDataCachingDataProcessor() { IIndexQuery query = sqlStatementDataQueryFactory.getAggregatedSqlStatementsQuery(new SqlStatementData(), null, null); IAggregator<SqlStatementData> aggregator = Aggregators.SQL_STATEMENT_DATA_AGGREGATOR; return new QueryCachingDataProcessor<>(query, aggregator); }
switch (enumId) { case PARAMETERS: return new StyledString(TextFormatter.getSqlParametersText(data.getParameterValues())); case INVOCATION_AFFILLIATION: int percentage = (int) (data.getInvocationAffiliationPercentage() * 100); int invocations = 0; if (null != data.getInvocationParentsIdSet()) { invocations = data.getInvocationParentsIdSet().size(); return new StyledString(Long.toString(data.getCount())); case AVERAGE: return new StyledString(NumberFormatter.formatDouble(data.getAverage(), timeDecimalPlaces)); case MIN: return new StyledString(NumberFormatter.formatDouble(data.getMin(), timeDecimalPlaces)); case MAX: return new StyledString(NumberFormatter.formatDouble(data.getMax(), timeDecimalPlaces)); case DURATION: return new StyledString(NumberFormatter.formatDouble(data.getDuration(), timeDecimalPlaces)); default: return new StyledString("error");
switch (this) { case IS_PREPARED_STATEMENT: return Boolean.valueOf(o1.isPreparedStatement()).compareTo(Boolean.valueOf(o2.isPreparedStatement())); case SQL: return ObjectUtils.compare(o1.getSql(), o2.getSql()); case DATABASE_NAME: return ObjectUtils.compare(o1.getDatabaseProductName(), o2.getDatabaseProductName()); case DATABASE_VERSION: return ObjectUtils.compare(o1.getDatabaseProductVersion(), o2.getDatabaseProductVersion()); case DATABASE_URL: return ObjectUtils.compare(o1.getDatabaseUrl(), o2.getDatabaseUrl()); case PARAMETERS: List<String> parameterList1 = o1.getParameterValues(); List<String> parameterList2 = o2.getParameterValues(); return ObjectUtils.compare(parameterList1, parameterList2); case SQL_AND_PARAMETERS:
SqlStatementData sql = new SqlStatementData(); sql.setCount(1L); sql.setDuration(1L); child.setSqlStatementData(sql); child.setParentSequence(parent); assertThat(sql.isOnlyFoundInInvocations(), is(true)); assertThat(sql.getInvocationParentsIdSet(), hasSize(1)); assertThat(sql.getInvocationParentsIdSet(), hasItem(10L));
/** * {@inheritDoc} */ @Override public void setInputDefinition(InputDefinition inputDefinition) { super.setInputDefinition(inputDefinition); template = new SqlStatementData(); template.setPlatformIdent(inputDefinition.getIdDefinition().getPlatformId()); template.setId(-1); if (inputDefinition.hasInputDefinitionExtra(InputDefinitionExtrasMarkerFactory.SQL_STATEMENT_EXTRAS_MARKER)) { SqlStatementInputDefinitionExtra inputDefinitionExtra = inputDefinition.getInputDefinitionExtra(InputDefinitionExtrasMarkerFactory.SQL_STATEMENT_EXTRAS_MARKER); template.setSql(inputDefinitionExtra.getSql()); } dataAccessService = inputDefinition.getRepositoryDefinition().getSqlDataAccessService(); cachedDataService = inputDefinition.getRepositoryDefinition().getCachedDataService(); }
/** * Tests that the {@link SqlStatementData} is searched correctly. */ @Test public void sqlStatementDataSearch() { SqlStatementData sqlData = new SqlStatementData(); sqlData.setMethodIdent(1L); sqlData.setSql("Select blah from table where condition"); SearchCriteria searchCriteria = new SearchCriteria("Blah"); SearchCriteria wrong = new SearchCriteria("halB"); searchCriteria.setCaseSensitive(false); assertThat(SearchFactory.isSearchCompatible(sqlData, searchCriteria, repositoryDefinition), is(equalTo(true))); searchCriteria.setCaseSensitive(true); assertThat(SearchFactory.isSearchCompatible(sqlData, searchCriteria, repositoryDefinition), is(equalTo(false))); assertThat(SearchFactory.isSearchCompatible(sqlData, wrong, repositoryDefinition), is(equalTo(false))); List<String> parameters = new ArrayList<>(); parameters.add("blah"); sqlData.setSql("Select somthing from table where condition=?"); sqlData.setParameterValues(parameters); searchCriteria.setCaseSensitive(false); assertThat(SearchFactory.isSearchCompatible(sqlData, searchCriteria, repositoryDefinition), is(equalTo(true))); searchCriteria.setCaseSensitive(true); assertThat(SearchFactory.isSearchCompatible(sqlData, searchCriteria, repositoryDefinition), is(equalTo(false))); assertThat(SearchFactory.isSearchCompatible(sqlData, wrong, repositoryDefinition), is(equalTo(false))); }
query.setPlatformIdent(sqlStatementData.getPlatformIdent()); ArrayList<Class<?>> searchedClasses = new ArrayList<>(); searchedClasses.add(SqlStatementData.class); searchedClasses.add(AggregatedSqlStatementData.class); query.setObjectClasses(searchedClasses); if (null != sqlStatementData.getSql()) { query.addIndexingRestriction(IndexQueryRestrictionFactory.equal("sql", sqlStatementData.getSql())); if (null != sqlStatementData.getDatabaseProductName()) { query.addIndexingRestriction(IndexQueryRestrictionFactory.equal("databaseProductName", sqlStatementData.getDatabaseProductName())); if (null != sqlStatementData.getDatabaseProductVersion()) { query.addIndexingRestriction(IndexQueryRestrictionFactory.equal("databaseProductVersion", sqlStatementData.getDatabaseProductVersion())); if (null != sqlStatementData.getDatabaseUrl()) { query.addIndexingRestriction(IndexQueryRestrictionFactory.equal("databaseUrl", sqlStatementData.getDatabaseUrl()));
/** * Tests the {@link SqlExclusiveTimeCmrProcessor}. */ @Test public void sqlExclusiveTimeProcessor() { SqlExclusiveTimeCmrProcessor processor = new SqlExclusiveTimeCmrProcessor(); // don't fail on null processor.process((DefaultData) null, entityManager); // only sqls assertThat(processor.canBeProcessed(new TimerData()), is(false)); // make sure exclusive data is set SqlStatementData sqlStatementData = new SqlStatementData(); sqlStatementData.setDuration(5d); processor.process(sqlStatementData, entityManager); assertThat(sqlStatementData.getExclusiveCount(), is(1l)); assertThat(sqlStatementData.getExclusiveDuration(), is(5d)); assertThat(sqlStatementData.getExclusiveMin(), is(5d)); assertThat(sqlStatementData.getExclusiveMax(), is(5d)); verifyZeroInteractions(entityManager); }