1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.examples.stress;
18
19 import java.util.List;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.io.IOException;
23 import java.io.File;
24 import java.io.PrintWriter;
25 import java.io.FileWriter;
26 import java.io.DataOutputStream;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.Future;
31
32 import org.apache.commons.rng.UniformRandomProvider;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class RandomStressTester {
54
55 private static final String C = "# ";
56
57 private static final String N = "\n";
58
59 private final List<String> cmdLine;
60
61 private final String fileOutputPrefix;
62
63
64
65
66
67
68
69 private RandomStressTester(List<String> cmd,
70 String outputPrefix) {
71 final File exec = new File(cmd.get(0));
72 if (!exec.exists() ||
73 !exec.canExecute()) {
74 throw new IllegalArgumentException("Program is not executable: " + exec);
75 }
76
77 cmdLine = new ArrayList<String>(cmd);
78 fileOutputPrefix = outputPrefix;
79
80 final File reportDir = new File(fileOutputPrefix).getParentFile();
81 if (!reportDir.exists() ||
82 !reportDir.isDirectory() ||
83 !reportDir.canWrite()) {
84 throw new IllegalArgumentException("Invalid output directory: " + reportDir);
85 }
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 public static void main(String[] args) {
108 final String output = args[0];
109 final int numThreads = Integer.parseInt(args[1]);
110
111 final Iterable<UniformRandomProvider> rngList = createGeneratorsList(args[2]);
112
113 final List<String> cmdLine = new ArrayList<String>();
114 cmdLine.addAll(Arrays.asList(Arrays.copyOfRange(args, 3, args.length)));
115
116 final RandomStressTester app = new RandomStressTester(cmdLine, output);
117
118 try {
119 app.run(rngList, numThreads);
120 } catch (IOException e) {
121 throw new RuntimeException(e);
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134 private void run(Iterable<UniformRandomProvider> generators,
135 int numConcurrentTasks)
136 throws IOException {
137
138 final ExecutorService service = Executors.newFixedThreadPool(numConcurrentTasks);
139
140
141 final List<Future<?>> execOutput = new ArrayList<Future<?>>();
142
143
144 int count = 0;
145 for (UniformRandomProvider rng : generators) {
146 final File output = new File(fileOutputPrefix + (++count));
147 final Runnable r = new Task(rng, output);
148 execOutput.add(service.submit(r));
149 }
150
151
152 try {
153 for (Future<?> f : execOutput) {
154 try {
155 f.get();
156 } catch (ExecutionException e) {
157 System.err.println(e.getCause().getMessage());
158 }
159 }
160 } catch (InterruptedException ignored) {}
161
162
163 service.shutdown();
164 }
165
166
167
168
169
170
171
172
173
174 private static Iterable<UniformRandomProvider> createGeneratorsList(String name) {
175 try {
176 return (Iterable<UniformRandomProvider>) Class.forName(name).newInstance();
177 } catch (ClassNotFoundException|
178 InstantiationException|
179 IllegalAccessException e) {
180 throw new RuntimeException(e);
181 }
182 }
183
184
185
186
187 private class Task implements Runnable {
188
189 private final File output;
190
191 private final UniformRandomProvider rng;
192
193
194
195
196
197
198
199 Task(UniformRandomProvider random,
200 File report) {
201 rng = random;
202 output = report;
203 }
204
205
206 @Override
207 public void run() {
208 try {
209
210 printHeader(output, rng);
211
212
213 final ProcessBuilder builder = new ProcessBuilder(cmdLine);
214 builder.redirectOutput(ProcessBuilder.Redirect.appendTo(output));
215 final Process testingProcess = builder.start();
216 final DataOutputStream sink = new DataOutputStream(testingProcess.getOutputStream());
217
218 final long startTime = System.nanoTime();
219
220 try {
221 while (true) {
222 sink.writeInt(rng.nextInt());
223 }
224 } catch (IOException e) {
225
226 }
227
228 final long endTime = System.nanoTime();
229
230
231 printFooter(output, endTime - startTime);
232
233 } catch (IOException e) {
234 throw new RuntimeException("Failed to start task: " + e.getMessage());
235 }
236 }
237 }
238
239
240
241
242
243
244
245
246 private void printHeader(File output,
247 UniformRandomProvider rng)
248 throws IOException {
249 final StringBuilder sb = new StringBuilder();
250 sb.append(C).append(N);
251 sb.append(C).append("RNG: ").append(rng.toString()).append(N);
252 sb.append(C).append(N);
253 sb.append(C).append("Java: ").append(System.getProperty("java.version")).append(N);
254 sb.append(C).append("Runtime: ").append(System.getProperty("java.runtime.version", "?")).append(N);
255 sb.append(C).append("JVM: ").append(System.getProperty("java.vm.name"))
256 .append(" ").append(System.getProperty("java.vm.version")).append(N);
257 sb.append(C).append("OS: ").append(System.getProperty("os.name"))
258 .append(" ").append(System.getProperty("os.version"))
259 .append(" ").append(System.getProperty("os.arch")).append(N);
260 sb.append(C).append(N);
261
262 sb.append(C).append("Analyzer: ");
263 for (String s : cmdLine) {
264 sb.append(s).append(" ");
265 }
266 sb.append(N);
267 sb.append(C).append(N);
268
269 final PrintWriter w = new PrintWriter(new FileWriter(output, true));
270 w.print(sb.toString());
271 w.close();
272 }
273
274
275
276
277
278
279
280 private void printFooter(File output,
281 long nanoTime)
282 throws IOException {
283 final PrintWriter w = new PrintWriter(new FileWriter(output, true));
284 w.println(C);
285
286 final double duration = ((double) nanoTime) * 1e-9 / 60;
287 w.println(C + "Test duration: " + duration + " minutes");
288
289 w.println(C);
290 w.close();
291 }
292 }