Fixed bug on calculation average duration

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@117147 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-07-10 09:58:29 +00:00
parent f7a1e4eeec
commit 196351946f
2 changed files with 20 additions and 7 deletions

View File

@ -23,13 +23,21 @@ public class ServiceUsageRecordAggregationStrategy extends AggregationStrategy<S
this.aggregationField.add(ServiceUsageRecord.SERVICE_CLASS); this.aggregationField.add(ServiceUsageRecord.SERVICE_CLASS);
this.aggregationField.add(ServiceUsageRecord.SERVICE_NAME); this.aggregationField.add(ServiceUsageRecord.SERVICE_NAME);
} }
protected long durationWeightedAverage(ServiceUsageRecord record){
long tDuration = t.getDuration() * t.getOperationCount();
long recordDuration = record.getDuration() * record.getOperationCount();
long totalOperationCount = t.getOperationCount() + record.getOperationCount();
return (tDuration + recordDuration) / totalOperationCount;
}
protected ServiceUsageRecord reallyAggregate(ServiceUsageRecord record) protected ServiceUsageRecord reallyAggregate(ServiceUsageRecord record)
throws NotAggregatableRecordsExceptions { throws NotAggregatableRecordsExceptions {
try { try {
t.setDuration(durationWeightedAverage(record));
t.setOperationCount(t.getOperationCount() + record.getOperationCount()); t.setOperationCount(t.getOperationCount() + record.getOperationCount());
t.setDuration((t.getDuration() + record.getDuration()) / 2);
long max = record.getMaxInvocationTime(); long max = record.getMaxInvocationTime();
if(max > t.getMaxInvocationTime()){ if(max > t.getMaxInvocationTime()){

View File

@ -106,6 +106,10 @@ public class ServiceUsageRecordAggregationStrategyTest {
Assert.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME)); Assert.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME));
} }
protected long durationWeightedAverage(int numberA, long durationA, int numberB, long durationB){
return ((numberA * durationA) + (numberB * durationB)) / (numberA + numberB);
}
@Test @Test
public void aggregationStressTest() throws InvalidValueException, NotAggregatableRecordsExceptions { public void aggregationStressTest() throws InvalidValueException, NotAggregatableRecordsExceptions {
@ -131,25 +135,26 @@ public class ServiceUsageRecordAggregationStrategyTest {
long minInvocationTime = aggregated.getMinInvocationTime(); long minInvocationTime = aggregated.getMinInvocationTime();
long maxInvocationTime = aggregated.getMaxInvocationTime(); long maxInvocationTime = aggregated.getMaxInvocationTime();
long oldDuration = aggregated.getDuration(); long oldDuration = aggregated.getDuration();
long newDuration = sur.getDuration(); long surDuration = sur.getDuration();
suras.aggregate(sur); suras.aggregate(sur);
logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated); logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated);
aggregated.validate(); aggregated.validate();
Assert.assertTrue(aggregated.getDuration() == ((oldDuration + newDuration)/2)); long avgDuration = durationWeightedAverage(i-1, oldDuration, 1, surDuration);
Assert.assertTrue(aggregated.getDuration() == (avgDuration));
Assert.assertTrue(aggregated.getOperationCount() == i); Assert.assertTrue(aggregated.getOperationCount() == i);
if(minInvocationTime >= newDuration){ if(minInvocationTime >= surDuration){
Assert.assertTrue(aggregated.getMinInvocationTime() == newDuration); Assert.assertTrue(aggregated.getMinInvocationTime() == surDuration);
}else{ }else{
Assert.assertTrue(aggregated.getMinInvocationTime() == minInvocationTime); Assert.assertTrue(aggregated.getMinInvocationTime() == minInvocationTime);
} }
if(maxInvocationTime >= newDuration){ if(maxInvocationTime >= surDuration){
Assert.assertTrue(aggregated.getMaxInvocationTime() == maxInvocationTime); Assert.assertTrue(aggregated.getMaxInvocationTime() == maxInvocationTime);
}else{ }else{
Assert.assertTrue(aggregated.getMaxInvocationTime() == newDuration); Assert.assertTrue(aggregated.getMaxInvocationTime() == surDuration);
} }
Assert.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME)); Assert.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME));