From 4f8260e8bcc81b63c4bf4c9a7d5b1702ca4460a3 Mon Sep 17 00:00:00 2001 From: "Ardyc (Gulyakin Egor)" <42569377+ArhostCode@users.noreply.github.com> Date: Thu, 4 Jan 2024 11:54:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=20?= =?UTF-8?q?=E2=84=965=20(#16)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Done project 5 * Fixes project 5 * Done project 5 * Move project 5 to test to increase coverage --- pom.xml | 15 +++ .../edu/project5/ReflectionBenchmark.java | 101 ++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/test/java/edu/project5/ReflectionBenchmark.java diff --git a/pom.xml b/pom.xml index c34bdc6..0877fc1 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,7 @@ 4.0.0-M9 0.8.10 + 1.37 1.14.9 9.6 @@ -137,6 +138,20 @@ 5.6.0 test + + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + provided + + diff --git a/src/test/java/edu/project5/ReflectionBenchmark.java b/src/test/java/edu/project5/ReflectionBenchmark.java new file mode 100644 index 0000000..8cb38aa --- /dev/null +++ b/src/test/java/edu/project5/ReflectionBenchmark.java @@ -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 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) 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) { + } +}