1 package at.ac.tuwien.lsdc;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.Random;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
11 import at.ac.tuwien.lsdc.types.ScenarioType;
12 import au.com.bytecode.opencsv.CSVWriter;
14 public class JobGenerator {
15 private static final Logger log = LoggerFactory
16 .getLogger(JobGenerator.class);
17 private static final String USAGE = "JobGenerator needs exactly 3 parameters: Szenario, count, targetFile";
19 private static ScenarioType scenario;
20 private static int count;
21 private static File targetFile;
23 private static int numWBA;
24 private static int numHPA;
26 private static CSVWriter writer;
28 public static void main(String[] args) throws Exception {
29 if (args.length == 3) {
30 doArgumentHandling(args);
31 } else if (args.length == 1) {
32 String[] newArgs = args[0].split(" ");
33 if (newArgs.length == 3) {
34 doArgumentHandling(newArgs);
40 writer = new CSVWriter(new FileWriter(targetFile), ';',
41 CSVWriter.NO_QUOTE_CHARACTER);
44 for (int i = 0; i < count; i++) {
45 timestamp = timestamp + randomInt(1, 10);
48 generateWBA(timestamp);
51 generateHPA(timestamp);
54 generateWBAorHPA(timestamp);
58 log.info("Created CSV " + targetFile.getName() + " for Scenario "
59 + scenario.toString());
60 log.info("Total: " + (numWBA + numHPA) + " Expected: " + count);
61 log.info("WBA: " + numWBA + " HPA: " + numHPA);
65 private static void doArgumentHandling(String[] args) {
66 log.info("handling args");
67 String scenarioStr = args[0];
68 String countStr = args[1];
69 String targetFileStr = args[2];
71 scenario = ScenarioType.valueOf(scenarioStr);
72 boolean fileWriteOK = false;
73 count = Integer.parseInt(countStr);
74 targetFile = new File(targetFileStr);
75 if (targetFile.exists()) {
76 fileWriteOK = targetFile.canWrite();
78 targetFile.createNewFile();
80 targetFile.createNewFile();
84 throw new IOException("Can not write to File " + targetFileStr);
85 } catch (Exception e) {
92 private static void writeCSVHeader() {
93 String[] header = new String[] { "Timestamp", "Size", "RAM", "CPU",
95 writer.writeNext(header);
98 private static int randomInt(int low, int high) {
99 Random r = new Random();
100 return (r.nextInt(high - low) + low);
103 private static void generateWBA(int timestamp) {
104 int size = randomInt(20, 100);
105 int ram = randomInt(30, 50);
106 int cpu = randomInt(50, 100);
107 int duration = randomInt(1, 50);
109 writeApplication(timestamp,size,ram,cpu,duration);
112 private static void generateHPA(int timestamp) {
113 int size = randomInt(150, 500);
114 int ram = randomInt(100, 400);
115 int cpu = randomInt(100, 700);
116 int duration = randomInt(1, 50);
118 writeApplication(timestamp,size,ram,cpu,duration);
121 private static void generateWBAorHPA(int timestamp) {
122 Random r = new Random();
123 boolean wba = r.nextBoolean();
124 if (wba && (numWBA < (count / 2))) {
125 generateWBA(timestamp);
126 } else if (numHPA < (count / 2)) {
127 generateHPA(timestamp);
131 private static void writeApplication(int timestamp, int size, int ram,
132 int cpu, int duration) {
133 String[] app = new String[] { String.valueOf(timestamp),
134 String.valueOf(size), String.valueOf(ram), String.valueOf(cpu),
135 String.valueOf(duration) };
136 writer.writeNext(app);