diff --git a/src/main/java/org/gcube/accounting/persistence/Persistence.java b/src/main/java/org/gcube/accounting/persistence/Persistence.java index a4529d7..573de1b 100644 --- a/src/main/java/org/gcube/accounting/persistence/Persistence.java +++ b/src/main/java/org/gcube/accounting/persistence/Persistence.java @@ -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}. * 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 */ public void account(final SingleUsageRecord usageRecord) throws InvalidValueException{ - /* Runnable runnable = new Runnable(){ @Override public void run(){ - 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()); - } + validateAccountAggregate(usageRecord, true, true); } }; 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 { diff --git a/src/test/java/org/gcube/accounting/datamodel/persistence/PersistenceTest.java b/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java similarity index 63% rename from src/test/java/org/gcube/accounting/datamodel/persistence/PersistenceTest.java rename to src/test/java/org/gcube/accounting/persistence/PersistenceTest.java index 433e807..87f8956 100644 --- a/src/test/java/org/gcube/accounting/datamodel/persistence/PersistenceTest.java +++ b/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java @@ -1,7 +1,7 @@ /** * */ -package org.gcube.accounting.datamodel.persistence; +package org.gcube.accounting.persistence; import java.util.Calendar; import java.util.GregorianCalendar; @@ -27,6 +27,25 @@ public class PersistenceTest { 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 public void stressTest() throws Exception { Persistence.setFallbackLocation(System.getProperty("user.home"));