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_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)
throws NotAggregatableRecordsExceptions {
try {
t.setDuration(durationWeightedAverage(record));
t.setOperationCount(t.getOperationCount() + record.getOperationCount());
t.setDuration((t.getDuration() + record.getDuration()) / 2);
long max = record.getMaxInvocationTime();
if(max > t.getMaxInvocationTime()){

View File

@ -106,6 +106,10 @@ public class ServiceUsageRecordAggregationStrategyTest {
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
public void aggregationStressTest() throws InvalidValueException, NotAggregatableRecordsExceptions {
@ -131,25 +135,26 @@ public class ServiceUsageRecordAggregationStrategyTest {
long minInvocationTime = aggregated.getMinInvocationTime();
long maxInvocationTime = aggregated.getMaxInvocationTime();
long oldDuration = aggregated.getDuration();
long newDuration = sur.getDuration();
long surDuration = sur.getDuration();
suras.aggregate(sur);
logger.debug("Resulting Aggregated ServiceUsageRecord: {}", aggregated);
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);
if(minInvocationTime >= newDuration){
Assert.assertTrue(aggregated.getMinInvocationTime() == newDuration);
if(minInvocationTime >= surDuration){
Assert.assertTrue(aggregated.getMinInvocationTime() == surDuration);
}else{
Assert.assertTrue(aggregated.getMinInvocationTime() == minInvocationTime);
}
if(maxInvocationTime >= newDuration){
if(maxInvocationTime >= surDuration){
Assert.assertTrue(aggregated.getMaxInvocationTime() == maxInvocationTime);
}else{
Assert.assertTrue(aggregated.getMaxInvocationTime() == newDuration);
Assert.assertTrue(aggregated.getMaxInvocationTime() == surDuration);
}
Assert.assertFalse(aggregated.getResourceProperties().containsKey(TestUsageRecord.TEST_PROPERTY_NAME));