From 4c9e924f9e8f83028351aa10ca42b19ee6e14396 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 1 Jul 2015 13:13:55 +0000 Subject: [PATCH] refs #200: Create accouting-lib library https://support.d4science.org/issues/200 Adding tests git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115756 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../scheduler/AggregationSchedulerTest.java | 109 ++++++++++++++++++ .../ServiceUsageRecordTest.java | 27 ----- .../persistence/PersistenceTest.java | 66 +++++------ .../testutility/StressTestUtility.java | 37 ++++++ .../accounting/testutility/TestOperation.java | 15 +++ 5 files changed, 187 insertions(+), 67 deletions(-) create mode 100644 src/test/java/org/gcube/accounting/aggregation/scheduler/AggregationSchedulerTest.java delete mode 100644 src/test/java/org/gcube/accounting/datamodel/implementation/ServiceUsageRecordTest.java create mode 100644 src/test/java/org/gcube/accounting/testutility/StressTestUtility.java create mode 100644 src/test/java/org/gcube/accounting/testutility/TestOperation.java diff --git a/src/test/java/org/gcube/accounting/aggregation/scheduler/AggregationSchedulerTest.java b/src/test/java/org/gcube/accounting/aggregation/scheduler/AggregationSchedulerTest.java new file mode 100644 index 0000000..47afc79 --- /dev/null +++ b/src/test/java/org/gcube/accounting/aggregation/scheduler/AggregationSchedulerTest.java @@ -0,0 +1,109 @@ +/** + * + */ +package org.gcube.accounting.aggregation.scheduler; + +import org.gcube.accounting.datamodel.SingleUsageRecord; +import org.gcube.accounting.datamodel.TestUsageRecord; +import org.gcube.accounting.datamodel.UsageRecord; +import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; +import org.gcube.accounting.persistence.PersistenceExecutor; +import org.gcube.accounting.testutility.StressTestUtility; +import org.gcube.accounting.testutility.TestOperation; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class AggregationSchedulerTest { + + private static final Logger logger = LoggerFactory.getLogger(AggregationSchedulerTest.class); + + public static AggregationScheduler getAggregationScheduler(){ + return AggregationScheduler.getInstance(); + } + + public static PersistenceExecutor persistenceExecutor = new PersistenceExecutor(){ + + @Override + public void persist(UsageRecord... usageRecords) throws Exception { + logger.debug(usageRecords.toString()); + } + + }; + + + @Test + public void stressTestAggregableURSingleType() throws Exception { + final AggregationScheduler aggregationScheduler = getAggregationScheduler(); + StressTestUtility.stressTest(new TestOperation() { + @Override + public void operate(int i) throws Exception { + SingleUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecord(); + aggregationScheduler.aggregate(usageRecord, persistenceExecutor); + } + }); + aggregationScheduler.flush(persistenceExecutor); + } + + public static final String ALTERNATIVE_SERVICE_CLASS = "ContentManagement"; + + @Test + public void stressTestDifferentAggregableURSingleType() throws Exception { + final AggregationScheduler aggregationScheduler = getAggregationScheduler(); + StressTestUtility.stressTest(new TestOperation() { + @Override + public void operate(int i) throws Exception { + ServiceUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecord(); + if(i%2==0){ + usageRecord.setServiceClass(ALTERNATIVE_SERVICE_CLASS); + } + aggregationScheduler.aggregate(usageRecord, persistenceExecutor); + } + }); + aggregationScheduler.flush(persistenceExecutor); + } + + @Test + public void stressTestDifferentAggregableURTwoType() throws Exception { + final AggregationScheduler aggregationScheduler = getAggregationScheduler(); + StressTestUtility.stressTest(new TestOperation() { + @Override + public void operate(int i) throws Exception { + SingleUsageRecord usageRecord; + if(i%2==0){ + usageRecord = TestUsageRecord.createTestServiceUsageRecord(); + }else{ + usageRecord = TestUsageRecord.createTestStorageUsageRecord(); + } + aggregationScheduler.aggregate(usageRecord, persistenceExecutor); + } + }); + aggregationScheduler.flush(persistenceExecutor); + } + + /* + @Test + public void stressTestDifferentAggregableURMultipleType() throws Exception { + StressTestUtility.stressTest(new TestOperation() { + @Override + public void operate(int i) throws Exception { + + } + }); + } + + @Test + public void stressTestDifferentAggregableAndNotAggregable() throws Exception { + StressTestUtility.stressTest(new TestOperation() { + @Override + public void operate(int i) throws Exception { + + } + }); + } + */ +} diff --git a/src/test/java/org/gcube/accounting/datamodel/implementation/ServiceUsageRecordTest.java b/src/test/java/org/gcube/accounting/datamodel/implementation/ServiceUsageRecordTest.java deleted file mode 100644 index 67256ad..0000000 --- a/src/test/java/org/gcube/accounting/datamodel/implementation/ServiceUsageRecordTest.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * - */ -package org.gcube.accounting.datamodel.implementation; - -import org.gcube.accounting.datamodel.TestUsageRecord; -import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; -import org.gcube.accounting.exception.InvalidValueException; -import org.gcube.accounting.persistence.Persistence; -import org.junit.Test; - -/** - * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ - * - */ -public class ServiceUsageRecordTest { - - @Test - public void exampleTest() throws InvalidValueException { - Persistence.setFallbackLocation(System.getProperty("user.home")); - Persistence persistence = Persistence.getInstance(); - - ServiceUsageRecord serviceUsageRecord = TestUsageRecord.createTestServiceUsageRecord(); - - persistence.account(serviceUsageRecord); - } -} diff --git a/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java b/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java index 87f8956..921b4ab 100644 --- a/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java +++ b/src/test/java/org/gcube/accounting/persistence/PersistenceTest.java @@ -3,15 +3,11 @@ */ package org.gcube.accounting.persistence; -import java.util.Calendar; -import java.util.GregorianCalendar; - import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.TestUsageRecord; -import org.gcube.accounting.persistence.Persistence; +import org.gcube.accounting.testutility.StressTestUtility; +import org.gcube.accounting.testutility.TestOperation; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ @@ -19,51 +15,41 @@ import org.slf4j.LoggerFactory; */ public class PersistenceTest { - private static final Logger logger = LoggerFactory.getLogger(PersistenceTest.class); + protected final static String HOME_SYSTEM_PROPERTY = "user.home"; + + public static Persistence getPersistence(){ + Persistence.setFallbackLocation(System.getProperty(HOME_SYSTEM_PROPERTY)); + return Persistence.getInstance(); + } @Test public void test() throws Exception { - Persistence.setFallbackLocation(System.getProperty("user.home")); - Persistence.getInstance(); + getPersistence(); } @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); + final Persistence persistence = getPersistence(); + StressTestUtility.stressTest(new TestOperation() { + @Override + public void operate(int i) { + SingleUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecord(); + persistence.validateAccountAggregate(usageRecord, true, false); + } + }); } @Test - public void stressTest() 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.account(usageRecord); - } - 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); + public void stressTestWithAggregation() throws Exception { + final Persistence persistence = getPersistence(); + StressTestUtility.stressTest(new TestOperation() { + @Override + public void operate(int i) throws Exception { + SingleUsageRecord usageRecord = TestUsageRecord.createTestServiceUsageRecord(); + persistence.account(usageRecord); + } + }); persistence.flush(); } diff --git a/src/test/java/org/gcube/accounting/testutility/StressTestUtility.java b/src/test/java/org/gcube/accounting/testutility/StressTestUtility.java new file mode 100644 index 0000000..90767ec --- /dev/null +++ b/src/test/java/org/gcube/accounting/testutility/StressTestUtility.java @@ -0,0 +1,37 @@ +/** + * + */ +package org.gcube.accounting.testutility; + +import java.util.Calendar; +import java.util.GregorianCalendar; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class StressTestUtility { + + private static final Logger logger = LoggerFactory.getLogger(StressTestUtility.class); + + + protected final static int NUMBER_OF_RECORDS = 3000; + + public static void stressTest(TestOperation operation) throws Exception { + Calendar startTestTime = new GregorianCalendar(); + for(int i=0; i< NUMBER_OF_RECORDS; i++){ + operation.operate(i); + } + Calendar stopTestTime = new GregorianCalendar(); + double startMillis = startTestTime.getTimeInMillis(); + double stopMillis = stopTestTime.getTimeInMillis(); + double duration = stopMillis - startMillis; + double average = (duration/NUMBER_OF_RECORDS); + logger.debug("Duration (in millisec) : " + duration); + logger.debug("Average (in millisec) : " + average); + } +} diff --git a/src/test/java/org/gcube/accounting/testutility/TestOperation.java b/src/test/java/org/gcube/accounting/testutility/TestOperation.java new file mode 100644 index 0000000..62406a4 --- /dev/null +++ b/src/test/java/org/gcube/accounting/testutility/TestOperation.java @@ -0,0 +1,15 @@ +/** + * + */ +package org.gcube.accounting.testutility; + + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public interface TestOperation { + + public void operate(int i) throws Exception; + +}