Skip to content

Commit

Permalink
Use annotation-indexer as suggested by @jglick
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhyudayaSharma committed Jun 17, 2019
1 parent 061ed49 commit 992fc81
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
27 changes: 21 additions & 6 deletions src/main/java/jenkins/benchmark/jmh/BenchmarkFinder.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
package jenkins.benchmark.jmh;


import net.java.sezpoz.Index;
import net.java.sezpoz.IndexItem;
import org.jvnet.hudson.annotation_indexer.Index;
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;

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 {
private BenchmarkFinder() {
private final ClassLoader classLoader;

/**
* Class whose {@link ClassLoader} will be used to search for benchmarks.
*
* @param clazz the class whose {@link ClassLoader} will be used to search for benchmarks.
*/
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 static void findBenchmarks(ChainedOptionsBuilder optionsBuilder) {
for (IndexItem<JmhBenchmark, Object> item : Index.load(JmhBenchmark.class, Object.class)) {
optionsBuilder.include(item.className() + item.annotation().value());
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);
optionsBuilder.include(clazz.getName() + annotation.value());
System.out.println(clazz.getName());
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/jenkins/benchmark/jmh/JmhBenchmark.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package jenkins.benchmark.jmh;

import net.java.sezpoz.Indexable;
import org.jvnet.hudson.annotation_indexer.Indexed;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -9,11 +9,12 @@

/**
* 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)
@Indexable
@Indexed
public @interface JmhBenchmark {
/**
* Methods which annotated by {@link org.openjdk.jmh.annotations.Benchmark}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/jenkins/benchmark/jmh/BenchmarkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testJmhBenchmarks() throws Exception {
.result("target/jmh-reports/jmh-benchmark-report.json")
.timeUnit(TimeUnit.MICROSECONDS)
.resultFormat(ResultFormatType.JSON);
BenchmarkFinder.findBenchmarks(optionsBuilder);
new BenchmarkFinder(getClass()).findBenchmarks(optionsBuilder);
new Runner(optionsBuilder.build()).run();
}
}

0 comments on commit 992fc81

Please sign in to comment.