package test.stress;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import simulator.workload.generator.WorkloadGenerator;
/**
* Class responsible for generating experiments based on amount and range of
* resources and a certain amount of tasks.
*
* @author Jarek Szymczak
* @version $Id$
*/
public class ResourcesExperimentsGenerator extends AbstractExperimentsGenerator {
/** Class logger. */
private static final Log LOGGER =
LogFactory.getLog(ResourcesExperimentsGenerator.class);
/**
* It runs the resource range / values based generation.
*
* @param args
* the following arguments (other are not allowed - arguments
* predecessed by "#" are integers):
*
* - -g #tasks #start #step #end - experiments with
* #tasks number of tasks will be generated for range of
* resources from #start to #end with step #step
*
- -v #tasks #resources_1 ... #resources_n -
* experiments with #tasks number of tasks will be generated for
* #resources_1, ..., #resources_n number of resources
*
* @return path to directory containing generated experiments
*/
public String run(String args[]) {
long max = 0;
long tasks = 0;
long[] values = null;
if (args[0].equals("-g")) {
tasks = Long.parseLong(args[1]);
long start = Long.parseLong(args[2]);
long step = Long.parseLong(args[3]);
max = Long.parseLong(args[4]);
values =
new long[(int) Math.ceil((double) (max - start)
/ (double) step) + 1];
for (long j = start; j <= max; j += step) {
values[(int) ((j - start) / step)] = j;
}
} else if (args[0].equals("-v")) {
tasks = Long.parseLong(args[1]);
values = new long[args.length - 2];
max = Long.parseLong(args[args.length - 1]);
for (int i = 2; i < args.length; i++) {
values[i - 2] = Long.parseLong(args[i]);
}
} else {
throw new RuntimeException("Class must be launched with -v or -g");
}
LOGGER
.info("Generating workload from range (for resource experiments): "
+ Long.toString(values[0]) + "-" + Long.toString(max));
Properties variables = new Properties();
FileInputStream fis = null;
try {
fis =
new FileInputStream(new File(
"properties/variables.properties"));
variables.load(fis);
} catch (FileNotFoundException e) {
LOGGER
.error("Could not find variables.properties file, application will terminate!");
throw new RuntimeException(e);
} catch (IOException e) {
LOGGER
.error("Problems with I/O operations while trying to load variables.properties file, application will terminate!");
throw new RuntimeException(e);
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
LOGGER
.error("An error occured while closing the stream with variables");
}
}
variables
.setProperty("EXP_PATH", variables.getProperty("RES_EXP_PATH"));
variables.setProperty("MAX_RES_NO", Long.toString(max));
variables.setProperty("MAX_TASKS_NO", Long.toString(tasks));
try {
createFileFromTemplate("properties/generator_template.properties",
"properties/generator.properties", variables);
createFileFromTemplate("properties/generator_template.xml",
"properties/generator.xml", variables);
} catch (IOException e) {
LOGGER
.error("Error while trying to generate files needed for generation of tasks, applcation will terminate!");
throw new RuntimeException(e);
}
deleteDirectory(new File(variables.getProperty("EXP_PATH")));
File dir =
new File(variables.getProperty("EXP_PATH") + "/"
+ variables.getProperty("MAX_TASKS_NO"));
if (!dir.exists()) {
try {
dir.mkdirs();
} catch (SecurityException e) {
LOGGER
.error("Security violation while trying to create directories: "
+ dir.getAbsolutePath() + "!");
}
}
String parameters[] = { "properties/generator.properties" };
WorkloadGenerator.main(parameters);
LOGGER.info("Dividing max number of tasks among other values");
variables
.setProperty("TASKS_NO", variables.getProperty("MAX_TASKS_NO"));
for (long i : values) {
variables.setProperty("RES_NO", Long.toString(i));
variables.setProperty("RESOURCES_PATH", variables
.getProperty("EXP_PATH")
+ "/AResources128_" + Long.toString(i) + ".xml");
int zeroes =
variables.getProperty("MAX_RES_NO").length()
- variables.getProperty("RES_NO").length();
StringBuilder expPropertiesNo =
new StringBuilder(variables.getProperty("TASKS_NO"));
expPropertiesNo.append("R");
for (int j = 0; j < zeroes; j++)
expPropertiesNo.append('0');
expPropertiesNo.append(variables.getProperty("RES_NO"));
expPropertiesNo.append(".properties");
try {
createResourcesFromSingleTemplate(i,
"properties/resource_template.part",
"properties/resources_template.xml", variables);
createFileFromTemplate(
"properties/reservation_template.properties", variables
.getProperty("EXP_PATH")
+ "/" + expPropertiesNo.toString(), variables);
} catch (IOException e) {
LOGGER.error("An exception occured while trying to ");
}
}
return variables.getProperty("EXP_PATH");
}
/**
* Generates the resource file (it's directory is specified in variables and
* file name depends on amount of resources it contains) from general and
* single templates. It's based on variables replacement mechanism in method
*
* @param numberOfResources
* the number of resources the
* @param templatePath
* the template path
* @param generalTemplatePath
* the general template path
* @param variables
* the variables for resources generation
* @throws IOException
* Signals that an I/O exception has occurred.
* {@link #substituteVariables(String, Properties)}. After
* preparation of variables it calls method:
* {@link #createFileFromTemplate(String, String, Properties)}
*/
private void createResourcesFromSingleTemplate(long numberOfResources,
String templatePath, String generalTemplatePath,
Properties variables) throws IOException {
File template = new File(templatePath);
BufferedReader reader = new BufferedReader(new FileReader(template));
String line = "";
StringBuilder oldtext = new StringBuilder();
try {
while ((line = reader.readLine()) != null) {
oldtext.append(line);
oldtext.append('\n');
}
} finally {
if (reader != null)
reader.close();
}
StringBuilder newtext = new StringBuilder();
for (long i = 0; i < numberOfResources; i++) {
variables.setProperty("RESOURCE_ID", "compRes" + Long.toString(i));
newtext.append(substituteVariables(oldtext.toString(), variables));
}
variables.setProperty("RESOURCES_DETAILS", newtext.toString());
createFileFromTemplate(generalTemplatePath, variables
.getProperty("RESOURCES_PATH"), variables);
}
}