tools.service.AbstractService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brunner-core-tools Show documentation
Show all versions of brunner-core-tools Show documentation
Tools module for the BRunner project
The newest version!
package tools.service;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import org.slf4j.Logger;
import api.running.IService;
import api.running.ITaskResult;
import measure.MeasureContext;
import measure.OperationMeasurer;
import runners.utils.StringUtils;
import tools.plan.IServiceOperationsPlan;
import tools.plan.ServiceOperationsPlan;
import tools.service.metadata.IOperationMetadata;
/**
* Abstract service.
*/
public abstract class AbstractService implements IService {
protected IServiceOperationsPlan operations;
protected List executionTimes;
protected InputScenarioType inputDataScenario;
protected InputAlgorithmConfigurationType inputAlgorithmParameters;
protected static Logger LOG;
/**
* @param ids input data scenario
* @param iap algorithm parameters
*/
public AbstractService(InputScenarioType ids, InputAlgorithmConfigurationType iap) {
this.inputDataScenario = ids;
this.inputAlgorithmParameters = iap;
this.operations = new ServiceOperationsPlan();
this.executionTimes = new ArrayList<>();
}
/**
*
* Any operation that shouldn't be measured should be done here.
*
* @throws Exception
*
*/
public final void beforeTrial() throws Exception {
/*
* Initialize operations to execute in this trial
*/
prepareServiceSpecificOperations();
LOG.info("\n\n" + StringUtils.printBanner("TRIAL SETUP", 0) + getInputDataScenario().toString()
+ getInputAlgorithmParameters().toString());
/*
* Operations run setup operations
*/
var setupResults = OperationMeasurer.measureExecutionTime(operations.getSetupOperations(),
MeasureContext.SETUP);
executionTimes.addAll(setupResults);
}
protected InputScenarioType getInputDataScenario() {
return this.inputDataScenario;
}
protected InputAlgorithmConfigurationType getInputAlgorithmParameters() {
return this.inputAlgorithmParameters;
}
public final void beforeIteration() throws Exception {
}
public void afterIteration() throws Exception {
postProcessingIteration();
printResults();
}
public void afterTrial() throws Exception {
postProcessingTrial();
close();
}
/**
* default implementation
*/
protected void close() throws Exception {
}
/**
*
* Service executions.
*
* @throws Exception
*
*/
public void executeService() throws Exception {
executionTimes
.addAll(OperationMeasurer.measureExecutionTime(operations.getOperations(), MeasureContext.EXECUTION));
}
/*
* Returns the measured times.
*
*/
public List getServiceResult() {
return executionTimes;
}
/**
* registers setup operations
*
* @param description
* @param operation
*/
protected void setup(IOperationMetadata description, Runnable operation) {
operations.setupRunnable(description, operation);
}
/**
* register an operation
*
* @param description
* @param operation
*/
protected void operation(IOperationMetadata description, Callable> operation) {
operations.addCallable(description, operation);
}
/**
* register an operation
*
* @param description
* @param operation
*/
protected void operationC(IOperationMetadata description, Runnable operation) {
operations.addRunnable(description, operation);
}
/**
* register an operation
*
* @param description
* @param operation
*/
protected void operation(IOperationMetadata description, Runnable operation) {
operations.addRunnable(description, operation);
}
/**
*
* Specific methods to be implemented by subclasses.
*
*/
protected abstract void prepareServiceSpecificOperations() throws Exception;
protected void postProcessingTrial() {
};
protected void postProcessingIteration() {
};
private void printResults() {
LOG.info(StringUtils.buildBoxedLogMessage("ITERATION RUNNING TIMES", 0));
executionTimes.forEach(measure -> {
LOG.info(measure.toString());
});
}
}