1 | package test.stress; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileInputStream; |
---|
5 | import java.io.FileNotFoundException; |
---|
6 | import java.io.IOException; |
---|
7 | import java.util.Map; |
---|
8 | import java.util.Properties; |
---|
9 | import java.util.Set; |
---|
10 | import java.util.Map.Entry; |
---|
11 | |
---|
12 | import org.apache.commons.logging.Log; |
---|
13 | import org.apache.commons.logging.LogFactory; |
---|
14 | |
---|
15 | import simulator.workload.generator.WorkloadGenerator; |
---|
16 | import simulator.workload.reader.archive.swf.SWFParser; |
---|
17 | import simulator.workload.writer.swf.Grms3SWFJobWriter; |
---|
18 | |
---|
19 | /** |
---|
20 | * Class responsible for generating experiments based on amount and range of |
---|
21 | * tasks. |
---|
22 | * |
---|
23 | * @author Jarek Szymczak |
---|
24 | * @version $Id$ |
---|
25 | */ |
---|
26 | public class TasksExperimentsGenerator extends AbstractExperimentsGenerator { |
---|
27 | |
---|
28 | /** Class logger. */ |
---|
29 | private static final Log LOGGER = |
---|
30 | LogFactory.getLog(TasksExperimentsGenerator.class); |
---|
31 | |
---|
32 | /** |
---|
33 | * It runs the resource range / values based generation |
---|
34 | * |
---|
35 | * @param args |
---|
36 | * the following arguments (other are not allowed - arguments |
---|
37 | * predecessed by "#" are integers): |
---|
38 | * <ul> |
---|
39 | * <li><b>-g #start #step #end</b> - experiments with tasks |
---|
40 | * range from #start to #end with step #step will be generated |
---|
41 | * <li><b>-v #tasks_1 ... #tasks_n</b> - experiments with |
---|
42 | * #tasks_1, ..., #tasks_n number of tasks will be generated |
---|
43 | * </ul> |
---|
44 | * @return path to directory containing generated experiments |
---|
45 | * @throws FileNotFoundException |
---|
46 | * one of necessary files was not found |
---|
47 | * @throws IOException |
---|
48 | * signals that an I/O exception has occurred |
---|
49 | */ |
---|
50 | public String run(String args[]) throws FileNotFoundException, IOException { |
---|
51 | |
---|
52 | long max = 0; |
---|
53 | long[] values = null; |
---|
54 | |
---|
55 | if (args[0].equals("-g")) { |
---|
56 | |
---|
57 | long start = Long.parseLong(args[1]); |
---|
58 | long step = Long.parseLong(args[2]); |
---|
59 | max = Long.parseLong(args[3]); |
---|
60 | |
---|
61 | values = |
---|
62 | new long[(int) Math.ceil((double) (max - start) |
---|
63 | / (double) step)]; |
---|
64 | int i = 0; |
---|
65 | for (long j = start; j < max; j += step) { |
---|
66 | values[i++] = j; |
---|
67 | } |
---|
68 | } else if (args[0].equals("-v")) { |
---|
69 | values = new long[args.length - 2]; |
---|
70 | for (int i = 1; i < args.length - 1; i++) |
---|
71 | values[i - 1] = Long.parseLong(args[i]); |
---|
72 | max = Long.parseLong(args[args.length - 1]); |
---|
73 | } else { |
---|
74 | throw new RuntimeException("Class must be launched with -v or -g"); |
---|
75 | } |
---|
76 | |
---|
77 | LOGGER.info("Generating workload from range: " |
---|
78 | + Long.toString(values[0]) + "-" + Long.toString(max)); |
---|
79 | |
---|
80 | Properties variables = new Properties(); |
---|
81 | FileInputStream fis = null; |
---|
82 | try { |
---|
83 | fis = |
---|
84 | new FileInputStream(new File( |
---|
85 | "properties/variables.properties")); |
---|
86 | variables.load(fis); |
---|
87 | } catch (FileNotFoundException e) { |
---|
88 | LOGGER |
---|
89 | .error("Could not find variables.properties file, application will terminate!"); |
---|
90 | throw new RuntimeException(e); |
---|
91 | } catch (IOException e) { |
---|
92 | LOGGER |
---|
93 | .error("Problems with I/O operations while trying to load variables.properties file, application will terminate!"); |
---|
94 | throw new RuntimeException(e); |
---|
95 | } finally { |
---|
96 | try { |
---|
97 | if (fis != null) |
---|
98 | fis.close(); |
---|
99 | } catch (IOException e) { |
---|
100 | LOGGER |
---|
101 | .error("An error occured while closing the stream with variables"); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | variables.setProperty("MAX_TASKS_NO", Long.toString(max)); |
---|
106 | |
---|
107 | createFileFromTemplate("properties/generator_template.properties", |
---|
108 | "properties/generator.properties", variables); |
---|
109 | createFileFromTemplate("properties/generator_template.xml", |
---|
110 | "properties/generator.xml", variables); |
---|
111 | |
---|
112 | deleteDirectory(new File(variables.getProperty("EXP_PATH"))); |
---|
113 | |
---|
114 | File dir = |
---|
115 | new File(variables.getProperty("EXP_PATH") + "/" |
---|
116 | + variables.getProperty("MAX_TASKS_NO")); |
---|
117 | |
---|
118 | if (!dir.exists()) { |
---|
119 | try { |
---|
120 | dir.mkdirs(); |
---|
121 | } catch (SecurityException e) { |
---|
122 | LOGGER |
---|
123 | .error("Security violation while trying to create directories: " |
---|
124 | + dir.getAbsolutePath() + "!"); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | String parameters[] = { "properties/generator.properties" }; |
---|
129 | WorkloadGenerator.main(parameters); // deleteDirectory(new File("raz")); |
---|
130 | LOGGER.info("Dividing max number of tasks among other values"); |
---|
131 | for (long i : values) { |
---|
132 | |
---|
133 | LOGGER.debug("Workload with " + Long.toString(i) |
---|
134 | + " tasks is about to be truncated"); |
---|
135 | variables.setProperty("TASKS_NO", Long.toString(i)); |
---|
136 | |
---|
137 | copyFiles(i, variables.getProperty("EXP_PATH") + "/" |
---|
138 | + variables.getProperty("MAX_TASKS_NO"), variables |
---|
139 | .getProperty("EXP_PATH") |
---|
140 | + "/" + variables.getProperty("TASKS_NO"), variables); |
---|
141 | |
---|
142 | truncWorkloadFile(i, variables.getProperty("EXP_PATH") + "/" |
---|
143 | + variables.getProperty("MAX_TASKS_NO") + "/" |
---|
144 | + variables.getProperty("WORKLOAD_FILENAME"), variables |
---|
145 | .getProperty("EXP_PATH") |
---|
146 | + "/" |
---|
147 | + variables.getProperty("TASKS_NO") |
---|
148 | + "/" |
---|
149 | + variables.getProperty("WORKLOAD_FILENAME")); |
---|
150 | |
---|
151 | int zeroes = |
---|
152 | variables.getProperty("MAX_TASKS_NO").length() |
---|
153 | - variables.getProperty("TASKS_NO").length(); |
---|
154 | StringBuilder expPropertiesNo = new StringBuilder(); |
---|
155 | for (int j = 0; j < zeroes; j++) |
---|
156 | expPropertiesNo.append('0'); |
---|
157 | expPropertiesNo.append(variables.getProperty("TASKS_NO")); |
---|
158 | expPropertiesNo.append(".properties"); |
---|
159 | |
---|
160 | createFileFromTemplate( |
---|
161 | "properties/reservation_template.properties", variables |
---|
162 | .getProperty("EXP_PATH") |
---|
163 | + "/" + expPropertiesNo.toString(), variables); |
---|
164 | LOGGER.debug("Workload with " + Long.toString(i) |
---|
165 | + " tasks truncating finished"); |
---|
166 | } |
---|
167 | |
---|
168 | variables |
---|
169 | .setProperty("TASKS_NO", variables.getProperty("MAX_TASKS_NO")); |
---|
170 | |
---|
171 | createFileFromTemplate("properties/reservation_template.properties", |
---|
172 | variables.getProperty("EXP_PATH") + "/" |
---|
173 | + variables.getProperty("TASKS_NO") + ".properties", |
---|
174 | variables); |
---|
175 | |
---|
176 | LOGGER.info("Division of tasks finished"); |
---|
177 | |
---|
178 | return variables.getProperty("EXP_PATH"); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * It truncates workload file. |
---|
183 | * |
---|
184 | * @param numberOfExperiments |
---|
185 | * the number of experiments contained in originalWorkloadPath to |
---|
186 | * save in truncWorkloadPath |
---|
187 | * @param originalWorkloadPath |
---|
188 | * the original workload path |
---|
189 | * @param truncWorkloadPath |
---|
190 | * the truncated workload path |
---|
191 | * @throws IOException |
---|
192 | * Signals that an I/O exception has occurred. |
---|
193 | */ |
---|
194 | private void truncWorkloadFile(long numberOfExperiments, |
---|
195 | String originalWorkloadPath, String truncWorkloadPath) |
---|
196 | throws IOException { |
---|
197 | |
---|
198 | SWFParser swfParser = null; |
---|
199 | Grms3SWFJobWriter jobWriter = null; |
---|
200 | |
---|
201 | try { |
---|
202 | swfParser = new SWFParser(originalWorkloadPath); |
---|
203 | File temp = new File(truncWorkloadPath); |
---|
204 | jobWriter = new Grms3SWFJobWriter(temp.getParent(), temp.getName()); |
---|
205 | |
---|
206 | Map<String, String> header = swfParser.loadHeader(); |
---|
207 | Set<Entry<String, String>> commentsEntrySet = header.entrySet(); |
---|
208 | header.entrySet(); |
---|
209 | |
---|
210 | for (Entry<String, String> entry : commentsEntrySet) { |
---|
211 | jobWriter.writeComment(entry.getKey(), entry.getValue()); |
---|
212 | } |
---|
213 | |
---|
214 | Map<String, String[]> mapping = swfParser.getIDMapping(); |
---|
215 | String[] singleJob = null; |
---|
216 | String[] singleJobMapping = null; |
---|
217 | |
---|
218 | for (long i = 0; i < numberOfExperiments; i++) { |
---|
219 | singleJob = swfParser.readTask(); |
---|
220 | singleJobMapping = mapping.get(Long.toString(i)); |
---|
221 | |
---|
222 | jobWriter.writeLine(singleJob); |
---|
223 | jobWriter.addTaskMapping(Long.toString(i), singleJobMapping[0], |
---|
224 | singleJobMapping[1]); |
---|
225 | } |
---|
226 | |
---|
227 | } finally { |
---|
228 | if (swfParser != null) |
---|
229 | swfParser.close(); |
---|
230 | if (jobWriter != null) |
---|
231 | jobWriter.close(); |
---|
232 | } |
---|
233 | |
---|
234 | } |
---|
235 | } |
---|