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

[JENKINS-58038] Use Annotation-Indexer instead of Reflections #141

Merged
merged 5 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,6 @@ THE SOFTWARE.
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
32 changes: 9 additions & 23 deletions src/main/java/jenkins/benchmark/jmh/BenchmarkFinder.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
package jenkins.benchmark.jmh;

import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
import org.reflections.Reflections;

import java.util.Objects;
import java.util.Set;
import net.java.sezpoz.Index;
import net.java.sezpoz.IndexItem;
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;

/**
* Find classes annotated with {@link JmhBenchmark} to run their benchmark methods.
* @since TODO
*/
@SuppressWarnings("WeakerAccess")
public final class BenchmarkFinder {
final private String[] packageName;

/**
* Creates a {@link BenchmarkFinder}
*
* @param packageNames find benchmarks in these packages
*/
public BenchmarkFinder(String... packageNames) {
this.packageName = packageNames;
private BenchmarkFinder() {
}

/**
* Includes classes annotated with {@link JmhBenchmark} as candidates for JMH benchmarks.
*
* @param optionsBuilder the optionsBuilder used to build the benchmarks
*/
public void findBenchmarks(ChainedOptionsBuilder optionsBuilder) {
Reflections reflections = new Reflections((Object[]) packageName);
Set<Class<?>> benchmarkClasses = reflections.getTypesAnnotatedWith(JmhBenchmark.class);
benchmarkClasses.forEach(clazz -> {
JmhBenchmark annotation = clazz.getAnnotation(JmhBenchmark.class);
if (Objects.nonNull(annotation)) {
optionsBuilder.include(clazz.getName() + annotation.value());
}
});
public static void findBenchmarks(ChainedOptionsBuilder optionsBuilder) {
for (IndexItem<JmhBenchmark, Object> item : Index.load(JmhBenchmark.class, Object.class)) {
optionsBuilder.include(item.className() + item.annotation().value());
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/jenkins/benchmark/jmh/JmhBenchmark.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package jenkins.benchmark.jmh;

import net.java.sezpoz.Indexable;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -11,6 +13,7 @@
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Indexable
public @interface JmhBenchmark {
/**
* Methods which annotated by {@link org.openjdk.jmh.annotations.Benchmark}
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/jenkins/benchmark/jmh/BenchmarkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ public void testJmhBenchmarks() throws Exception {
ChainedOptionsBuilder optionsBuilder =
new OptionsBuilder()
.forks(1)
.warmupIterations(1)
.warmupBatchSize(1)
.warmupIterations(0)
.measurementIterations(1)
.measurementBatchSize(1)
.shouldFailOnError(true)
.result("target/jmh-reports/jmh-benchmark-report.json")
.timeUnit(TimeUnit.MICROSECONDS)
.resultFormat(ResultFormatType.JSON);
BenchmarkFinder finder = new BenchmarkFinder(this.getClass().getPackage().getName());
finder.findBenchmarks(optionsBuilder);
BenchmarkFinder.findBenchmarks(optionsBuilder);
new Runner(optionsBuilder.build()).run();
}
}