in src/main/java/com/uber/rss/tools/FsyncPerfTest.java [91:148]
private void run() {
Arrays.stream(fileStreams).parallel().forEach(stream -> {
long writeBytes = 0;
for (int i = 0; i < numWrites; i++) {
int valueIndex = random.nextInt(testValues.length);
byte[] bytes = testValues[valueIndex];
try {
totalOperationCount.incrementAndGet();
writeBytes += bytes.length;
long startTime = System.nanoTime();
stream.write(bytes);
totalWriteTime.addAndGet(System.nanoTime() - startTime);
startTime = System.nanoTime();
stream.flush();
totalFlushTime.addAndGet(System.nanoTime() - startTime);
startTime = System.nanoTime();
stream.getFD().sync();
long fsyncDuration = System.nanoTime() - startTime;
totalFsyncTime.addAndGet(fsyncDuration);
fsyncMillisValues[fsyncMillisValuesIndex.getAndIncrement()] = TimeUnit.NANOSECONDS.toMillis(fsyncDuration);
} catch (IOException e) {
throw new RuntimeException("Failed to write file", e);
}
}
logger.info(String.format("Finished writing file: %s bytes", writeBytes));
});
logger.info(String.format("Total operation: %s, total write seconds: %s, flush: %s, fsync: %s, average write milliseconds: %s, flush: %s, fsync: %s",
totalOperationCount.get(),
TimeUnit.NANOSECONDS.toSeconds(totalWriteTime.get()),
TimeUnit.NANOSECONDS.toSeconds(totalFlushTime.get()),
TimeUnit.NANOSECONDS.toSeconds(totalFsyncTime.get()),
TimeUnit.NANOSECONDS.toMillis(totalWriteTime.get()/totalOperationCount.get()),
TimeUnit.NANOSECONDS.toMillis(totalFlushTime.get()/totalOperationCount.get()),
TimeUnit.NANOSECONDS.toMillis(totalFsyncTime.get()/totalOperationCount.get())));
EmpiricalDistribution fsyncMillisDistribution = new EmpiricalDistribution();
fsyncMillisDistribution.load(fsyncMillisValues);
DescriptiveStatistics stats = new DescriptiveStatistics();
for (double v: fsyncMillisValues) {
stats.addValue(v);
}
stats.getPercentile(50);
logger.info(String.format("fsync duration (milliseconds): P50: %s, P95: %s, P99: %s, max: %s",
stats.getPercentile(50),
stats.getPercentile(95),
stats.getPercentile(99),
stats.getMax()));
}