Created
March 22, 2020 19:23
-
-
Save netudima/787176639eac62ae8e4ab8430f95e883 to your computer and use it in GitHub Desktop.
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
package com.github.netudima.cassandra; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.buffer.ByteBufAllocator; | |
import io.netty.buffer.PooledByteBufAllocator; | |
import io.netty.util.concurrent.DefaultThreadFactory; | |
import org.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import java.util.concurrent.ThreadPoolExecutor; | |
import java.util.concurrent.TimeUnit; | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public class NettyAllocatorBenchmark { | |
public static class HarnessExecutor extends ThreadPoolExecutor { | |
public HarnessExecutor(int maxThreads, String prefix) { | |
super(maxThreads, maxThreads, 0, TimeUnit.MILLISECONDS, | |
new LinkedBlockingQueue<>(), new DefaultThreadFactory(prefix)); | |
} | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(NettyAllocatorBenchmark.class.getSimpleName()) | |
.jvmArgs("-Xms768m", "-Xmx768m", | |
"-XX:MaxDirectMemorySize=768m", | |
"-Djmh.executor=CUSTOM", | |
"-Djmh.executor.class=com.github.netudima.cassandra.NettyAllocatorBenchmark$HarnessExecutor", | |
"-Dio.netty.leakDetectionLevel=disabled") | |
.forks(3) | |
.build(); | |
new Runner(opt).run(); | |
} | |
@State(Scope.Thread) | |
public static class MyState { | |
public int value = 1234567; | |
@Param({"0", "5", "10", "100"}) | |
public long dutyTokens; | |
} | |
@Benchmark | |
public boolean getAndRelease(MyState state) { | |
ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT; | |
ByteBuf buf = alloc.directBuffer(state.value); | |
final long tokens = state.dutyTokens; | |
if (tokens > 0) | |
Blackhole.consumeCPU(tokens); | |
return buf.release(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment