Implemented realistic random ServiceUsageREcord generation

This commit is contained in:
Luca Frosini 2021-03-17 11:37:08 +01:00
parent e6f6a18b53
commit 7cbfc33ecf
1 changed files with 146 additions and 24 deletions

View File

@ -3,7 +3,12 @@
*/
package org.gcube.documentstore.persistence;
import java.util.Calendar;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
import org.gcube.accounting.datamodel.aggregation.AggregatedJobUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedPortletUsageRecord;
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
@ -27,40 +32,146 @@ public class PersistencePostgreSQLTest extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQLTest.class);
/*
protected UsageRecord getTestServiceUsageRecord() throws Exception {
public static final String[] scopes = new String[]{
"/gcube",
"/gcube/devsec", "/gcube/devsec/devVRE",
"/gcube/devNext", "/gcube/devNext/NextNext"
};
public static final String[] calledMethods = new String[]{"create", "read", "update", "delete", "purge", "execute", "addToContext", "removeFromContext", "other"};
public static final String[] users = new String[]{"luca.frosini", "lucio.lelii", "francesco.frangiacrapa", "fabio.sinibaldi", "massimiliano.assante", "giancarlo.panichi", "leonardo.candela", "pasquale.pagano"};
public static final String[] serviceClasses = new String[]{
"information-system",
"data-publishing",
"data-catalogue",
"vre-management",
"accounting",
"data-access",
"transfer"
};
public static final String[] serviceNames = new String[]{
"resource-registry", "registry-publisher",
"catalogue-ws", "sdmx-publisher",
"gcat", "grsf-publisher-ws",
"smart-executor", "ghn-manager",
"accounting-service", "accounting-analytics",
"storage-hub", "species-products-discovery",
"data-transfer-service", "uri-resolver"
};
public static final int minutesInAnYear;
public static final int maxDuration = 450;
private static final Random random;
static {
random = new Random();
minutesInAnYear = (int) TimeUnit.DAYS.toMinutes(365);
}
public static void setCalledMethod(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(calledMethods.length);
serviceUsageRecord.setCalledMethod(calledMethods[randomNumber]);
}
public static void setCallerHostAndHost(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(25);
serviceUsageRecord.setCallerHost("host" + randomNumber + ".d4science.org");
serviceUsageRecord.setHost("host" + (25-randomNumber) + ".d4science.org");
}
public static String getRandomUser() {
int randomNumber = random.nextInt(users.length);
return users[randomNumber];
}
public static void setConsumerId(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
serviceUsageRecord.setConsumerId(getRandomUser());
}
public static void setServiceClassAndName(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomNumber = random.nextInt(serviceClasses.length);
serviceUsageRecord.setServiceClass(serviceClasses[randomNumber]);
int randomInt = random.nextInt(2);
serviceUsageRecord.setServiceName(serviceNames[randomNumber+randomInt]);
}
public static double randomBetween(int min, int max) {
return (Math.random()*(max-min+1)+min);
}
public static void setTiming(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int operationCount = random.nextInt(55);
operationCount = operationCount + 1;
serviceUsageRecord.setOperationCount(operationCount);
int minutesToSubstract = random.nextInt(minutesInAnYear);
Calendar creationTime = Calendar.getInstance();
creationTime.add(Calendar.MINUTE, -minutesToSubstract);
serviceUsageRecord.setCreationTime(creationTime);
long duration = (long) randomBetween(90, maxDuration);
serviceUsageRecord.setDuration(duration);
if(operationCount==1) {
serviceUsageRecord.setMaxInvocationTime(duration);
serviceUsageRecord.setMinInvocationTime(duration);
serviceUsageRecord.setEndTime(creationTime);
serviceUsageRecord.setStartTime(creationTime);
}else {
// Random number between generated duration and 678;
long maxInvocationTime = (long) randomBetween((int) duration, 678);
serviceUsageRecord.setMaxInvocationTime(maxInvocationTime);
// Random number between 30 and generated duration
long minInvocationTime = (long) randomBetween(30, (int) duration);
serviceUsageRecord.setMinInvocationTime(minInvocationTime);
Calendar startTime = Calendar.getInstance();
startTime.setTimeInMillis(creationTime.getTimeInMillis());
int startTimeMinutesToSubstract = (int) randomBetween(2880, 1440);
startTime.add(Calendar.MINUTE, -startTimeMinutesToSubstract);
serviceUsageRecord.setStartTime(startTime);
Calendar endTime = Calendar.getInstance();
endTime.setTimeInMillis(creationTime.getTimeInMillis());
int edTimeMinutesToSubstract = (int) randomBetween(startTimeMinutesToSubstract, 60);
endTime.add(Calendar.MINUTE, -edTimeMinutesToSubstract);
serviceUsageRecord.setEndTime(endTime);
}
}
public static void setOperationResult(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomInt = random.nextInt(OperationResult.values().length);
serviceUsageRecord.setOperationResult(OperationResult.values()[randomInt]);
}
public static void setScope(AggregatedServiceUsageRecord serviceUsageRecord) throws Exception {
int randomInt = random.nextInt(scopes.length);
serviceUsageRecord.setScope(scopes[randomInt]);
}
protected AggregatedServiceUsageRecord getTestServiceUsageRecord() throws Exception {
AggregatedServiceUsageRecord serviceUsageRecord = new AggregatedServiceUsageRecord();
serviceUsageRecord.setAggregated(true);
Calendar creationTime = Calendar.getInstance();
serviceUsageRecord.setCreationTime(creationTime);
setCalledMethod(serviceUsageRecord);
setCallerHostAndHost(serviceUsageRecord);
setConsumerId(serviceUsageRecord);
Calendar startTime = Calendar.getInstance();
serviceUsageRecord.setStartTime(startTime);
serviceUsageRecord.setCalledMethod("createResource");
serviceUsageRecord.setCallerHost("localhost");
serviceUsageRecord.setHost("localhost");
serviceUsageRecord.setConsumerId("luca.frosini");
serviceUsageRecord.setCallerQualifier("TOKEN");
serviceUsageRecord.setDuration((long) 100);
serviceUsageRecord.setMaxInvocationTime((long) 150);
serviceUsageRecord.setMinInvocationTime((long) 50);
serviceUsageRecord.setOperationCount(9);
serviceUsageRecord.setOperationResult(OperationResult.SUCCESS);
serviceUsageRecord.setScope(ContextTest.getCurrentContextFullName());
setTiming(serviceUsageRecord);
Calendar endTime = Calendar.getInstance();
endTime.add(Calendar.MINUTE, 5);
serviceUsageRecord.setEndTime(endTime);
setOperationResult(serviceUsageRecord);
serviceUsageRecord.setServiceClass("information-system");
serviceUsageRecord.setServiceName("resource-registry");
setScope(serviceUsageRecord);
setServiceClassAndName(serviceUsageRecord);
return serviceUsageRecord;
}
*/
protected AggregatedJobUsageRecord getTestAggregatedJobUsageRecord() throws Exception {
JobUsageRecord jobUsageRecord = TestUsageRecord.createTestJobUsageRecord();
AggregatedJobUsageRecord aggregatedJobUsageRecord = new AggregatedJobUsageRecord(jobUsageRecord);
@ -145,6 +256,17 @@ public class PersistencePostgreSQLTest extends ContextTest {
persistencePostgreSQL.newConnection();
}
AggregatedServiceUsageRecord usageRecord = getTestServiceUsageRecord();
/*
logger.debug("operationCount : {} - min : {} duration : {} - max : {}",
usageRecord.getOperationCount(),
usageRecord.getMinInvocationTime(),
usageRecord.getDuration(),
usageRecord.getMaxInvocationTime());
*/
persistencePostgreSQL.insert(usageRecord);
/*
UsageRecord usageRecord = getTestAggregatedJobUsageRecord();
persistencePostgreSQL.insert(usageRecord);
@ -160,7 +282,7 @@ public class PersistencePostgreSQLTest extends ContextTest {
usageRecord = getTestAggregatedStorageUsageRecord();
persistencePostgreSQL.insert(usageRecord);
*/
}
persistencePostgreSQL.commitAndClose();
}