package test.stress; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.Map.Entry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import simulator.workload.generator.WorkloadGenerator; import simulator.workload.reader.archive.swf.SWFParser; import simulator.workload.writer.swf.Grms3SWFJobWriter; /** * Class responsible for generating experiments based on amount and range of * tasks. * * @author Jarek Szymczak * @version $Id$ */ public class TasksExperimentsGenerator extends AbstractExperimentsGenerator { /** Class logger. */ private static final Log LOGGER = LogFactory.getLog(TasksExperimentsGenerator.class); /** * It runs the resource range / values based generation * * @param args * the following arguments (other are not allowed - arguments * predecessed by "#" are integers): * * @return path to directory containing generated experiments * @throws FileNotFoundException * one of necessary files was not found * @throws IOException * signals that an I/O exception has occurred */ public String run(String args[]) throws FileNotFoundException, IOException { long max = 0; long[] values = null; if (args[0].equals("-g")) { long start = Long.parseLong(args[1]); long step = Long.parseLong(args[2]); max = Long.parseLong(args[3]); values = new long[(int) Math.ceil((double) (max - start) / (double) step)]; int i = 0; for (long j = start; j < max; j += step) { values[i++] = j; } } else if (args[0].equals("-v")) { values = new long[args.length - 2]; for (int i = 1; i < args.length - 1; i++) values[i - 1] = Long.parseLong(args[i]); max = Long.parseLong(args[args.length - 1]); } else { throw new RuntimeException("Class must be launched with -v or -g"); } LOGGER.info("Generating workload from range: " + 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("MAX_TASKS_NO", Long.toString(max)); createFileFromTemplate("properties/generator_template.properties", "properties/generator.properties", variables); createFileFromTemplate("properties/generator_template.xml", "properties/generator.xml", variables); 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); // deleteDirectory(new File("raz")); LOGGER.info("Dividing max number of tasks among other values"); for (long i : values) { LOGGER.debug("Workload with " + Long.toString(i) + " tasks is about to be truncated"); variables.setProperty("TASKS_NO", Long.toString(i)); copyFiles(i, variables.getProperty("EXP_PATH") + "/" + variables.getProperty("MAX_TASKS_NO"), variables .getProperty("EXP_PATH") + "/" + variables.getProperty("TASKS_NO"), variables); truncWorkloadFile(i, variables.getProperty("EXP_PATH") + "/" + variables.getProperty("MAX_TASKS_NO") + "/" + variables.getProperty("WORKLOAD_FILENAME"), variables .getProperty("EXP_PATH") + "/" + variables.getProperty("TASKS_NO") + "/" + variables.getProperty("WORKLOAD_FILENAME")); int zeroes = variables.getProperty("MAX_TASKS_NO").length() - variables.getProperty("TASKS_NO").length(); StringBuilder expPropertiesNo = new StringBuilder(); for (int j = 0; j < zeroes; j++) expPropertiesNo.append('0'); expPropertiesNo.append(variables.getProperty("TASKS_NO")); expPropertiesNo.append(".properties"); createFileFromTemplate( "properties/reservation_template.properties", variables .getProperty("EXP_PATH") + "/" + expPropertiesNo.toString(), variables); LOGGER.debug("Workload with " + Long.toString(i) + " tasks truncating finished"); } variables .setProperty("TASKS_NO", variables.getProperty("MAX_TASKS_NO")); createFileFromTemplate("properties/reservation_template.properties", variables.getProperty("EXP_PATH") + "/" + variables.getProperty("TASKS_NO") + ".properties", variables); LOGGER.info("Division of tasks finished"); return variables.getProperty("EXP_PATH"); } /** * It truncates workload file. * * @param numberOfExperiments * the number of experiments contained in originalWorkloadPath to * save in truncWorkloadPath * @param originalWorkloadPath * the original workload path * @param truncWorkloadPath * the truncated workload path * @throws IOException * Signals that an I/O exception has occurred. */ private void truncWorkloadFile(long numberOfExperiments, String originalWorkloadPath, String truncWorkloadPath) throws IOException { SWFParser swfParser = null; Grms3SWFJobWriter jobWriter = null; try { swfParser = new SWFParser(originalWorkloadPath); File temp = new File(truncWorkloadPath); jobWriter = new Grms3SWFJobWriter(temp.getParent(), temp.getName()); Map header = swfParser.loadHeader(); Set> commentsEntrySet = header.entrySet(); header.entrySet(); for (Entry entry : commentsEntrySet) { jobWriter.writeComment(entry.getKey(), entry.getValue()); } Map mapping = swfParser.getIDMapping(); String[] singleJob = null; String[] singleJobMapping = null; for (long i = 0; i < numberOfExperiments; i++) { singleJob = swfParser.readTask(); singleJobMapping = mapping.get(Long.toString(i)); jobWriter.writeLine(singleJob); jobWriter.addTaskMapping(Long.toString(i), singleJobMapping[0], singleJobMapping[1]); } } finally { if (swfParser != null) swfParser.close(); if (jobWriter != null) jobWriter.close(); } } }