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 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
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: 16 additions & 16 deletions src/main/java/jenkins/benchmark/jmh/BenchmarkFinder.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
package jenkins.benchmark.jmh;


import org.jvnet.hudson.annotation_indexer.Index;
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
import org.reflections.Reflections;

import java.util.Objects;
import java.util.Set;
import java.io.IOException;
import java.lang.reflect.AnnotatedElement;

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

/**
* Creates a {@link BenchmarkFinder}
* Class whose {@link ClassLoader} will be used to search for benchmarks.
*
* @param packageNames find benchmarks in these packages
* @param clazz the class whose {@link ClassLoader} will be used to search for benchmarks.
*/
public BenchmarkFinder(String... packageNames) {
this.packageName = packageNames;
public BenchmarkFinder(Class<?> clazz) {
this.classLoader = clazz.getClassLoader();
}

/**
* 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 -> {
public void findBenchmarks(ChainedOptionsBuilder optionsBuilder) throws IOException {
for (AnnotatedElement e : Index.list(JmhBenchmark.class, classLoader)) {
Class<?> clazz = (Class<?>) e;
JmhBenchmark annotation = clazz.getAnnotation(JmhBenchmark.class);
if (Objects.nonNull(annotation)) {
optionsBuilder.include(clazz.getName() + annotation.value());
}
});
optionsBuilder.include(clazz.getName() + annotation.value());
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/jenkins/benchmark/jmh/JmhBenchmark.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package jenkins.benchmark.jmh;

import org.jvnet.hudson.annotation_indexer.Indexed;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotate your benchmark classes with this annotation to allow them to be discovered by {@link BenchmarkFinder}
*
* @since 2.50
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Indexed
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);
new BenchmarkFinder(getClass()).findBenchmarks(optionsBuilder);
new Runner(optionsBuilder.build()).run();
}
}