Вот что я сделал:Загрузите JAR-файлы JMH:
- Загрузите следующие JAR-файлы из Maven Central:
Код: Выделить всё
jmh-core
Код: Выделить всё
jmh-generator-annprocess
Добавьте JMH JAR в свой проект Eclipse:
- Щелкните правой кнопкой мыши свой проект в Eclipse.
- Выберите Путь сборки > Добавить внешние архивы...
- Просмотрите и выберите загруженные файлы JMH JAR.
- В Eclipse выберите Свойства проекта > Компилятор Java > Обработка аннотаций.
- Включите Обработку аннотаций и Factory Путь, если это необходимо для правильного распознавания аннотаций JMH.
BenchmarkRunner.java
Код: Выделить всё
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
public class BenchmarkRunner extends Runner implements Filterable {
private final Class benchmarkClass;
private final List benchmarkMethods;
private List readyToRunMethods;
public BenchmarkRunner(Class benchmarkClass) {
this.benchmarkClass = benchmarkClass;
benchmarkMethods = Arrays.stream(benchmarkClass.getDeclaredMethods())
.filter(m -> m.isAnnotationPresent(Benchmark.class))
.collect(Collectors.toList());
this.readyToRunMethods = new ArrayList(benchmarkMethods);
}
@Override
public Description getDescription() {
Description result = Description.createSuiteDescription(benchmarkClass);
readyToRunMethods.stream()
.map(this::getBenchmarkMethodDescription)
.forEach(result::addChild);
return result;
}
private Description getBenchmarkMethodDescription(Method benchmarkMethod) {
return Description.createTestDescription(benchmarkClass, benchmarkMethod.getName());
}
@Override
public void run(RunNotifier notifier) {
for (Method benchmarkMethod : readyToRunMethods) {
Description testDescription = getBenchmarkMethodDescription(benchmarkMethod);
try {
notifier.fireTestStarted(testDescription);
Options opt = new OptionsBuilder()
.include(".*" + benchmarkClass.getSimpleName() + ".*")
.jvmArgsAppend("-Djmh.separateClasspathJAR=false")
.result(benchmarkClass.getName() + "." + benchmarkMethod.getName() + "_" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + ".json")
.resultFormat(ResultFormatType.JSON)
.build();
new org.openjdk.jmh.runner.Runner(opt).run();
notifier.fireTestFinished(testDescription);
} catch (Exception e) {
e.printStackTrace();
notifier.fireTestFailure(new Failure(testDescription, e));
return;
}
}
}
@Override
public void filter(Filter filter) throws NoTestsRemainException {
List filteredMethods = new ArrayList();
for (Method benchmarkMethod : benchmarkMethods) {
if (filter.shouldRun(getBenchmarkMethodDescription(benchmarkMethod))) {
filteredMethods.add(benchmarkMethod);
}
}
if (filteredMethods.isEmpty()) {
throw new NoTestsRemainException();
}
this.readyToRunMethods = filteredMethods;
}
}
Код: Выделить всё
package xoc.stex.jmh;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.junit.runner.RunWith;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
@RunWith(BenchmarkRunner.class)
public class CollectionsBenchmark {
@Benchmark
public void addAndRemoveToArrayList(Data data) {
data.arrayList.add(data.element);
data.arrayList.remove(data.element);
}
@Benchmark
public void addAndRemoveToLinkedList(Data data) {
data.linkedList.add(data.element);
data.linkedList.remove(data.element);
}
@State(Scope.Benchmark)
public static class Data {
private final List arrayList = new ArrayList();
private final List linkedList = new LinkedList();
private Object element;
@Setup(Level.Iteration)
public void setUp() {
element = new Object();
}
}
}
Каталог bin/META-INF содержит BenchmarkList и CompilerHints< /code> так что эта часть работает.
bin/META-INF/BenchmarkList
Код: Выделить всё
JMH S 33 xoc.stex.jmh.CollectionsBenchmark S 79 xoc.stex.jmh.jmh_generated.CollectionsBenchmark_addAndRemoveToArrayList_jmhTest S 23 addAndRemoveToArrayList S 10 Throughput E A 1 1 1 E E E E E E E E E E E E E E E E E
JMH S 33 xoc.stex.jmh.CollectionsBenchmark S 80 xoc.stex.jmh.jmh_generated.CollectionsBenchmark_addAndRemoveToLinkedList_jmhTest S 24 addAndRemoveToLinkedList S 10 Throughput E A 1 1 1 E E E E E E E E E E E E E E E E E
JMH S 24 xoc.stex.jmh.MyBenchmark S 57 xoc.stex.jmh.jmh_generated.MyBenchmark_testMethod_jmhTest S 10 testMethod S 11 AverageTime E A 1 1 1 E E E E E E E E E E E E E E U 12 MILLISECONDS E E
Код: Выделить всё
dontinline,*.*_all_jmhStub
dontinline,*.*_avgt_jmhStub
dontinline,*.*_sample_jmhStub
dontinline,*.*_ss_jmhStub
dontinline,*.*_thrpt_jmhStub
inline,xoc/stex/jmh/MyBenchmark.testMethod
Подробнее здесь: https://stackoverflow.com/questions/791 ... le-project