Skip to content

Instantly share code, notes, and snippets.

@jcttrll
Created February 4, 2019 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcttrll/c8a4f67b22556e06636d88823b74082c to your computer and use it in GitHub Desktop.
Save jcttrll/c8a4f67b22556e06636d88823b74082c to your computer and use it in GitHub Desktop.
Quick 'n' dirty write performance test
import java.io.*;
import java.util.*;
import java.security.SecureRandom;
public class SimpleIoTest {
private static final int BLOCK_SIZE = 1024 * 1024;
private static final int TOTAL_SIZE = 1024 * 1024 * 1024;
private static final int SEQUENTIAL_ITERATIONS = 32;
private static final int RANDOM_ITERATIONS = 16;
private static final byte[] CRUMMY_RANDOM = new byte[TOTAL_SIZE];
public static void main(String[] args) throws Exception {
System.out.print("Generating random data... ");
System.out.flush();
new Random().nextBytes(CRUMMY_RANDOM);
int indexCount = CRUMMY_RANDOM.length / BLOCK_SIZE;
int[] indexes = SecureRandom.getInstance("SHA1PRNG").ints(0, indexCount).limit(indexCount).map(i -> BLOCK_SIZE * i).toArray();
System.out.println("done");
System.out.flush();
File dest = File.createTempFile("iotest-", ".tmp", new File("."));
dest.deleteOnExit();
RandomAccessFile file = new RandomAccessFile(dest, "rwd");
System.out.print("Performing sequential write... ");
System.out.flush();
long startNanos = System.nanoTime();
for (int i = 0; i < SEQUENTIAL_ITERATIONS; i++) {
file.write(CRUMMY_RANDOM);
file.seek(0);
}
long endNanos = System.nanoTime();
System.out.printf("%,.0f bytes/sec\n", (SEQUENTIAL_ITERATIONS * (double) CRUMMY_RANDOM.length) / ((endNanos - startNanos) / 1_000_000_000d));
System.out.flush();
System.out.print("Performing random-access write... ");
System.out.flush();
startNanos = System.nanoTime();
for (int iter = 0; iter < RANDOM_ITERATIONS; iter++) {
for (int i = 0; i < indexes.length; i++) {
int index = indexes[i];
file.seek(index);
file.write(CRUMMY_RANDOM, index, BLOCK_SIZE);
}
}
endNanos = System.nanoTime();
System.out.printf("%,.0f bytes/sec\n", (RANDOM_ITERATIONS * (double) CRUMMY_RANDOM.length) / ((endNanos - startNanos) / 1_000_000_000d));
System.out.flush();
}
}
@jcttrll
Copy link
Author

jcttrll commented Feb 4, 2019

This was thrown together quickly, so there may be bugs. Note that it only attempts to benchmark write performance, as read performance is more difficult due to caching.

Adjust SEQUENTIAL_ITERATIONS and RANDOM_ITERATIONS as desired.

Suggested usage:

javac SimpleIoTest.java
sudo ionice -c 1 `which java` SimpleIoTest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment