public
Created

Microbench Test

  • Download Gist
BBCompactPerfTest.java
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
package nio;
 
import java.nio.ByteBuffer;
import java.util.Random;
 
public final class BBCompactPerfTest {
 
private static final int SEQ = 1000000;
 
private static final int REP = 100;
 
private static final int PAGE = 0x1000;
 
private interface Compactor {
public void compact(ByteBuffer b);
}
 
private static final Compactor CN = new Compactor() {
 
@Override
public void compact(ByteBuffer b) {
b.compact();
}
 
};
 
private static final Compactor CO = new Compactor() {
 
@Override
public void compact(ByteBuffer b) {
if (b.hasRemaining()) {
b.compact();
} else {
b.clear();
}
}
};
 
public static void main(String[] args) {
System.out.println("Start");
final int[] writes = calculateSequence();
 
for (int i = 0; i < 10; ++i) {
System.out.printf("\nRun: %3d\n\n", i);
testRun(writes);
}
}
 
private static void testRun(final int[] writes) {
final long t1 = System.nanoTime();
long w1 = -1;
 
for (int i = 0; i < REP; ++i) {
final long w = test(writes, CN);
 
if (w1 == -1) {
w1 = w;
} else if (w1 != w) {
throw new IllegalStateException();
}
}
 
final long t2 = System.nanoTime();
long w2 = -1;
 
for (int i = 0; i < REP; ++i) {
final long w = test(writes, CO);
 
if (w2 == -1) {
w2 = w;
} else if (w2 != w) {
throw new IllegalStateException();
}
}
 
final long t3 = System.nanoTime();
 
System.out.printf("%-20s: %20dns\n", "norm", t2 - t1);
System.out.printf("%-20s: %20dns\n", "opt", t3 - t2);
System.out.printf("%-20s: %30.20f\n", "Factor norm/opt",
(double) (t2 - t1) / (t3 - t2));
}
 
private static long test(int[] writes, Compactor cn) {
final ByteBuffer b = ByteBuffer.allocate(PAGE);
long written = 0;
 
for (int i = 0; i < writes.length; ++i) {
if (empty(b)) {
// fill
b.position(b.capacity());
}
 
// write
b.flip();
final int toWrite = Math.min(b.remaining(), writes[i]);
written += toWrite;
b.position(b.position() + toWrite);
cn.compact(b);
}
 
return written;
}
 
private static boolean empty(ByteBuffer b) {
return b.remaining() == b.capacity();
}
 
private static int[] calculateSequence() {
final Random r = new Random();
final int[] seq = new int[SEQ];
 
for (int i = 0; i < seq.length; ++i) {
seq[i] = r.nextInt(PAGE);
}
 
return seq;
}
 
}
output.txt
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
Start
 
Run: 0
 
norm : 21067688894ns
opt : 20590065017ns
Factor norm/opt : 1,02319681247269760000
 
Run: 1
 
norm : 21551762554ns
opt : 20308660554ns
Factor norm/opt : 1,06121043762067100000
 
Run: 2
 
norm : 21176808432ns
opt : 20648191012ns
Factor norm/opt : 1,02560114925771400000
 
Run: 3
 
norm : 20391160633ns
opt : 20600000096ns
Factor norm/opt : 0,98986216203753550000
 
Run: 4
 
norm : 21009198646ns
opt : 20302975344ns
Factor norm/opt : 1,03478422694379660000
 
Run: 5
 
norm : 20883764704ns
opt : 20858252446ns
Factor norm/opt : 1,00122312538243800000
 
Run: 6
 
norm : 21198802681ns
opt : 20998366687ns
Factor norm/opt : 1,00954531354689080000
 
Run: 7
 
norm : 20540393084ns
opt : 20865745400ns
Factor norm/opt : 0,98440734755634460000
 
Run: 8
 
norm : 21167540283ns
opt : 20547912503ns
Factor norm/opt : 1,03015526661939670000
 
Run: 9
 
norm : 21480784827ns
opt : 20890389695ns
Factor norm/opt : 1,02826156623307540000

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.