Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JMH microbenchmarks #35

Merged
merged 4 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions benchmarks/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
plugins {
id "java"
id "me.champeau.gradle.jmh" version "0.4.8"
}

sourceCompatibility = targetCompatibility = 8

jmh {
jmhVersion = '1.21'
duplicateClassesStrategy DuplicatesStrategy.INCLUDE
failOnError = true
}

dependencies {
jmh project(path: ":agent", configuration: 'shadow')
jmh "org.openjdk.jmh:jmh-generator-annprocess:1.21"
}
134 changes: 134 additions & 0 deletions benchmarks/src/jmh/java/reactor/blockhound/BlockHoundBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2018-2019 Pivotal Software Inc, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.blockhound;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

@SuppressWarnings("WeakerAccess")
@Warmup(iterations = 5)
@Measurement(iterations = 3)
@BenchmarkMode({Mode.AverageTime})
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@OperationsPerInvocation(BlockHoundBenchmark.OPERATIONS_PER_INVOCATION)
public class BlockHoundBenchmark {

static {
Thread.setDefaultUncaughtExceptionHandler((t, e) -> e.printStackTrace());
}

static final int OPERATIONS_PER_INVOCATION = 1_000;

static AtomicLong counter = new AtomicLong();

static void nonBlockingCall() {
counter.incrementAndGet();
}

static void blockingCall() {
try {
Thread.sleep(0);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

static void allowsBlockingCalls() {
blockingCall();
}

@State(Scope.Benchmark)
public static class BlockHoundInstalledState {

@Setup
public void prepare() {
System.out.println("Installing BlockHound");
BlockHound.builder()
.nonBlockingThreadPredicate(p -> p.or(NonBlockingThread.class::isInstance))
.allowBlockingCallsInside(BlockHoundBenchmark.class.getName(), "allowsBlockingCalls")
.blockingMethodCallback(m -> {}) // Do not throw
.install();
}
}

@Benchmark
public void baselineNonBlockingCall() throws Exception {
Thread thread = new Thread(runMultipleTimes(BlockHoundBenchmark::nonBlockingCall));
thread.start();
thread.join(5_000);
}

@Benchmark
public void measureNonBlockingCall(BlockHoundInstalledState state) throws Exception {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the JMH documentation calls inside the benchmark that do not return a value are easier to inline the optimalizations in JVM. Therefore, my tests had a return value or were using a BlackHole to prevent that. However, I haven't analyzed the calls using threads or sleep(0)/incrementAndGet() with a profiler, so maybe you were able to ensure that it doesn't happen. Similar situation with optimizations is with running it in a loop.

In general, it's nice that you were able to simplify the benchmarks itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the benchmark starts a Thread now, hence the inlining cannot affect the results I guess

Thread thread = new NonBlockingThread(runMultipleTimes(BlockHoundBenchmark::nonBlockingCall));
thread.start();
thread.join(5_000);
}

@Benchmark
public void baselineBlockingCallInBlockingThread() throws Exception {
Thread thread = new Thread(runMultipleTimes(BlockHoundBenchmark::blockingCall));
thread.start();
thread.join(5_000);
}

@Benchmark
public void measureBlockingCallInBlockingThread(BlockHoundInstalledState state) throws Exception {
Thread thread = new Thread(runMultipleTimes(BlockHoundBenchmark::blockingCall));
thread.start();
thread.join(5_000);
}

@Benchmark
public void baselineAllowedBlockingCall() throws Exception {
Thread thread = new Thread(runMultipleTimes(BlockHoundBenchmark::allowsBlockingCalls));
thread.start();
thread.join(5_000);
}

@Benchmark
public void measureAllowedBlockingCall(BlockHoundInstalledState state) throws Exception {
Thread thread = new NonBlockingThread(runMultipleTimes(BlockHoundBenchmark::allowsBlockingCalls));
thread.start();
thread.join(5_000);
}

static Runnable runMultipleTimes(Runnable runnable) {
return () -> {
for (int i = 0; i < OPERATIONS_PER_INVOCATION; i++) {
runnable.run();
}
};
}

static final class NonBlockingThread extends Thread {
public NonBlockingThread(Runnable target) {
super(target);
}
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ include 'agent'
include 'native-agent'
include 'example'
include 'junit-platform'
include 'benchmarks'