]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/JobGenerator.java
implemented parsing of CSV in SchedSimulator
[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: Szenario, 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 CSVWriter writer;
27
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);
35                         } else {
36                                 log.info(USAGE);
37                                 System.exit(1);
38                         }
39                 }
40                 writer = new CSVWriter(new FileWriter(targetFile), ';',
41                                 CSVWriter.NO_QUOTE_CHARACTER);
42                 writeCSVHeader();
43                 int timestamp = 1;
44                 for (int i = 0; i < count; i++) {
45                         timestamp = timestamp + randomInt(1, 10);
46                         switch (scenario) {
47                         case A:
48                                 generateWBA(timestamp);
49                                 break;
50                         case B:
51                                 generateHPA(timestamp);
52                                 break;
53                         case C:
54                                 generateWBAorHPA(timestamp);
55                                 break;
56                         }
57                 }
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);
62                 writer.close();
63         }
64
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];
70                 try {
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();
77                                 targetFile.delete();
78                                 targetFile.createNewFile();
79                         } else {
80                                 targetFile.createNewFile();
81                                 fileWriteOK = true;
82                         }
83                         if (!fileWriteOK)
84                                 throw new IOException("Can not write to File " + targetFileStr);
85                 } catch (Exception e) {
86                         e.printStackTrace();
87                         log.info(USAGE);
88                         System.exit(1);
89                 }
90         }
91
92         private static void writeCSVHeader() {
93                 String[] header = new String[] { "Timestamp", "Size", "RAM", "CPU",
94                                 "Duration" };
95                 writer.writeNext(header);
96         }
97
98         private static int randomInt(int low, int high) {
99                 Random r = new Random();
100                 return (r.nextInt(high - low) + low);
101         }
102
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);
108                 numWBA++;
109                 writeApplication(timestamp,size,ram,cpu,duration);
110         }
111
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);
117                 numHPA++;
118                 writeApplication(timestamp,size,ram,cpu,duration);
119         }
120
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);
128                 }
129         }
130
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);
137         }
138 }