Skip to content

@rklemme /BBCompactPerfTest.java
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Microbench Test
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;
}
}
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.