1 | package test.stress; |
---|
2 | |
---|
3 | import java.io.FileNotFoundException; |
---|
4 | import java.io.IOException; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.HashMap; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Map; |
---|
9 | |
---|
10 | import org.apache.commons.logging.Log; |
---|
11 | import org.apache.commons.logging.LogFactory; |
---|
12 | |
---|
13 | /** |
---|
14 | * The class responsible for generate tasks or resource range experiments and |
---|
15 | * running memory and time analysis on them. |
---|
16 | * |
---|
17 | * @author Jarek Szymczak |
---|
18 | * @version $Id$ |
---|
19 | */ |
---|
20 | public class GeneratingAndTestingManager { |
---|
21 | |
---|
22 | /** Class logger. */ |
---|
23 | private static final Log LOGGER = |
---|
24 | LogFactory.getLog(GeneratingAndTestingManager.class); |
---|
25 | |
---|
26 | /** |
---|
27 | * The file paths of generated experiments passed to memory and time |
---|
28 | * analysis. |
---|
29 | */ |
---|
30 | private static List<String> parameters = new ArrayList<String>(); |
---|
31 | |
---|
32 | /** List of paramters (after they're parsed from main method arguments). */ |
---|
33 | private static Map<String, String[]> methodParameters = |
---|
34 | new HashMap<String, String[]>(); |
---|
35 | |
---|
36 | /** |
---|
37 | * It determines whether memory and time analysis should be performed after |
---|
38 | * experiments generation. |
---|
39 | */ |
---|
40 | private static boolean run; |
---|
41 | |
---|
42 | /** |
---|
43 | * The main method. Apart arguments the program is run with, generation of |
---|
44 | * experiments depends on file "properties/variables.properties" and |
---|
45 | * "*properties/template*.*" files |
---|
46 | * |
---|
47 | * @param args |
---|
48 | * the following arguments (other are not allowed - arguments |
---|
49 | * predecessed by "#" are integers): |
---|
50 | * <ul> |
---|
51 | * <li><b>-r</b> - memory and time analysis will be performed |
---|
52 | * after generation (if specified) |
---|
53 | * <li><b>--rg #tasks #start #step #end</b> - experiments with |
---|
54 | * #tasks number of tasks will be generated for range of |
---|
55 | * resources from #start to #end with step #step |
---|
56 | * <li><b>--rv #tasks #resources_1 ... #resources_n</b> - |
---|
57 | * experiments with #tasks number of tasks will be generated for |
---|
58 | * #resources_1, ..., #resources_n number of resources |
---|
59 | * <li><b>--tg #start #step #end</b> - experiments with tasks |
---|
60 | * range from #start to #end with step #step will be generated |
---|
61 | * <li><b>--tv #tasks_1 ... #tasks_n</b> - experiments with |
---|
62 | * #tasks_1, ..., #tasks_n number of tasks will be generated |
---|
63 | * </ul> |
---|
64 | * |
---|
65 | * @throws FileNotFoundException |
---|
66 | * if one of necessary files was not found |
---|
67 | * @throws IOException |
---|
68 | * Signals that an I/O exception has occurred. |
---|
69 | * @throws InterruptedException |
---|
70 | * should not occur |
---|
71 | */ |
---|
72 | public static void main(String args[]) throws FileNotFoundException, |
---|
73 | IOException, InterruptedException { |
---|
74 | |
---|
75 | String argsProcessed[] = args; |
---|
76 | |
---|
77 | while (argsProcessed.length != 0) { |
---|
78 | argsProcessed = processArguments(argsProcessed); |
---|
79 | } |
---|
80 | |
---|
81 | if (methodParameters.containsKey("tg") |
---|
82 | && methodParameters.containsKey("tv")) { |
---|
83 | System.out |
---|
84 | .println("Argumets --tg and --tv cannot be used together"); |
---|
85 | return; |
---|
86 | } |
---|
87 | |
---|
88 | if (methodParameters.containsKey("rg") |
---|
89 | && methodParameters.containsKey("rv")) { |
---|
90 | System.out |
---|
91 | .println("Argumets --rg and --rv cannot be used together"); |
---|
92 | return; |
---|
93 | } |
---|
94 | |
---|
95 | if (methodParameters.containsKey("tg") |
---|
96 | && (methodParameters.get("tg").length != 4)) { |
---|
97 | System.out.println("Wrong usage of --tg argument"); |
---|
98 | return; |
---|
99 | } |
---|
100 | |
---|
101 | if (methodParameters.containsKey("rg") |
---|
102 | && (methodParameters.get("rg").length != 5)) { |
---|
103 | System.out.println("Wrong usage of --rg argument"); |
---|
104 | return; |
---|
105 | } |
---|
106 | |
---|
107 | if (methodParameters.containsKey("tv") |
---|
108 | && (methodParameters.get("tv").length < 2)) { |
---|
109 | System.out.println("Wrong usage of --tg argument"); |
---|
110 | return; |
---|
111 | } |
---|
112 | |
---|
113 | if (methodParameters.containsKey("rv") |
---|
114 | && (methodParameters.get("rv").length < 3)) { |
---|
115 | System.out.println("Wrong usage of --rg argument"); |
---|
116 | return; |
---|
117 | } |
---|
118 | |
---|
119 | if (methodParameters.containsKey("tg") |
---|
120 | && methodParameters.containsKey("tv")) { |
---|
121 | System.out |
---|
122 | .println("Argumets --tg and --tv cannot be used together"); |
---|
123 | return; |
---|
124 | } |
---|
125 | |
---|
126 | if (methodParameters.containsKey("tg")) { |
---|
127 | TasksExperimentsGenerator teg = new TasksExperimentsGenerator(); |
---|
128 | parameters.add(teg.run(methodParameters.get("tg"))); |
---|
129 | } |
---|
130 | |
---|
131 | if (methodParameters.containsKey("tv")) { |
---|
132 | TasksExperimentsGenerator teg = new TasksExperimentsGenerator(); |
---|
133 | parameters.add(teg.run(methodParameters.get("tv"))); |
---|
134 | } |
---|
135 | |
---|
136 | if (methodParameters.containsKey("rg")) { |
---|
137 | ResourcesExperimentsGenerator reg = |
---|
138 | new ResourcesExperimentsGenerator(); |
---|
139 | parameters.add(reg.run(methodParameters.get("rg"))); |
---|
140 | } |
---|
141 | |
---|
142 | if (methodParameters.containsKey("rv")) { |
---|
143 | ResourcesExperimentsGenerator reg = |
---|
144 | new ResourcesExperimentsGenerator(); |
---|
145 | parameters.add(reg.run(methodParameters.get("rv"))); |
---|
146 | } |
---|
147 | |
---|
148 | if (run) { |
---|
149 | LOGGER |
---|
150 | .info("Memory and time consumption analysis will begin in 3 seconds, please wait..."); |
---|
151 | |
---|
152 | for (int i = 0; i < 5; i++) { |
---|
153 | System.gc(); |
---|
154 | Thread.sleep(600); |
---|
155 | } |
---|
156 | |
---|
157 | MemoryAndTimeAnalyzer.main(parameters.toArray(new String[parameters |
---|
158 | .size()])); |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * It processes the arguments. It moves arguments with it's parameters from |
---|
165 | * an args array (they're removed) to methodParameters map. |
---|
166 | * |
---|
167 | * @param args |
---|
168 | * array with (possibly) many groups of parameters (arguments) |
---|
169 | * @return separated group of parameters |
---|
170 | */ |
---|
171 | private static String[] processArguments(String args[]) { |
---|
172 | int offset = 0; |
---|
173 | |
---|
174 | if ("-r".equals(args[0])) { |
---|
175 | run = true; |
---|
176 | offset = 1; |
---|
177 | |
---|
178 | } else { |
---|
179 | |
---|
180 | do { |
---|
181 | offset++; |
---|
182 | } while ((offset < args.length) && !args[offset].startsWith("-")); |
---|
183 | |
---|
184 | if ("--rv".equals(args[0])) { |
---|
185 | |
---|
186 | String[] rvArgs = arrayCopyOfRange(args, 0, offset); |
---|
187 | rvArgs[0] = "-v"; |
---|
188 | methodParameters.put("rv", rvArgs); |
---|
189 | |
---|
190 | } else if ("--rg".equals(args[0])) { |
---|
191 | String[] rvArgs = arrayCopyOfRange(args, 0, offset); |
---|
192 | rvArgs[0] = "-g"; |
---|
193 | methodParameters.put("rg", rvArgs); |
---|
194 | |
---|
195 | } else if ("--tv".equals(args[0])) { |
---|
196 | String[] rvArgs = arrayCopyOfRange(args, 0, offset); |
---|
197 | rvArgs[0] = "-v"; |
---|
198 | methodParameters.put("tv", rvArgs); |
---|
199 | |
---|
200 | } else if ("--tg".equals(args[0])) { |
---|
201 | String[] rvArgs = arrayCopyOfRange(args, 0, offset); |
---|
202 | rvArgs[0] = "-g"; |
---|
203 | methodParameters.put("tg", rvArgs); |
---|
204 | |
---|
205 | } else { |
---|
206 | System.out.println("Incorrect switch (" + args[0] |
---|
207 | + "). Supported are:"); |
---|
208 | System.out |
---|
209 | .println("-r\t\tif specified after generation tests will be performed"); |
---|
210 | System.out |
---|
211 | .println("--rv\t\tif resources experiment generation from certain values"); |
---|
212 | System.out |
---|
213 | .println("--rg\t\tif resources experiment generation"); |
---|
214 | System.out |
---|
215 | .println("--tv\t\tif tasks experiment generation from certain values"); |
---|
216 | System.out.println("--tg\t\tif tasks experiment generation"); |
---|
217 | |
---|
218 | System.exit(-1); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | return arrayCopyOfRange(args, offset, args.length); |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * Copy of array's range. Method written as a wrapper to System method |
---|
227 | * arraycopy(). |
---|
228 | * |
---|
229 | * @param original |
---|
230 | * the original array |
---|
231 | * @param from |
---|
232 | * from index |
---|
233 | * @param to |
---|
234 | * to index |
---|
235 | * @return truncated array |
---|
236 | */ |
---|
237 | private static String[] arrayCopyOfRange(String[] original, int from, int to) { |
---|
238 | String[] result = new String[to - from]; |
---|
239 | System.arraycopy(original, from, result, 0, to - from); |
---|
240 | return result; |
---|
241 | } |
---|
242 | } |
---|