Skip to content
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
6 changes: 6 additions & 0 deletions microbenchmark-runner-junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,95 +9,80 @@
*/
package jmh.mbr.junit5.discovery;

import static java.util.stream.Collectors.*;
import static org.junit.platform.engine.discovery.DiscoverySelectors.*;
import static org.junit.platform.engine.support.discovery.SelectorResolver.Resolution.*;

import jmh.mbr.core.model.BenchmarkClass;
import jmh.mbr.core.model.BenchmarkDescriptorFactory;
import jmh.mbr.junit5.descriptor.BenchmarkClassDescriptor;
import jmh.mbr.junit5.discovery.predicates.IsBenchmarkClass;
import jmh.mbr.junit5.discovery.predicates.IsBenchmarkMethod;

import java.lang.reflect.AnnotatedElement;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.support.HierarchyTraversalMode;
import org.junit.platform.commons.support.ReflectionSupport;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.discovery.ClassSelector;
import org.junit.platform.engine.discovery.UniqueIdSelector;
import org.junit.platform.engine.support.discovery.SelectorResolver;

/**
* {@link ElementResolver} for test containers. Containers are based on {@link Class classes} that contain
* {@link SelectorResolver} for test containers. Containers are based on {@link Class classes} that contain
* {@code Benchmark} methods.
*
* @see IsBenchmarkClass
*/
class BenchmarkContainerResolver implements ElementResolver {
class BenchmarkContainerResolver implements SelectorResolver {

private static final String SEGMENT_TYPE = "class";

BenchmarkContainerResolver() {}

@Override
public Set<TestDescriptor> resolveElement(AnnotatedElement element, TestDescriptor parent) {

if (!(element instanceof Class)) {
return Collections.emptySet();
}
private final Predicate<String> classNameFilter;

Class<?> clazz = (Class<?>) element;
if (!isPotentialCandidate(clazz)) {
return Collections.emptySet();
}

UniqueId uniqueId = createUniqueId(clazz, parent);
return Collections.singleton(resolveClass(clazz, uniqueId));
BenchmarkContainerResolver(Predicate<String> classNameFilter) {
this.classNameFilter = classNameFilter;
}

@Override
public Optional<TestDescriptor> resolveUniqueId(UniqueId.Segment segment, TestDescriptor parent) {

if (!segment.getType().equals(SEGMENT_TYPE)) {
return Optional.empty();
public Resolution resolve(ClassSelector selector, Context context) {
if (classNameFilter.test(selector.getClassName())) {
return resolveClass(selector.getJavaClass(), context);
}
return Resolution.unresolved();
}

if (!requiredParentType().isInstance(parent)) {
return Optional.empty();
}

String className = getClassName(parent, segment.getValue());

Optional<Class<?>> optionalContainerClass = ReflectionUtils.loadClass(className);
if (!optionalContainerClass.isPresent()) {
return Optional.empty();
}
@Override
public Resolution resolve(UniqueIdSelector selector, Context context) {
UniqueId uniqueId = selector.getUniqueId();
UniqueId.Segment lastSegment = uniqueId.getLastSegment();

Class<?> containerClass = optionalContainerClass.get();
if (!isPotentialCandidate(containerClass)) {
return Optional.empty();
if (lastSegment.getType().equals(SEGMENT_TYPE)) {
return ReflectionSupport.tryToLoadClass(lastSegment.getValue()).toOptional()
.map(testClass -> resolveClass(testClass, context)).orElse(unresolved());
}

UniqueId uniqueId = createUniqueId(containerClass, parent);
return Optional.of(resolveClass(containerClass, uniqueId));
return unresolved();
}

private Class<? extends TestDescriptor> requiredParentType() {
return TestDescriptor.class;
}

private String getClassName(TestDescriptor parent, String segmentValue) {
return segmentValue;
}

private boolean isPotentialCandidate(Class<?> element) {
return IsBenchmarkClass.INSTANCE.test(element);
private Resolution resolveClass(Class<?> testClass, Context context) {
if (IsBenchmarkClass.INSTANCE.test(testClass)) {
return context.addToParent(parent -> createClassDescriptor(testClass, parent))
.map(descriptor -> Resolution.match(Match.exact(descriptor,
() -> ReflectionSupport
.streamMethods(testClass, IsBenchmarkMethod.INSTANCE, HierarchyTraversalMode.TOP_DOWN)
.map(m -> selectMethod(testClass, m)).collect(toSet())))) //
.orElse(unresolved());
}
return unresolved();
}

private UniqueId createUniqueId(Class<?> benchmarkClass, TestDescriptor parent) {
return parent.getUniqueId().append(SEGMENT_TYPE, benchmarkClass.getName());
private static Optional<BenchmarkClassDescriptor> createClassDescriptor(Class<?> testClass, TestDescriptor parent) {
UniqueId uniqueId = parent.getUniqueId().append(BenchmarkContainerResolver.SEGMENT_TYPE, testClass.getName());
BenchmarkClass descriptor = BenchmarkDescriptorFactory.create(testClass).createDescriptor();
return Optional.of(new BenchmarkClassDescriptor(uniqueId, descriptor));
}

private TestDescriptor resolveClass(Class<?> benchmarkClass, UniqueId uniqueId) {

BenchmarkClass descriptor = BenchmarkDescriptorFactory.create(benchmarkClass).createDescriptor();

return new BenchmarkClassDescriptor(uniqueId, descriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,86 +9,55 @@
*/
package jmh.mbr.junit5.discovery;

import static org.junit.platform.engine.discovery.DiscoverySelectors.*;
import static org.junit.platform.engine.support.discovery.SelectorResolver.Resolution.*;

import jmh.mbr.core.model.BenchmarkFixture;
import jmh.mbr.core.model.BenchmarkMethod;
import jmh.mbr.core.model.ParametrizedBenchmarkMethod;
import jmh.mbr.junit5.descriptor.AbstractBenchmarkDescriptor;
import jmh.mbr.junit5.descriptor.BenchmarkFixtureDescriptor;
import jmh.mbr.junit5.descriptor.ParametrizedBenchmarkMethodDescriptor;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.UniqueId.Segment;
import org.junit.platform.engine.discovery.UniqueIdSelector;
import org.junit.platform.engine.support.discovery.SelectorResolver;

/**
* {@link ElementResolver} for {@link jmh.mbr.core.model.BenchmarkFixture fixtures}.
* {@link SelectorResolver} for {@link jmh.mbr.core.model.BenchmarkFixture fixtures}.
*/
class BenchmarkFixtureResolver implements ElementResolver {

private static final String SEGMENT_TYPE = "fixture";

@Override
public Set<TestDescriptor> resolveElement(AnnotatedElement element, TestDescriptor parent) {

if (!(element instanceof Method)) {
return Collections.emptySet();
}

if (!(parent instanceof ParametrizedBenchmarkMethodDescriptor)) {
return Collections.emptySet();
}

ParametrizedBenchmarkMethodDescriptor parentDescriptor = (ParametrizedBenchmarkMethodDescriptor) parent;

Method method = (Method) element;

if (!parentDescriptor.isUnderlyingMethod(method)) {
return Collections.emptySet();
}
class BenchmarkFixtureResolver implements SelectorResolver {

return findFixtures(parent.getUniqueId(), parentDescriptor.getParametrizedMethod());
}
static final String SEGMENT_TYPE = "fixture";

@Override
public Optional<TestDescriptor> resolveUniqueId(Segment segment, TestDescriptor parent) {

if (!segment.getType().equals(SEGMENT_TYPE)) {
return Optional.empty();
}

if (!(parent instanceof ParametrizedBenchmarkMethodDescriptor)) {
return Optional.empty();
public Resolution resolve(UniqueIdSelector selector, Context context) {

UniqueId uniqueId = selector.getUniqueId();
UniqueId.Segment lastSegment = uniqueId.getLastSegment();

if (lastSegment.getType().equals(BenchmarkFixtureResolver.SEGMENT_TYPE)) {
return context
.<AbstractBenchmarkDescriptor> addToParent(() -> selectUniqueId(uniqueId.removeLastSegment()), parent -> {
ParametrizedBenchmarkMethodDescriptor methodDescriptor = (ParametrizedBenchmarkMethodDescriptor) parent;
ParametrizedBenchmarkMethod parametrizedMethod = methodDescriptor.getParametrizedMethod();
return parametrizedMethod.getChildren().stream() //
.filter(fixture -> fixture.getDisplayName().equals(lastSegment.getValue())) //
.findFirst() //
.flatMap(fixture -> Optional
.of(createFixtureDescriptor(parent, parametrizedMethod.getDescriptor(), fixture)));
}).map(descriptor -> Resolution.match(Match.exact(descriptor))).orElse(unresolved());
}

ParametrizedBenchmarkMethodDescriptor parentDescriptor = (ParametrizedBenchmarkMethodDescriptor) parent;

return findTestDescriptor(segment.getValue(), parentDescriptor.getUniqueId(),
parentDescriptor.getParametrizedMethod());
}

private Set<TestDescriptor> findFixtures(UniqueId parentId, ParametrizedBenchmarkMethod descriptor) {

return descriptor.getChildren().stream().map(it -> {

UniqueId uniqueId = parentId.append(SEGMENT_TYPE, it.getDisplayName());
return new BenchmarkFixtureDescriptor(uniqueId, descriptor.getDescriptor(), it);
}).collect(Collectors.toSet());
return unresolved();
}

private Optional<TestDescriptor> findTestDescriptor(String segmentValue, UniqueId parentId,
ParametrizedBenchmarkMethod parametrizedMethod) {

return parametrizedMethod.getChildren().stream().filter(it -> {
return it.getDisplayName().equals(segmentValue);
}).map(it -> {

UniqueId uniqueId = parentId.append(SEGMENT_TYPE, it.getDisplayName());

return (TestDescriptor) new BenchmarkFixtureDescriptor(uniqueId, parametrizedMethod.getDescriptor(), it);
}).findFirst();
private BenchmarkFixtureDescriptor createFixtureDescriptor(TestDescriptor parent, BenchmarkMethod method,
BenchmarkFixture fixture) {
UniqueId uniqueId = parent.getUniqueId().append(BenchmarkFixtureResolver.SEGMENT_TYPE, fixture.getDisplayName());
return new BenchmarkFixtureDescriptor(uniqueId, method, fixture);
}
}
Loading