Created
February 4, 2019 04:04
-
-
Save jcttrll/c8a4f67b22556e06636d88823b74082c to your computer and use it in GitHub Desktop.
Quick 'n' dirty write performance test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
andRANDOM_ITERATIONS
as desired.Suggested usage: