1 | package test.stress; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.File; |
---|
5 | import java.io.FileInputStream; |
---|
6 | import java.io.FileNotFoundException; |
---|
7 | import java.io.FileReader; |
---|
8 | import java.io.IOException; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import org.apache.commons.logging.Log; |
---|
12 | import org.apache.commons.logging.LogFactory; |
---|
13 | |
---|
14 | import simulator.workload.generator.WorkloadGenerator; |
---|
15 | |
---|
16 | /** |
---|
17 | * Class responsible for generating experiments based on amount and range of |
---|
18 | * resources and a certain amount of tasks. |
---|
19 | * |
---|
20 | * @author Jarek Szymczak |
---|
21 | * @version $Id$ |
---|
22 | */ |
---|
23 | public class ResourcesExperimentsGenerator extends AbstractExperimentsGenerator { |
---|
24 | |
---|
25 | /** Class logger. */ |
---|
26 | private static final Log LOGGER = |
---|
27 | LogFactory.getLog(ResourcesExperimentsGenerator.class); |
---|
28 | |
---|
29 | /** |
---|
30 | * It runs the resource range / values based generation. |
---|
31 | * |
---|
32 | * @param args |
---|
33 | * the following arguments (other are not allowed - arguments |
---|
34 | * predecessed by "#" are integers): |
---|
35 | * <ul> |
---|
36 | * <li><b>-g #tasks #start #step #end</b> - experiments with |
---|
37 | * #tasks number of tasks will be generated for range of |
---|
38 | * resources from #start to #end with step #step |
---|
39 | * <li><b>-v #tasks #resources_1 ... #resources_n</b> - |
---|
40 | * experiments with #tasks number of tasks will be generated for |
---|
41 | * #resources_1, ..., #resources_n number of resources |
---|
42 | * </ul> |
---|
43 | * @return path to directory containing generated experiments |
---|
44 | */ |
---|
45 | public String run(String args[]) { |
---|
46 | |
---|
47 | long max = 0; |
---|
48 | long tasks = 0; |
---|
49 | long[] values = null; |
---|
50 | |
---|
51 | if (args[0].equals("-g")) { |
---|
52 | |
---|
53 | tasks = Long.parseLong(args[1]); |
---|
54 | long start = Long.parseLong(args[2]); |
---|
55 | long step = Long.parseLong(args[3]); |
---|
56 | max = Long.parseLong(args[4]); |
---|
57 | |
---|
58 | values = |
---|
59 | new long[(int) Math.ceil((double) (max - start) |
---|
60 | / (double) step) + 1]; |
---|
61 | for (long j = start; j <= max; j += step) { |
---|
62 | values[(int) ((j - start) / step)] = j; |
---|
63 | } |
---|
64 | } else if (args[0].equals("-v")) { |
---|
65 | tasks = Long.parseLong(args[1]); |
---|
66 | values = new long[args.length - 2]; |
---|
67 | max = Long.parseLong(args[args.length - 1]); |
---|
68 | for (int i = 2; i < args.length; i++) { |
---|
69 | values[i - 2] = Long.parseLong(args[i]); |
---|
70 | } |
---|
71 | } else { |
---|
72 | throw new RuntimeException("Class must be launched with -v or -g"); |
---|
73 | } |
---|
74 | |
---|
75 | LOGGER |
---|
76 | .info("Generating workload from range (for resource experiments): " |
---|
77 | + Long.toString(values[0]) + "-" + Long.toString(max)); |
---|
78 | |
---|
79 | Properties variables = new Properties(); |
---|
80 | FileInputStream fis = null; |
---|
81 | try { |
---|
82 | fis = |
---|
83 | new FileInputStream(new File( |
---|
84 | "properties/variables.properties")); |
---|
85 | variables.load(fis); |
---|
86 | } catch (FileNotFoundException e) { |
---|
87 | LOGGER |
---|
88 | .error("Could not find variables.properties file, application will terminate!"); |
---|
89 | throw new RuntimeException(e); |
---|
90 | } catch (IOException e) { |
---|
91 | LOGGER |
---|
92 | .error("Problems with I/O operations while trying to load variables.properties file, application will terminate!"); |
---|
93 | throw new RuntimeException(e); |
---|
94 | } finally { |
---|
95 | try { |
---|
96 | if (fis != null) |
---|
97 | fis.close(); |
---|
98 | } catch (IOException e) { |
---|
99 | LOGGER |
---|
100 | .error("An error occured while closing the stream with variables"); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | variables |
---|
105 | .setProperty("EXP_PATH", variables.getProperty("RES_EXP_PATH")); |
---|
106 | |
---|
107 | variables.setProperty("MAX_RES_NO", Long.toString(max)); |
---|
108 | |
---|
109 | variables.setProperty("MAX_TASKS_NO", Long.toString(tasks)); |
---|
110 | |
---|
111 | try { |
---|
112 | createFileFromTemplate("properties/generator_template.properties", |
---|
113 | "properties/generator.properties", variables); |
---|
114 | createFileFromTemplate("properties/generator_template.xml", |
---|
115 | "properties/generator.xml", variables); |
---|
116 | } catch (IOException e) { |
---|
117 | LOGGER |
---|
118 | .error("Error while trying to generate files needed for generation of tasks, applcation will terminate!"); |
---|
119 | throw new RuntimeException(e); |
---|
120 | } |
---|
121 | |
---|
122 | deleteDirectory(new File(variables.getProperty("EXP_PATH"))); |
---|
123 | |
---|
124 | File dir = |
---|
125 | new File(variables.getProperty("EXP_PATH") + "/" |
---|
126 | + variables.getProperty("MAX_TASKS_NO")); |
---|
127 | |
---|
128 | if (!dir.exists()) { |
---|
129 | try { |
---|
130 | dir.mkdirs(); |
---|
131 | } catch (SecurityException e) { |
---|
132 | LOGGER |
---|
133 | .error("Security violation while trying to create directories: " |
---|
134 | + dir.getAbsolutePath() + "!"); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | String parameters[] = { "properties/generator.properties" }; |
---|
139 | WorkloadGenerator.main(parameters); |
---|
140 | |
---|
141 | LOGGER.info("Dividing max number of tasks among other values"); |
---|
142 | |
---|
143 | variables |
---|
144 | .setProperty("TASKS_NO", variables.getProperty("MAX_TASKS_NO")); |
---|
145 | |
---|
146 | for (long i : values) { |
---|
147 | |
---|
148 | variables.setProperty("RES_NO", Long.toString(i)); |
---|
149 | |
---|
150 | variables.setProperty("RESOURCES_PATH", variables |
---|
151 | .getProperty("EXP_PATH") |
---|
152 | + "/AResources128_" + Long.toString(i) + ".xml"); |
---|
153 | |
---|
154 | int zeroes = |
---|
155 | variables.getProperty("MAX_RES_NO").length() |
---|
156 | - variables.getProperty("RES_NO").length(); |
---|
157 | StringBuilder expPropertiesNo = |
---|
158 | new StringBuilder(variables.getProperty("TASKS_NO")); |
---|
159 | expPropertiesNo.append("R"); |
---|
160 | for (int j = 0; j < zeroes; j++) |
---|
161 | expPropertiesNo.append('0'); |
---|
162 | expPropertiesNo.append(variables.getProperty("RES_NO")); |
---|
163 | expPropertiesNo.append(".properties"); |
---|
164 | |
---|
165 | try { |
---|
166 | |
---|
167 | createResourcesFromSingleTemplate(i, |
---|
168 | "properties/resource_template.part", |
---|
169 | "properties/resources_template.xml", variables); |
---|
170 | |
---|
171 | createFileFromTemplate( |
---|
172 | "properties/reservation_template.properties", variables |
---|
173 | .getProperty("EXP_PATH") |
---|
174 | + "/" + expPropertiesNo.toString(), variables); |
---|
175 | } catch (IOException e) { |
---|
176 | LOGGER.error("An exception occured while trying to "); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | return variables.getProperty("EXP_PATH"); |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * Generates the resource file (it's directory is specified in variables and |
---|
185 | * file name depends on amount of resources it contains) from general and |
---|
186 | * single templates. It's based on variables replacement mechanism in method |
---|
187 | * |
---|
188 | * @param numberOfResources |
---|
189 | * the number of resources the |
---|
190 | * @param templatePath |
---|
191 | * the template path |
---|
192 | * @param generalTemplatePath |
---|
193 | * the general template path |
---|
194 | * @param variables |
---|
195 | * the variables for resources generation |
---|
196 | * @throws IOException |
---|
197 | * Signals that an I/O exception has occurred. |
---|
198 | * {@link #substituteVariables(String, Properties)}. After |
---|
199 | * preparation of variables it calls method: |
---|
200 | * {@link #createFileFromTemplate(String, String, Properties)} |
---|
201 | */ |
---|
202 | private void createResourcesFromSingleTemplate(long numberOfResources, |
---|
203 | String templatePath, String generalTemplatePath, |
---|
204 | Properties variables) throws IOException { |
---|
205 | |
---|
206 | File template = new File(templatePath); |
---|
207 | |
---|
208 | BufferedReader reader = new BufferedReader(new FileReader(template)); |
---|
209 | String line = ""; |
---|
210 | StringBuilder oldtext = new StringBuilder(); |
---|
211 | try { |
---|
212 | while ((line = reader.readLine()) != null) { |
---|
213 | oldtext.append(line); |
---|
214 | oldtext.append('\n'); |
---|
215 | } |
---|
216 | } finally { |
---|
217 | if (reader != null) |
---|
218 | reader.close(); |
---|
219 | } |
---|
220 | |
---|
221 | StringBuilder newtext = new StringBuilder(); |
---|
222 | |
---|
223 | for (long i = 0; i < numberOfResources; i++) { |
---|
224 | variables.setProperty("RESOURCE_ID", "compRes" + Long.toString(i)); |
---|
225 | newtext.append(substituteVariables(oldtext.toString(), variables)); |
---|
226 | } |
---|
227 | |
---|
228 | variables.setProperty("RESOURCES_DETAILS", newtext.toString()); |
---|
229 | |
---|
230 | createFileFromTemplate(generalTemplatePath, variables |
---|
231 | .getProperty("RESOURCES_PATH"), variables); |
---|
232 | } |
---|
233 | } |
---|