-
Notifications
You must be signed in to change notification settings - Fork 98
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
134
benchmarks/src/jmh/java/reactor/blockhound/BlockHoundBenchmark.java
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
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 { | ||
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); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ include 'agent' | |
include 'native-agent' | ||
include 'example' | ||
include 'junit-platform' | ||
include 'benchmarks' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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