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
This commit is contained in:
Luca Frosini 2015-07-01 13:13:55 +00:00
parent 0907cc7810
commit 4c9e924f9e
5 changed files with 187 additions and 67 deletions

View File

@ -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 {
}
});
}
*/
}

View File

@ -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);
}
}

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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;
}