]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/JobGenerator.java
Merge branch 'master' of ssh://dev.somenet.org:666/tu/lsdc
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / JobGenerator.java
1 package at.ac.tuwien.lsdc;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.Random;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import at.ac.tuwien.lsdc.types.ScenarioType;
12 import au.com.bytecode.opencsv.CSVWriter;
13
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: scenario, count, targetFile";
18
19         private static ScenarioType scenario;
20         private static int count;
21         private static File targetFile;
22
23         private static int numWBA;
24         private static int numHPA;
25
26         private static Random rand = new Random();
27
28         private static CSVWriter writer;
29
30         public static void main(String[] args) throws Exception {
31                 if (args.length == 3) {
32                         doArgumentHandling(args);
33                 } else if (args.length == 1) {
34                         String[] newArgs = args[0].split(" ");
35                         if (newArgs.length == 3) {
36                                 doArgumentHandling(newArgs);
37                         } else {
38                                 log.info(USAGE);
39                                 System.exit(1);
40                         }
41                 } else {
42                         log.info(USAGE);
43                         System.exit(1);
44                 }
45                 writer = new CSVWriter(new FileWriter(targetFile), ';',
46                                 CSVWriter.NO_QUOTE_CHARACTER);
47                 writeCSVHeader();
48                 int timestamp = 1;
49                 for (int i = 0; i < count; i++) {
50                         timestamp = timestamp + randomInt(0, 2);
51                         switch (scenario) {
52                         case A:
53                                 generateWBA(timestamp);
54                                 break;
55                         case B:
56                                 generateHPA(timestamp);
57                                 break;
58                         case C:
59                                 generateWBAorHPA(timestamp);
60                                 break;
61                         }
62                 }
63                 log.info("Created CSV " + targetFile.getName() + " for Scenario "
64                                 + scenario.toString());
65                 log.info("Total: " + (numWBA + numHPA) + " Expected: " + count);
66                 log.info("WBA: " + numWBA + " HPA: " + numHPA);
67                 writer.close();
68         }
69
70         private static void doArgumentHandling(String[] args) {
71                 log.info("handling args");
72                 String scenarioStr = args[0];
73                 String countStr = args[1];
74                 String targetFileStr = args[2];
75                 try {
76                         scenario = ScenarioType.valueOf(scenarioStr);
77                         boolean fileWriteOK = false;
78                         count = Integer.parseInt(countStr);
79                         targetFile = new File(targetFileStr);
80                         if (targetFile.exists()) {
81                                 fileWriteOK = targetFile.canWrite();
82                                 targetFile.delete();
83                                 targetFile.createNewFile();
84                         } else {
85                                 targetFile.createNewFile();
86                                 fileWriteOK = true;
87                         }
88                         if (!fileWriteOK)
89                                 throw new IOException("Can not write to File " + targetFileStr);
90                 } catch (Exception e) {
91                         e.printStackTrace();
92                         log.info(USAGE);
93                         System.exit(1);
94                 }
95         }
96
97         private static void writeCSVHeader() {
98                 String[] header = new String[] { "Timestamp", "Size", "RAM", "CPU",
99                                 "Duration" };
100                 writer.writeNext(header);
101         }
102
103         public static int randomInt(int low, int high) {
104                 return (rand.nextInt(high - low) + low);
105         }
106
107         private static void generateWBA(int timestamp) {
108                 int size = randomInt(20, 100);
109                 int ram = randomInt(30, 50);
110                 int cpu = randomInt(50, 100);
111                 int duration = randomInt(10, 100) + 75;
112                 numWBA++;
113                 writeApplication(timestamp,size,ram,cpu,duration);
114         }
115
116         private static void generateHPA(int timestamp) {
117                 int size = randomInt(150, 500);
118                 int ram = randomInt(100, 700);
119                 int cpu = randomInt(100, 400);
120                 int duration = randomInt(10, 100) + 75;
121                 numHPA++;
122                 writeApplication(timestamp,size,ram,cpu,duration);
123         }
124
125         private static void generateWBAorHPA(int timestamp) {
126                 boolean wba = rand.nextBoolean();
127                 if ((wba && (numWBA < (count / 2))) || (numHPA == (count / 2))) {
128                         generateWBA(timestamp);
129                 } else if (numHPA < (count / 2)) {
130                         generateHPA(timestamp);
131                 }
132         }
133
134         private static void writeApplication(int timestamp, int size, int ram,
135                         int cpu, int duration) {
136                 String[] app = new String[] { String.valueOf(timestamp),
137                                 String.valueOf(size), String.valueOf(ram), String.valueOf(cpu),
138                                 String.valueOf(duration) };
139                 writer.writeNext(app);
140         }
141 }