From 478a12bcb729fbf7a4fd25366fc0a110282f9737 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 8 Aug 2018 10:50:56 +0200 Subject: [PATCH] #78 - Fix benchmark filtering. We now check the specified benchmark filter expression against the JUnit class to make sure JMH does not spin up benchmark classes that are different than the actual JUnit class. --- .../microbenchmark/common/JmhSupport.java | 15 ++++++---- .../microbenchmark/common/Microbenchmark.java | 29 ++++++++++++++++--- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/benchmark/support/src/main/java/org/springframework/data/microbenchmark/common/JmhSupport.java b/benchmark/support/src/main/java/org/springframework/data/microbenchmark/common/JmhSupport.java index eaf265c..19334fe 100644 --- a/benchmark/support/src/main/java/org/springframework/data/microbenchmark/common/JmhSupport.java +++ b/benchmark/support/src/main/java/org/springframework/data/microbenchmark/common/JmhSupport.java @@ -58,9 +58,10 @@ class JmhSupport { * name separator. * * @return never {@literal null}. + * @param name * @param methods */ - protected List includes(Collection methods) { + protected List includes(Class testClass, Collection methods) { String tests = environment.getProperty("benchmark", String.class); @@ -70,12 +71,16 @@ class JmhSupport { .collect(Collectors.toList()); } - if (!tests.contains("#")) { - return Collections.singletonList(".*" + tests + ".*"); + if (tests.contains(testClass.getName()) || tests.contains(testClass.getSimpleName())) { + if (!tests.contains("#")) { + return Collections.singletonList(".*" + tests + ".*"); + } + + String[] args = tests.split("#"); + return Collections.singletonList(".*" + args[0] + "." + args[1]); } - String[] args = tests.split("#"); - return Collections.singletonList(".*" + args[0] + "." + args[1]); + return Collections.emptyList(); } /** diff --git a/benchmark/support/src/main/java/org/springframework/data/microbenchmark/common/Microbenchmark.java b/benchmark/support/src/main/java/org/springframework/data/microbenchmark/common/Microbenchmark.java index fe17c75..f242b90 100644 --- a/benchmark/support/src/main/java/org/springframework/data/microbenchmark/common/Microbenchmark.java +++ b/benchmark/support/src/main/java/org/springframework/data/microbenchmark/common/Microbenchmark.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Function; @@ -47,6 +48,7 @@ import org.openjdk.jmh.results.BenchmarkResult; import org.openjdk.jmh.results.IterationResult; import org.openjdk.jmh.results.RunResult; import org.openjdk.jmh.runner.Defaults; +import org.openjdk.jmh.runner.NoBenchmarksException; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.format.OutputFormat; import org.openjdk.jmh.runner.format.OutputFormatFactory; @@ -189,18 +191,33 @@ public class Microbenchmark extends BlockJUnit4ClassRunner { Collection methods = getFilteredChildren(); CacheFunction cache = new CacheFunction(methods, this::describeChild); + if (methods.isEmpty()) { + return new Statement() { + @Override + public void evaluate() {} + }; + } + return new Statement() { @Override public void evaluate() throws Throwable { - doRun(notifier, methods, cache); + try { + doRun(notifier, methods, cache); + } catch (NoBenchmarksException | NoTestsRemainException e) { + methods.forEach(it -> notifier.fireTestIgnored(describeChild(it))); + } } }; } - private void doRun(RunNotifier notifier, Collection methods, CacheFunction cache) throws Exception { + void doRun(RunNotifier notifier, Collection methods, CacheFunction cache) throws Exception { - List includes = jmhRunner.includes(methods); + List includes = jmhRunner.includes(getTestClass().getJavaClass(), methods); + + if (includes.isEmpty()) { + throw new NoTestsRemainException(); + } ChainedOptionsBuilder optionsBuilder = jmhRunner.options(); @@ -466,7 +483,11 @@ public class Microbenchmark extends BlockJUnit4ClassRunner { public Description apply(String benchmarkName) { FrameworkMethod frameworkMethod = methodMap.computeIfAbsent(benchmarkName, key -> { - return methods.stream().filter(method -> getBenchmarkName(method).equals(key)).findFirst().get(); + + Optional method = methods.stream().filter(it -> getBenchmarkName(it).equals(key)).findFirst(); + + return method.orElseThrow(() -> new IllegalArgumentException( + String.format("Cannot resolve %s to a FrameworkMethod!", benchmarkName))); }); return describeFunction.apply(frameworkMethod);