Skip to content

Commit

Permalink
Проект №5 (#16)
Browse files Browse the repository at this point in the history
* Done project 5

* Fixes project 5

* Done project 5

* Move project 5 to test to increase coverage
  • Loading branch information
arhostcode authored Jan 4, 2024
1 parent 1cdd777 commit 4f8260e
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
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) {
}
}

0 comments on commit 4f8260e

Please sign in to comment.