Skip to content

Commit b2066fc

Browse files
committed
JMH integration fixes
Signed-off-by: Luis Pinto <luis.pinto@consensys.net>
1 parent e40d796 commit b2066fc

File tree

5 files changed

+63
-93
lines changed

5 files changed

+63
-93
lines changed

build.gradle

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ plugins {
3636
id 'se.patrikerdes.use-latest-versions' version '0.2.18'
3737
id 'com.github.ben-manes.versions' version '0.49.0'
3838
id 'org.jreleaser' version '1.16.0'
39+
id 'me.champeau.jmh' version '0.7.3' apply false
3940
}
4041

4142
description = 'A set of libraries and other tools to aid development of blockchain and other decentralized software in Java and other JVM languages'
@@ -124,6 +125,39 @@ subprojects {
124125
}
125126
}
126127

128+
if (file('src/jmh').directory) {
129+
apply plugin: 'me.champeau.jmh'
130+
tasks.named("jmh") {
131+
description = "Usage: gradle jmh -Pincludes=MyBench -PasyncProfiler=<libPath> -PasyncProfilerOptions=<options>\n" +
132+
"\nRun JMH benchmarks in each of the projects. Allows for controlling JMH execution directly from the command line.\n" +
133+
"\t-Pincludes=<includePattern>\tInclude pattern (regular expression) for benchmarks to be executed. Defaults to including all benchmarks.\n" +
134+
"\t-PasyncProfiler=<libPath>\tLibrary path to fetch the Async profiler from. Default is to disable profiling.\n" +
135+
"\t-PasyncProfilerOptions=<asyncProfilerOptions>\tOptions to pass on to the Async profiler separated by ';'. Default is to produce a flamegraph with all other default profiler options.\n"
136+
}
137+
138+
// to pass compilation as the compiler doesn't like what jmh tool is doing
139+
compileJmhJava {
140+
options.compilerArgs << '-Xlint:none'
141+
}
142+
143+
jmh {
144+
jmhVersion = '1.37'
145+
fork = 3
146+
includes = _strListCmdArg('includes', [''])
147+
var asyncProfiler = _strCmdArg('asyncProfiler')
148+
var asyncProfilerOptions = _strCmdArg('asyncProfilerOptions', 'output=flamegraph')
149+
if (asyncProfiler != null) {
150+
profilers = ['async:libPath=' + asyncProfiler + ';' + asyncProfilerOptions]
151+
}
152+
duplicateClassesStrategy = DuplicatesStrategy.WARN
153+
jvmArgs = ['-XX:+EnableDynamicAgentLoading']
154+
}
155+
156+
dependencies {
157+
jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.37'
158+
}
159+
}
160+
127161
plugins.withId('java', { _ ->
128162
sourceSets {
129163
integrationTest {
@@ -565,3 +599,18 @@ tasks.register('checkNotice') {
565599
}
566600
throw new GradleException('NOTICE file is not up-to-date')
567601
}
602+
603+
def _strListCmdArg(name, defaultValue) {
604+
if (!project.hasProperty(name))
605+
return defaultValue
606+
607+
return ((String) project.property(name)).tokenize(',')
608+
}
609+
610+
def _strCmdArg(name) {
611+
return _strCmdArg(name, null)
612+
}
613+
614+
def _strCmdArg(name, defaultValue) {
615+
return project.hasProperty(name) ? project.property(name) as String : defaultValue
616+
}

bytes/build.gradle

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
1111
* specific language governing permissions and limitations under the License.
1212
*/
13-
plugins {
14-
id 'me.champeau.jmh' version '0.7.2'
15-
}
16-
1713
description = 'Classes and utilities for working with byte arrays.'
1814

1915
dependencies {
@@ -28,30 +24,6 @@ dependencies {
2824
testImplementation 'org.junit.jupiter:junit-jupiter-params'
2925
testImplementation 'org.mockito:mockito-junit-jupiter'
3026
testImplementation 'org.assertj:assertj-core'
31-
jmhImplementation 'tools.profiler:async-profiler:3.0'
3227

3328
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
34-
35-
jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.37'
36-
}
37-
38-
// to pass compilation as the compiler doesn't like what jmh tool is doing
39-
compileJmhJava {
40-
options.compilerArgs << '-Xlint:none'
41-
}
42-
43-
jmh {
44-
jmhVersion = '1.37'
45-
if (project.hasProperty('jmh.includes')) {
46-
includes = [project.property('jmh.includes')]
47-
}
48-
49-
jvmArgs = ['-XX:LoopUnrollLimit=1',
50-
'-XX:-TieredCompilation',
51-
'-XX:+UnlockDiagnosticVMOptions',
52-
// '-XX:+TraceDeoptimization',
53-
// '-XX:+PrintCompilation',
54-
// '-XX:+PrintInlining',
55-
// '-XX:-Inline'
56-
]
5729
}

bytes/src/jmh/java/org/benchmark/BytesMegamorphicBenchmarkV1.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,24 @@
99

1010
import org.openjdk.jmh.annotations.Benchmark;
1111
import org.openjdk.jmh.annotations.BenchmarkMode;
12-
import org.openjdk.jmh.annotations.Fork;
1312
import org.openjdk.jmh.annotations.Measurement;
1413
import org.openjdk.jmh.annotations.Mode;
14+
import org.openjdk.jmh.annotations.OperationsPerInvocation;
1515
import org.openjdk.jmh.annotations.OutputTimeUnit;
1616
import org.openjdk.jmh.annotations.Param;
1717
import org.openjdk.jmh.annotations.Scope;
1818
import org.openjdk.jmh.annotations.Setup;
1919
import org.openjdk.jmh.annotations.State;
2020
import org.openjdk.jmh.annotations.Warmup;
2121

22-
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
22+
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
2323
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
2424
@BenchmarkMode(value = Mode.AverageTime)
25-
@State(Scope.Benchmark)
26-
@Fork(value = 1)
27-
@OutputTimeUnit(value = TimeUnit.MILLISECONDS)
28-
public class BytesMegamorphicBenchmarkV1 extends ProfiledBenchmark {
25+
@State(Scope.Thread)
26+
@OutputTimeUnit(value = TimeUnit.NANOSECONDS)
27+
public class BytesMegamorphicBenchmarkV1 {
2928
private static final int N = 4;
30-
private static final int FACTOR = 1000000;
29+
private static final int FACTOR = 1000;
3130
private static final Random RANDOM = new Random(23L);
3231
Bytes[] bytesV1;
3332

@@ -54,13 +53,10 @@ private static byte[] getBytes(final int size) {
5453
}
5554

5655
@Benchmark
56+
@OperationsPerInvocation(N * FACTOR)
5757
public void test() {
5858
for (Bytes b : bytesV1) {
5959
b.get(1);
6060
}
6161
}
62-
63-
String getUniqueTestId() {
64-
return this.getClass().getSimpleName() + "-" + mode + "-" + System.currentTimeMillis();
65-
}
6662
}

bytes/src/jmh/java/org/benchmark/BytesMegamorphicBenchmarkV2.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,24 @@
99

1010
import org.openjdk.jmh.annotations.Benchmark;
1111
import org.openjdk.jmh.annotations.BenchmarkMode;
12-
import org.openjdk.jmh.annotations.Fork;
1312
import org.openjdk.jmh.annotations.Measurement;
1413
import org.openjdk.jmh.annotations.Mode;
14+
import org.openjdk.jmh.annotations.OperationsPerInvocation;
1515
import org.openjdk.jmh.annotations.OutputTimeUnit;
1616
import org.openjdk.jmh.annotations.Param;
1717
import org.openjdk.jmh.annotations.Scope;
1818
import org.openjdk.jmh.annotations.Setup;
1919
import org.openjdk.jmh.annotations.State;
2020
import org.openjdk.jmh.annotations.Warmup;
2121

22-
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
22+
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
2323
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
2424
@BenchmarkMode(value = Mode.AverageTime)
25-
@State(Scope.Benchmark)
26-
@Fork(value = 1)
27-
@OutputTimeUnit(value = TimeUnit.MILLISECONDS)
28-
public class BytesMegamorphicBenchmarkV2 extends ProfiledBenchmark {
25+
@State(Scope.Thread)
26+
@OutputTimeUnit(value = TimeUnit.NANOSECONDS)
27+
public class BytesMegamorphicBenchmarkV2 {
2928
private static final int N = 4;
30-
private static final int FACTOR = 1000000;
29+
private static final int FACTOR = 1000;
3130
private static final Random RANDOM = new Random(23L);
3231
Bytes[] bytesV2;
3332

@@ -54,13 +53,10 @@ private static byte[] getBytes(final int size) {
5453
}
5554

5655
@Benchmark
56+
@OperationsPerInvocation(N * FACTOR)
5757
public void test() {
5858
for (Bytes b : bytesV2) {
5959
b.get(1);
6060
}
6161
}
62-
63-
String getUniqueTestId() {
64-
return this.getClass().getSimpleName() + "-" + mode + "-" + System.currentTimeMillis();
65-
}
6662
}

bytes/src/jmh/java/org/benchmark/ProfiledBenchmark.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)