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

Проект №5 #16

Merged
merged 5 commits into from
Jan 4, 2024
Merged
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
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
<maven-site-plugin.version>4.0.0-M9</maven-site-plugin.version>

<jacoco-maven-plugin.version>0.8.10</jacoco-maven-plugin.version>
<jmh.version>1.37</jmh.version>
<bytebuddy.version>1.14.9</bytebuddy.version>
<asm.version>9.6</asm.version>
</properties>
@@ -137,6 +138,20 @@
<version>5.6.0</version>
<scope>test</scope>
</dependency>

<!-- JMH -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
101 changes: 101 additions & 0 deletions src/test/java/edu/project5/ReflectionBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package edu.project5;

import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import lombok.SneakyThrows;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;

@State(Scope.Thread)
public class ReflectionBenchmark {

private static final String TESTING_METHOD = "name";
private static final int WARMUP_TIME = 5;
private static final int MEASUREMENT_TIME = 5;

@SuppressWarnings("checkstyle:UncommentedMain")
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(ReflectionBenchmark.class.getSimpleName())
.shouldFailOnError(true)
.shouldDoGC(true)
.mode(Mode.AverageTime)
.timeUnit(TimeUnit.NANOSECONDS)
.forks(1)
.warmupForks(1)
.warmupIterations(1)
.warmupTime(TimeValue.seconds(WARMUP_TIME))
.measurementIterations(1)
.measurementTime(TimeValue.seconds(MEASUREMENT_TIME))
.build();

new Runner(options).run();
}

private Student student;
private Method method;
private MethodHandle methodHandle;
private Function<Student, String> lambdaMetaFactory;

@SneakyThrows @Setup
public void setup() {
student = new Student("Egor", "Gulyakin");

method = Student.class.getMethod(TESTING_METHOD);

MethodHandles.Lookup publicLookup = MethodHandles.lookup();
MethodType methodType = MethodType.methodType(String.class);
methodHandle = publicLookup.findVirtual(Student.class, TESTING_METHOD, methodType);

lambdaMetaFactory = (Function<Student, String>) LambdaMetafactory.metafactory(
publicLookup,
"apply",
MethodType.methodType(Function.class),
MethodType.methodType(Object.class, Object.class),
methodHandle,
MethodType.methodType(String.class, Student.class)
).getTarget().invokeExact();
}

@Benchmark
public void directAccess(Blackhole blackhole) {
String name = student.name();
blackhole.consume(name);
}

@Benchmark
public void reflection(Blackhole blackhole) throws InvocationTargetException, IllegalAccessException {
String name = (String) method.invoke(student);
blackhole.consume(name);
}

@Benchmark
public void methodHandle(Blackhole blackhole) throws Throwable {
String name = (String) methodHandle.invoke(student);
blackhole.consume(name);
}

@Benchmark
public void lambdaMetaFactory(Blackhole blackhole) {
String name = lambdaMetaFactory.apply(student);
blackhole.consume(name);
}

private record Student(String name, String surname) {
}
}