Skip to content

Commit 3b77a71

Browse files
daschlNorman Maurer
authored andcommitted
Make JMH options modifiable through the subclassed benchmark.
1 parent 29484a4 commit 3b77a71

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

microbench/src/test/java/io/netty/microbench/buffer/ByteBufAllocatorBenchmark.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@
2121
import io.netty.buffer.UnpooledByteBufAllocator;
2222
import io.netty.microbench.util.AbstractMicrobenchmark;
2323
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
24-
import org.openjdk.jmh.annotations.Scope;
25-
import org.openjdk.jmh.annotations.State;
2624

2725
/**
2826
* This class benchmarks different allocators with different allocation sizes.
2927
*/
30-
@State(Scope.Thread)
3128
public class ByteBufAllocatorBenchmark extends AbstractMicrobenchmark {
3229

3330
private final ByteBufAllocator unpooledHeapAllocator = new UnpooledByteBufAllocator(false);

microbench/src/test/java/io/netty/microbench/util/AbstractMicrobenchmark.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,30 @@
1717

1818
import io.netty.util.ResourceLeakDetector;
1919
import org.junit.Test;
20-
import org.openjdk.jmh.output.OutputFormatType;
20+
import org.openjdk.jmh.annotations.Fork;
21+
import org.openjdk.jmh.annotations.Measurement;
22+
import org.openjdk.jmh.annotations.Scope;
23+
import org.openjdk.jmh.annotations.State;
24+
import org.openjdk.jmh.annotations.Warmup;
2125
import org.openjdk.jmh.output.results.ResultFormatType;
2226
import org.openjdk.jmh.runner.Runner;
2327
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
24-
import org.openjdk.jmh.runner.options.Options;
2528
import org.openjdk.jmh.runner.options.OptionsBuilder;
2629

2730
import java.io.File;
2831

2932
/**
3033
* Base class for all JMH benchmarks.
3134
*/
35+
@Warmup(iterations = AbstractMicrobenchmark.DEFAULT_WARMUP_ITERATIONS)
36+
@Measurement(iterations = AbstractMicrobenchmark.DEFAULT_MEASURE_ITERATIONS)
37+
@Fork(AbstractMicrobenchmark.DEFAULT_FORKS)
38+
@State(Scope.Thread)
3239
public class AbstractMicrobenchmark {
3340

34-
protected static final String WARMUP_ITERATIONS = "10";
35-
protected static final String MEASURE_ITERATIONS = "10";
36-
protected static final String NUM_FORKS = "2";
41+
protected static final int DEFAULT_WARMUP_ITERATIONS = 10;
42+
protected static final int DEFAULT_MEASURE_ITERATIONS = 1;
43+
protected static final int DEFAULT_FORKS = 2;
3744

3845
protected static final String JVM_ARGS = "-server -dsa -da -ea:io.netty... -Xms768m" +
3946
" -Xmx768m -XX:MaxDirectMemorySize=768m -XX:+AggressiveOpts -XX:+UseBiasedLocking" +
@@ -50,10 +57,19 @@ public void run() throws Exception {
5057

5158
ChainedOptionsBuilder runnerOptions = new OptionsBuilder()
5259
.include(".*" + className + ".*")
53-
.jvmArgs(JVM_ARGS)
54-
.warmupIterations(getWarmupIterations())
55-
.measurementIterations(getMeasureIterations())
56-
.forks(getForks());
60+
.jvmArgs(JVM_ARGS);
61+
62+
if (getWarmupIterations() > 0) {
63+
runnerOptions.warmupIterations(getWarmupIterations());
64+
}
65+
66+
if (getMeasureIterations() > 0) {
67+
runnerOptions.measurementIterations(getMeasureIterations());
68+
}
69+
70+
if (getForks() > 0) {
71+
runnerOptions.forks(getForks());
72+
}
5773

5874
if (getReportDir() != null) {
5975
String filePath = getReportDir() + className + ".json";
@@ -73,15 +89,15 @@ public void run() throws Exception {
7389
}
7490

7591
protected int getWarmupIterations() {
76-
return Integer.parseInt(System.getProperty("warmupIterations", WARMUP_ITERATIONS));
92+
return Integer.parseInt(System.getProperty("warmupIterations", "-1"));
7793
}
7894

7995
protected int getMeasureIterations() {
80-
return Integer.parseInt(System.getProperty("measureIterations", MEASURE_ITERATIONS));
96+
return Integer.parseInt(System.getProperty("measureIterations", "-1"));
8197
}
8298

8399
protected int getForks() {
84-
return Integer.parseInt(System.getProperty("forks", NUM_FORKS));
100+
return Integer.parseInt(System.getProperty("forks", "-1"));
85101
}
86102

87103
protected String getReportDir() {

0 commit comments

Comments
 (0)