More realistic ByteBuf allocation benchmark

Motivation:

Allocating a single buffer and releasing it repetitively for a benchmark will not involve the realistic execution path of the allocators.

Modifications:

Keep the last 8192 allocations and release them randomly.

Result:

We are now getting the result close to what we got with caliper.
This commit is contained in:
Trustin Lee 2014-05-29 19:50:43 +09:00
parent d54030266b
commit b50f91f6d0
2 changed files with 37 additions and 15 deletions

View File

@ -23,41 +23,64 @@ import io.netty.microbench.util.AbstractMicrobenchmark;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark; import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.Param; import org.openjdk.jmh.annotations.Param;
import java.util.Random;
/** /**
* This class benchmarks different allocators with different allocation sizes. * This class benchmarks different allocators with different allocation sizes.
*/ */
public class ByteBufAllocatorBenchmark extends AbstractMicrobenchmark { public class ByteBufAllocatorBenchmark extends AbstractMicrobenchmark {
private final ByteBufAllocator unpooledHeapAllocator = new UnpooledByteBufAllocator(false); private static final ByteBufAllocator unpooledAllocator = new UnpooledByteBufAllocator(true);
private final ByteBufAllocator unpooledDirectAllocator = new UnpooledByteBufAllocator(true); private static final ByteBufAllocator pooledAllocator =
private final ByteBufAllocator pooledHeapAllocator = new PooledByteBufAllocator(false); new PooledByteBufAllocator(true, 4, 4, 8192, 11, 0, 0, 0); // Disable thread-local cache
private final ByteBufAllocator pooledDirectAllocator = new PooledByteBufAllocator(true);
private static final int MAX_LIVE_BUFFERS = 8192;
private static final Random rand = new Random();
private static final ByteBuf[] unpooledHeapBuffers = new ByteBuf[MAX_LIVE_BUFFERS];
private static final ByteBuf[] unpooledDirectBuffers = new ByteBuf[MAX_LIVE_BUFFERS];
private static final ByteBuf[] pooledHeapBuffers = new ByteBuf[MAX_LIVE_BUFFERS];
private static final ByteBuf[] pooledDirectBuffers = new ByteBuf[MAX_LIVE_BUFFERS];
@Param({ "00000", "00256", "01024", "04096", "16384", "65536" }) @Param({ "00000", "00256", "01024", "04096", "16384", "65536" })
public int size; public int size;
@GenerateMicroBenchmark @GenerateMicroBenchmark
public void unpooledHeapAllocAndFree() { public void unpooledHeapAllocAndFree() {
ByteBuf buffer = unpooledHeapAllocator.buffer(size); int idx = rand.nextInt(unpooledHeapBuffers.length);
buffer.release(); ByteBuf oldBuf = unpooledHeapBuffers[idx];
if (oldBuf != null) {
oldBuf.release();
}
unpooledHeapBuffers[idx] = unpooledAllocator.heapBuffer(size);
} }
@GenerateMicroBenchmark @GenerateMicroBenchmark
public void unpooledDirectAllocAndFree() { public void unpooledDirectAllocAndFree() {
ByteBuf buffer = unpooledDirectAllocator.buffer(size); int idx = rand.nextInt(unpooledDirectBuffers.length);
buffer.release(); ByteBuf oldBuf = unpooledDirectBuffers[idx];
if (oldBuf != null) {
oldBuf.release();
}
unpooledDirectBuffers[idx] = unpooledAllocator.directBuffer(size);
} }
@GenerateMicroBenchmark @GenerateMicroBenchmark
public void pooledHeapAllocAndFree() { public void pooledHeapAllocAndFree() {
ByteBuf buffer = pooledHeapAllocator.buffer(size); int idx = rand.nextInt(pooledHeapBuffers.length);
buffer.release(); ByteBuf oldBuf = pooledHeapBuffers[idx];
if (oldBuf != null) {
oldBuf.release();
}
pooledHeapBuffers[idx] = pooledAllocator.heapBuffer(size);
} }
@GenerateMicroBenchmark @GenerateMicroBenchmark
public void pooledDirectAllocAndFree() { public void pooledDirectAllocAndFree() {
ByteBuf buffer = pooledDirectAllocator.buffer(size); int idx = rand.nextInt(pooledDirectBuffers.length);
buffer.release(); ByteBuf oldBuf = pooledDirectBuffers[idx];
if (oldBuf != null) {
oldBuf.release();
}
pooledDirectBuffers[idx] = pooledAllocator.directBuffer(size);
} }
} }

View File

@ -27,7 +27,6 @@ import org.openjdk.jmh.output.results.ResultFormatType;
import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder; import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
import org.openjdk.jmh.runner.options.OptionsBuilder; import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.VerboseMode;
import java.io.File; import java.io.File;
@ -44,7 +43,7 @@ public class AbstractMicrobenchmark {
protected static final int DEFAULT_MEASURE_ITERATIONS = 10; protected static final int DEFAULT_MEASURE_ITERATIONS = 10;
protected static final int DEFAULT_FORKS = 2; protected static final int DEFAULT_FORKS = 2;
protected static final String[] JVM_ARGS = new String[] { protected static final String[] JVM_ARGS = {
"-server", "-dsa", "-da", "-ea:io.netty...", "-Xms768m", "-Xmx768m", "-server", "-dsa", "-da", "-ea:io.netty...", "-Xms768m", "-Xmx768m",
"-XX:MaxDirectMemorySize=768m", "-XX:+AggressiveOpts", "-XX:+UseBiasedLocking", "-XX:MaxDirectMemorySize=768m", "-XX:+AggressiveOpts", "-XX:+UseBiasedLocking",
"-XX:+UseFastAccessorMethods", "-XX:+UseStringCache", "-XX:+OptimizeStringConcat", "-XX:+UseFastAccessorMethods", "-XX:+UseStringCache", "-XX:+OptimizeStringConcat",