diff --git a/microbench/src/test/java/io/netty/microbench/buffer/ByteBufAllocatorBenchmark.java b/microbench/src/test/java/io/netty/microbench/buffer/ByteBufAllocatorBenchmark.java index 5e2030aad6..aa361aff13 100644 --- a/microbench/src/test/java/io/netty/microbench/buffer/ByteBufAllocatorBenchmark.java +++ b/microbench/src/test/java/io/netty/microbench/buffer/ByteBufAllocatorBenchmark.java @@ -23,41 +23,64 @@ import io.netty.microbench.util.AbstractMicrobenchmark; import org.openjdk.jmh.annotations.GenerateMicroBenchmark; import org.openjdk.jmh.annotations.Param; +import java.util.Random; + /** * This class benchmarks different allocators with different allocation sizes. */ public class ByteBufAllocatorBenchmark extends AbstractMicrobenchmark { - private final ByteBufAllocator unpooledHeapAllocator = new UnpooledByteBufAllocator(false); - private final ByteBufAllocator unpooledDirectAllocator = new UnpooledByteBufAllocator(true); - private final ByteBufAllocator pooledHeapAllocator = new PooledByteBufAllocator(false); - private final ByteBufAllocator pooledDirectAllocator = new PooledByteBufAllocator(true); + private static final ByteBufAllocator unpooledAllocator = new UnpooledByteBufAllocator(true); + private static final ByteBufAllocator pooledAllocator = + new PooledByteBufAllocator(true, 4, 4, 8192, 11, 0, 0, 0); // Disable thread-local cache + + 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" }) public int size; @GenerateMicroBenchmark public void unpooledHeapAllocAndFree() { - ByteBuf buffer = unpooledHeapAllocator.buffer(size); - buffer.release(); + int idx = rand.nextInt(unpooledHeapBuffers.length); + ByteBuf oldBuf = unpooledHeapBuffers[idx]; + if (oldBuf != null) { + oldBuf.release(); + } + unpooledHeapBuffers[idx] = unpooledAllocator.heapBuffer(size); } @GenerateMicroBenchmark public void unpooledDirectAllocAndFree() { - ByteBuf buffer = unpooledDirectAllocator.buffer(size); - buffer.release(); + int idx = rand.nextInt(unpooledDirectBuffers.length); + ByteBuf oldBuf = unpooledDirectBuffers[idx]; + if (oldBuf != null) { + oldBuf.release(); + } + unpooledDirectBuffers[idx] = unpooledAllocator.directBuffer(size); } @GenerateMicroBenchmark public void pooledHeapAllocAndFree() { - ByteBuf buffer = pooledHeapAllocator.buffer(size); - buffer.release(); + int idx = rand.nextInt(pooledHeapBuffers.length); + ByteBuf oldBuf = pooledHeapBuffers[idx]; + if (oldBuf != null) { + oldBuf.release(); + } + pooledHeapBuffers[idx] = pooledAllocator.heapBuffer(size); } @GenerateMicroBenchmark public void pooledDirectAllocAndFree() { - ByteBuf buffer = pooledDirectAllocator.buffer(size); - buffer.release(); + int idx = rand.nextInt(pooledDirectBuffers.length); + ByteBuf oldBuf = pooledDirectBuffers[idx]; + if (oldBuf != null) { + oldBuf.release(); + } + pooledDirectBuffers[idx] = pooledAllocator.directBuffer(size); } - } diff --git a/microbench/src/test/java/io/netty/microbench/util/AbstractMicrobenchmark.java b/microbench/src/test/java/io/netty/microbench/util/AbstractMicrobenchmark.java index b2357621a8..82adf78133 100644 --- a/microbench/src/test/java/io/netty/microbench/util/AbstractMicrobenchmark.java +++ b/microbench/src/test/java/io/netty/microbench/util/AbstractMicrobenchmark.java @@ -27,7 +27,6 @@ import org.openjdk.jmh.output.results.ResultFormatType; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.options.ChainedOptionsBuilder; import org.openjdk.jmh.runner.options.OptionsBuilder; -import org.openjdk.jmh.runner.options.VerboseMode; import java.io.File; @@ -44,7 +43,7 @@ public class AbstractMicrobenchmark { protected static final int DEFAULT_MEASURE_ITERATIONS = 10; 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", "-XX:MaxDirectMemorySize=768m", "-XX:+AggressiveOpts", "-XX:+UseBiasedLocking", "-XX:+UseFastAccessorMethods", "-XX:+UseStringCache", "-XX:+OptimizeStringConcat",