refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Fixing tests

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115738 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-07-01 08:56:44 +00:00
parent 9318f46c28
commit 1556c23629
2 changed files with 47 additions and 37 deletions

View File

@ -152,6 +152,32 @@ public abstract class Persistence {
} }
} }
protected void validateAccountAggregate(final SingleUsageRecord usageRecord, boolean validate, boolean aggregate){
try {
if(validate){
usageRecord.validate();
}
if(aggregate){
aggregationScheduler.aggregate(usageRecord, new PersistenceExecutor(){
@Override
public void persist(UsageRecord... usageRecords) throws Exception {
persistence.accountWithFallback(usageRecords);
}
});
}else{
persistence.accountWithFallback(usageRecord);
}
} catch (InvalidValueException e) {
logger.error("Error validating UsageRecord", e.getCause());
} catch (Exception e) {
logger.error("Error accounting UsageRecord", e.getCause());
}
}
/** /**
* Persist the {@link #UsageRecord}. * Persist the {@link #UsageRecord}.
* The Record is validated first, then accounted, in a separated thread. * The Record is validated first, then accounted, in a separated thread.
@ -162,49 +188,14 @@ public abstract class Persistence {
* @throws InvalidValueException if the Record Validation Fails * @throws InvalidValueException if the Record Validation Fails
*/ */
public void account(final SingleUsageRecord usageRecord) throws InvalidValueException{ public void account(final SingleUsageRecord usageRecord) throws InvalidValueException{
/*
Runnable runnable = new Runnable(){ Runnable runnable = new Runnable(){
@Override @Override
public void run(){ public void run(){
try { validateAccountAggregate(usageRecord, true, true);
usageRecord.validate();
aggregationScheduler.aggregate(usageRecord, new PersistenceExecutor(){
@Override
public void persist(UsageRecord... usageRecords) throws Exception {
persistence.accountWithFallback(usageRecords);
}
});
} catch (InvalidValueException e) {
logger.error("Error validating UsageRecord", e.getCause());
} catch (Exception e) {
logger.error("Error accounting UsageRecord", e.getCause());
}
} }
}; };
pool.execute(runnable); pool.execute(runnable);
*/
try {
//usageRecord.validate();
aggregationScheduler.aggregate(usageRecord, new PersistenceExecutor(){
@Override
public void persist(UsageRecord... usageRecords) throws Exception {
persistence.accountWithFallback(usageRecords);
}
});
} catch (InvalidValueException e) {
logger.error("Error validating UsageRecord", e.getCause());
} catch (Exception e) {
logger.error("Error accounting UsageRecord", e.getCause());
}
} }
public void flush() throws Exception { public void flush() throws Exception {

View File

@ -1,7 +1,7 @@
/** /**
* *
*/ */
package org.gcube.accounting.datamodel.persistence; package org.gcube.accounting.persistence;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
@ -27,6 +27,25 @@ public class PersistenceTest {
Persistence.getInstance(); Persistence.getInstance();
} }
@Test
public void stressTestNoAggregation() throws Exception {
Persistence.setFallbackLocation(System.getProperty("user.home"));
Persistence persistence = Persistence.getInstance();
int quantity = 3000;
Calendar startTestTime = new GregorianCalendar();
for(int i=0; i< quantity; i++){
SingleUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecord();
persistence.validateAccountAggregate(usageRecord, true, false);
}
Calendar stopTestTime = new GregorianCalendar();
double startMillis = startTestTime.getTimeInMillis();
double stopMillis = stopTestTime.getTimeInMillis();
double duration = stopMillis - startMillis;
double average = (duration/quantity);
logger.debug("Duration (in millisec) : " + duration);
logger.debug("Average (in millisec) : " + average);
}
@Test @Test
public void stressTest() throws Exception { public void stressTest() throws Exception {
Persistence.setFallbackLocation(System.getProperty("user.home")); Persistence.setFallbackLocation(System.getProperty("user.home"));