75 lines
2.4 KiB
Java
75 lines
2.4 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.accounting.persistence;
|
|
|
|
import java.util.UUID;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import org.gcube.accounting.datamodel.UsageRecord;
|
|
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
|
|
import org.gcube.accounting.persistence.AccountingPersistence;
|
|
import org.gcube.accounting.testutility.StressTestUtility;
|
|
import org.gcube.accounting.testutility.TestOperation;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
import org.gcube.documentstore.exception.InvalidValueException;
|
|
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
|
|
import org.junit.Test;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
*
|
|
*/
|
|
public class AccountingPersistenceTest {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceTest.class);
|
|
|
|
public static final String[] SCOPES = new String[]{
|
|
"/gcube",
|
|
"/gcube/devsec", "/gcube/devsec/devVRE",
|
|
"/gcube/devNext", "/gcube/devNext/NextNext"
|
|
};
|
|
|
|
public static final long timeout = 5000;
|
|
public static final TimeUnit timeUnit = TimeUnit.MILLISECONDS;
|
|
|
|
@Test
|
|
public void stressTest() throws Exception {
|
|
final AccountingPersistence persistence = AccountingPersistence.getInstance();
|
|
PersistenceBackendFactory.setFallbackLocation(null);
|
|
StressTestUtility.stressTest(new TestOperation() {
|
|
@Override
|
|
public void operate(int i) {
|
|
int randomNumber = ThreadLocalRandom.current().nextInt(0, 5);
|
|
ScopeProvider.instance.set(SCOPES[randomNumber]);
|
|
UsageRecord usageRecord = null;
|
|
switch (i%2) {
|
|
case 0:
|
|
usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
|
|
break;
|
|
case 1:
|
|
usageRecord = TestUsageRecord.createTestStorageUsageRecordAutomaticScope();
|
|
break;
|
|
default:
|
|
usageRecord = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
|
|
break;
|
|
}
|
|
try {
|
|
usageRecord.setConsumerId(UUID.randomUUID().toString());
|
|
persistence.account(usageRecord);
|
|
} catch (InvalidValueException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
});
|
|
logger.debug(" START -----------------------------------------------");
|
|
logger.debug("Flushing the buffered records");
|
|
persistence.flush(timeout, timeUnit);
|
|
logger.debug(" END -----------------------------------------------");
|
|
|
|
}
|
|
}
|