This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DieharderTest.java
304 lines (270 loc) · 10.7 KB
/
DieharderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Written by Peter Levart <peter.levart@gmail.com>
* and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package si.pele.dieharder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
/**
* An interface to {@code dieharder} test tool in the form of a {@link Callable} implementation.
*/
public class DieharderTest<RNG, RNGINFO> implements Callable<DieharderTest.Results<RNGINFO>> {
final int testId;
final List<String> opts;
final RNGINFO rngInfo;
final byte[] buffer;
final RNG rng;
final BiConsumer<RNG, byte[]> bufferFiller;
public DieharderTest(int testId, List<String> opts,
RNGINFO rngInfo,
RNG rng, byte[] buffer,
BiConsumer<RNG, byte[]> bufferFiller
) {
this.testId = testId;
this.opts = opts;
this.rngInfo = rngInfo;
this.buffer = buffer;
this.rng = rng;
this.bufferFiller = bufferFiller;
}
@Override
public Results<RNGINFO> call() throws Exception {
List<String> cmd = new ArrayList<>(Arrays.asList(
"dieharder", "-g", "200", "-d", String.valueOf(testId))
);
if (opts != null) cmd.addAll(opts);
Process process = new ProcessBuilder()
.command(cmd)
.redirectInput(ProcessBuilder.Redirect.PIPE)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.start();
List<String> rawResults = new ArrayList<>(1);
OutputStream out = process.getOutputStream();
try (InputStream in = process.getInputStream()) {
for (; ; ) {
bufferFiller.accept(rng, buffer);
try {
out.write(buffer);
} catch (IOException e) {
// when dieharder exits during write,
// we get broken pipe or stream closed exception...
if (e.getMessage().equals("Broken pipe") ||
e.getMessage().equals("Stream closed")) {
break;
} else { // re-throw
throw e;
}
}
}
BufferedReader rin = new BufferedReader(new InputStreamReader(in, "ASCII"));
String line;
while ((line = rin.readLine()) != null) {
if (line.startsWith("#==")) {
rawResults.clear();
} else {
rawResults.add(line);
}
}
} finally {
try {
// sometimes when writing to pipe throws IOException
// with "Broken pipe" or "Stream closed" the stream is
// already closed, so the following close will throw
// IOException: "Already closed" and we ignore it.
out.close();
} catch (IOException ignore) {}
}
List<Result> results = new ArrayList<>(rawResults.size());
for (String rawResult : rawResults) {
String[] fields = rawResult.trim().split("\\s*\\|\\s*");
try {
if (fields.length != 6) {
throw new Exception("Invalid result from dieharder tool: " + rawResult);
}
results.add(new Result(
fields[0],
Integer.parseInt(fields[1]),
Long.parseLong(fields[2]),
Long.parseLong(fields[3]),
Double.parseDouble(fields[4]),
Assessment.valueOf(fields[5]),
rawResult
));
} catch (Exception exception) {
results.add(new Result(rawResult, exception));
}
}
return new Results<>(rngInfo, testId, results);
}
public enum Assessment {PASSED, WEAK, FAILED}
public static class Result {
public final String testName;
public final int ntup;
public final long tsamples, psamples;
public final double pvalue;
public final Assessment assessment;
public final String rawResult;
public final Exception exception;
public Result(String testName, int ntup, long tsamples, long psamples, double pvalue, Assessment assessment, String rawResult) {
this.testName = testName;
this.ntup = ntup;
this.tsamples = tsamples;
this.psamples = psamples;
this.pvalue = pvalue;
this.assessment = assessment;
this.rawResult = rawResult;
this.exception = null;
}
public Result(String rawResult, Exception exception) {
this.testName = "*invalid result*";
this.ntup = 0;
this.tsamples = 0L;
this.psamples = 0L;
this.pvalue = 0d;
this.assessment = Assessment.WEAK;
this.rawResult = rawResult;
this.exception = exception;
}
}
public static class Results<RNGINFO> {
/**
* a default format that can be used with {@link #format(String)}
*/
public static final String FORMAT =
"%3d | %20s | %2d...%2d | %8d | %8d | %10.8f...%10.8f | %s";
/**
* a header string that goes with {@link #FORMAT}
*/
public static final String HEADER = String.format(
"%3s | %20s | %7s | %8s | %8s | %23s | %s",
"Tid", "test name", "ntup", "tsamples", "psamples", "pvalue", "assessment(s)");
/**
* a header delimiter string that goes with {@link #HEADER}
*/
public static final String HEADER_DELIMITER = HEADER.replaceAll("[^\\|]", "-");
public final RNGINFO rngInfo;
public final int testId;
public final List<Result> testResults;
// aggregation
public final String testName;
public final int ntupMin, ntupMax;
public final long tsamplesAvg, psamplesAvg;
public final double pvalueMin, pvalueMax;
public final String assessments;
public final Exception exception;
// private
private final Assessment[] orderedAssessments = Assessment.values(); // ordered by decreasing count
private final int[] assHistogram = new int[orderedAssessments.length]; // indexed by Assessment.ordinal()
public Results(RNGINFO rngInfo, int testId, List<Result> testResults) {
this.rngInfo = rngInfo;
this.testId = testId;
this.testResults = Collections.unmodifiableList(testResults);
String testName = "*no results*";
int ntupMin = 0, ntupMax = 0;
long tsamplesSum = 0L, psamplesSum = 0L;
double pvalueMin = 0, pvalueMax = 0;
Exception exception = null;
int okResultCount = 0;
for (Result result : testResults) {
if (result.exception != null) {
if (exception == null) {
exception = result.exception;
} else {
exception.addSuppressed(result.exception);
}
} else {
if (okResultCount == 0) {
testName = result.testName;
ntupMin = ntupMax = result.ntup;
pvalueMin = pvalueMax = result.pvalue;
} else {
ntupMin = Math.min(ntupMin, result.ntup);
ntupMax = Math.max(ntupMax, result.ntup);
pvalueMin = Math.min(pvalueMin, result.pvalue);
pvalueMax = Math.max(pvalueMax, result.pvalue);
}
okResultCount++;
tsamplesSum += result.tsamples;
psamplesSum += result.psamples;
assHistogram[result.assessment.ordinal()]++;
}
}
this.testName = testName;
this.ntupMin = ntupMin;
this.ntupMax = ntupMax;
this.tsamplesAvg = okResultCount > 0 ? tsamplesSum / okResultCount : 0L;
this.psamplesAvg = okResultCount > 0 ? psamplesSum / okResultCount : 0L;
this.pvalueMin = pvalueMin;
this.pvalueMax = pvalueMax;
Arrays.sort(orderedAssessments, (a1, a2) -> assHistogram[a2.ordinal()] - assHistogram[a1.ordinal()]);
StringBuilder assBuilder = new StringBuilder();
for (Assessment ass : orderedAssessments) {
if (assHistogram[ass.ordinal()] > 0) {
if (assBuilder.length() > 0) assBuilder.append(", ");
assBuilder.append(assHistogram[ass.ordinal()]).append("*").append(ass);
}
}
this.assessments = assBuilder.toString();
this.exception = exception;
}
public Assessment getOrderedAssessment(int index) {
return orderedAssessments[index];
}
public Assessment[] getOrderedAssessments() {
return orderedAssessments.clone();
}
public int getAssessmentOccurrences(Assessment assessment) {
return assHistogram[assessment.ordinal()];
}
/**
* Formats according to specified format the following arguments:
* <pre>
* field name typical format specifier
*
* {@link #testId} %1$3d
* {@link #testName} %2$20s
* {@link #ntupMin} %3$2d
* {@link #ntupMax} %4$2d
* {@link #tsamplesAvg} %5$8d
* {@link #psamplesAvg} %6$8d
* {@link #pvalueMin} %7$10.8f
* {@link #pvalueMax} %8$10.8f
* {@link #assessments} %9$s
* </pre>
*
* @param format format to use
* @return formatted string
*/
public String format(String format) {
return String.format(
format,
testId, // 1$
testName, // 2$
ntupMin, // 3$
ntupMax, // 4$
tsamplesAvg,// 5$
psamplesAvg,// 6$
pvalueMin, // 7$
pvalueMax, // 8$
assessments // 9$
);
}
/**
* @return the result of {@link #format(String) format}({@link #FORMAT})
*/
@Override
public String toString() {
return format(FORMAT);
}
}
}