/** * {@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); } }
sqlData.calculateMin(duration); sqlData.calculateMax(duration); sqlData.setCount(1L); sqlData.setParameterValues(params);
@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)); }
@Test public void sqlStatementDataMustReturnAEmptyListOfAggregatedDiagnosisDataWhenTheDurationIsZero() { long platformIdent = 108; long sensorTypeIdent = 1; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); SqlStatementData firstSqlStatementData = new SqlStatementData(); firstSqlStatementData.calculateExclusiveMin(200d); firstSqlStatementData.setExclusiveDuration(200d); firstSqlStatementData.setCount(1); firstMethod.setSqlStatementData(firstSqlStatementData); nestedSequences.add(firstMethod); InvocationSequenceData secondMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(secondMethod); when(globalContext.getDuration()).thenReturn(new Double(0)); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return an array of AggregatedDiagnosisData not empty", timeWastingOperationsResults, hasSize(0)); }
@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())); } }
@Test public void sqlStatementDataMustReturnANotEmptyListOfAggregatedDiagnosisDataWhenTheDurationIsTooLong() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); SqlStatementData firstSqlStatementData = new SqlStatementData(); firstSqlStatementData.calculateExclusiveMin(200d); firstSqlStatementData.setExclusiveDuration(200d); firstSqlStatementData.setCount(1); firstMethod.setSqlStatementData(firstSqlStatementData); InvocationSequenceData secondMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(firstMethod); nestedSequences.add(secondMethod); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return a group of AggregatedDiagnosisData not empty", timeWastingOperationsResults, not(hasSize(0))); }
@Test private void mustReturnAnInstanceWithDataBaseSourceTypeDataIfHasSqlDataAndTimerData() { 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(new Timestamp(10), 10L, 20, 108L); invocationSequenceData.setTimerData(timerData); AggregatedDiagnosisData aggregatedDiagnosisData = DiagnosisDataAggregator.getInstance().getAggregatedDiagnosisData(invocationSequenceData); assertThat("The object must have DATABASE as source type", aggregatedDiagnosisData.getSourceType(), is(SourceType.DATABASE)); } }
firstSeqTimerData.calculateExclusiveMin(1008d); firstSeqTimerData.setExclusiveDuration(1008d); firstSeqTimerData.setCount(1); firstChildSequence.setTimerData(firstSeqTimerData); InvocationSequenceData secondChildSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); thirdTimerData.calculateExclusiveMin(2000d); thirdTimerData.setExclusiveDuration(2000d); thirdTimerData.setCount(1); thirdChildSequence.setTimerData(thirdTimerData); nestedSequences.add(firstChildSequence);
@Test public void sqlStatementDataMustReturnANotNullListOfAggregatedDiagnosisData() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); SqlStatementData firstSqlStatementData = new SqlStatementData(); firstSqlStatementData.calculateExclusiveMin(200d); firstSqlStatementData.setExclusiveDuration(200d); firstSqlStatementData.setCount(1); firstMethod.setSqlStatementData(firstSqlStatementData); InvocationSequenceData secondMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(firstMethod); nestedSequences.add(secondMethod); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("The returned list of AggregatedDiagnosisData must not be null", timeWastingOperationsResults, notNullValue()); }
@Test public void sqlStatementDataMustReturnAListOfAggregatedDiagnosisDataWithOneElement() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); SqlStatementData firstSqlStatementData = new SqlStatementData(); firstSqlStatementData.calculateExclusiveMin(200d); firstSqlStatementData.setExclusiveDuration(200d); firstSqlStatementData.setCount(1); firstMethod.setSqlStatementData(firstSqlStatementData); InvocationSequenceData secondMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(firstMethod); nestedSequences.add(secondMethod); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return a list with one AggregatedDiagnosisData", timeWastingOperationsResults, hasSize(1)); }
/** * * @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; }
@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())); }
@Test public void sqlStatementDataMustReturnAValidListOfAggregatedDiagnosisData() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); SqlStatementData firstSqlStatementData = new SqlStatementData(); firstSqlStatementData.calculateExclusiveMin(200d); firstSqlStatementData.setExclusiveDuration(200d); firstSqlStatementData.setCount(1); firstMethod.setSqlStatementData(firstSqlStatementData); InvocationSequenceData secondMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(firstMethod); nestedSequences.add(secondMethod); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return a list of AggregatedDiagnosisData not empty", timeWastingOperationsResults, not(hasSize(0))); for (AggregatedDiagnosisData AggregatedDiagnosisInvocationData : timeWastingOperationsResults) { isAValidAggregatedDiagnosisData(AggregatedDiagnosisInvocationData); } }
@Test private void ifTheAggregatedObjectHasSQLDataAndIsDefinedInTheMapItMustBeAggregatedToTheDiagnosisAggregator() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 2L); diagnosisDataAggregationPerformer = new DiagnosisDataAggregationPerformer(); SqlStatementData sqlStatementData = new SqlStatementData(new Timestamp(10), 10, 10, 108L); sqlStatementData.setCount(1); sqlStatementData.setSql("blahblahblah"); invocationSequenceData.setSqlStatementData(sqlStatementData); HttpTimerData timerData = new HttpTimerData(new Timestamp(10), 10, 10, 108L); invocationSequenceData.setTimerData(timerData); Object key = new Pair<Long, String>(invocationSequenceData.getMethodIdent(), invocationSequenceData.getSqlStatementData().getSql()); diagnosisDataAggregationPerformer.diagnosisDataAggregationMap.put(key, alreadyAggregatedObject); diagnosisDataAggregationPerformer.aggregateInvocationSequenceData(invocationSequenceData); verify(alreadyAggregatedObject, times(1)).aggregate(invocationSequenceData); }
@Test public void sqlStatementDataMustReturnAListOfAggregatedDiagnosisDataWithTwoElements() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); SqlStatementData firstSqlStatementData = new SqlStatementData(); firstSqlStatementData.calculateExclusiveMin(200d); firstSqlStatementData.setExclusiveDuration(200d); firstSqlStatementData.setCount(1); firstMethod.setSqlStatementData(firstSqlStatementData); InvocationSequenceData secondMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); InvocationSequenceData thirdMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 3L); SqlStatementData thirdSqlStatementData = new SqlStatementData(); thirdSqlStatementData.calculateExclusiveMin(200d); thirdSqlStatementData.setExclusiveDuration(200d); thirdSqlStatementData.setCount(1); secondMethod.setSqlStatementData(thirdSqlStatementData); nestedSequences.add(firstMethod); nestedSequences.add(secondMethod); nestedSequences.add(thirdMethod); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return a list with two AggregatedDiagnosisData", timeWastingOperationsResults, hasSize(2)); }
@Test public void sqlStatementDataMustReturnTheExpectedAggregatedDiagnosisData() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); SqlStatementData firstSqlStatementData = new SqlStatementData(); firstSqlStatementData.calculateExclusiveMin(200d); firstSqlStatementData.setExclusiveDuration(200d); firstSqlStatementData.setCount(1); firstMethod.setSqlStatementData(firstSqlStatementData); InvocationSequenceData secondMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); InvocationSequenceData thirdMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 3L); SqlStatementData thirdSqlStatementData = new SqlStatementData(); thirdSqlStatementData.calculateExclusiveMin(300d); thirdSqlStatementData.setExclusiveDuration(300d); thirdSqlStatementData.setCount(1); thirdMethod.setSqlStatementData(thirdSqlStatementData); nestedSequences.add(firstMethod); nestedSequences.add(secondMethod); nestedSequences.add(thirdMethod); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Identifier is not the expected one, the first result must have 3 as method identifier", timeWastingOperationsResults.get(0).getMethodIdent(), is(3L)); assertThat("Identifier is not the expected one, the first result must have 1 as method identifier", timeWastingOperationsResults.get(1).getMethodIdent(), is(1L)); }
long methodIdentDiff = random.nextLong(); InvocationSequenceData childSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, methodIdentDiff); timerDataSql.setCount(1); timerDataSql.setSql("somethingsomething"); childSequence.setTimerData(timerDataSql);
@Test private void ifTheAggregatedObjectIsNotDefineInTheMapItMustBeAggregated() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 2L); diagnosisDataAggregationPerformer = new DiagnosisDataAggregationPerformer(); SqlStatementData sqlStatementData = new SqlStatementData(new Timestamp(10), 10, 10, 108L); sqlStatementData.setCount(1); sqlStatementData.setSql("blahblahblah"); invocationSequenceData.setSqlStatementData(sqlStatementData); TimerData timerData = new TimerData(new Timestamp(10), 10, 10, 108L); invocationSequenceData.setTimerData(timerData); Object key = new Pair<Long, String>(invocationSequenceData.getMethodIdent(), invocationSequenceData.getSqlStatementData().getSql()); AggregatedDiagnosisData alreadyAggregatedObject = new AggregatedDiagnosisData(SourceType.TIMERDATA, invocationSequenceData, key); diagnosisDataAggregationPerformer.diagnosisDataAggregationMap.put(key, alreadyAggregatedObject); InvocationSequenceData secondInvocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 2L); secondInvocationSequenceData.setTimerData(timerData); List<InvocationSequenceData> invocationSequenceDataList = new ArrayList<>(); invocationSequenceDataList.add(invocationSequenceData); invocationSequenceDataList.add(secondInvocationSequenceData); diagnosisDataAggregationPerformer.aggregateInvocationSequenceDataList(invocationSequenceDataList); List<AggregatedDiagnosisData> resultList = diagnosisDataAggregationPerformer.getAggregationResultList(); assertThat("The list must have 2 aggregated objects", resultList.size(), is(2)); assertThat("The first aggregated object must have the same method ident that the invocationSequenceData", resultList.get(0).getMethodIdent(), equalTo(invocationSequenceData.getMethodIdent())); assertThat("The second aggregated object must have the same method ident that the secondInvocationSequenceData", resultList.get(1).getMethodIdent(), equalTo(secondInvocationSequenceData.getMethodIdent())); } }
long methodIdentDiff = random.nextLong(); InvocationSequenceData detectedProblemContext = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, methodIdentDiff); timerDataSql.setCount(1); timerDataSql.setSql("somethingsomething"); detectedProblemContext.setSqlStatementData(timerDataSql);
child.setId(20L); SqlStatementData sql = new SqlStatementData(); sql.setCount(1L); sql.setDuration(1L); child.setSqlStatementData(sql);