From 56365763f2684fbeb46876875622f9a97f76cfbf Mon Sep 17 00:00:00 2001 From: ardyc Date: Mon, 4 Dec 2023 11:31:22 +0300 Subject: [PATCH 1/4] Done project 5 --- pom.xml | 15 +++ .../edu/project5/ReflectionBenchmark.java | 101 ++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/main/java/edu/project5/ReflectionBenchmark.java diff --git a/pom.xml b/pom.xml index f508fbd..a3e025b 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,7 @@ 4.0.0-M9 0.8.10 + 1.37 @@ -120,6 +121,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/main/java/edu/project5/ReflectionBenchmark.java b/src/main/java/edu/project5/ReflectionBenchmark.java new file mode 100644 index 0000000..97f9263 --- /dev/null +++ b/src/main/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() throws NoSuchMethodException, IllegalAccessException { + student = new Student("Alexander", "Biryukov"); + + 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) throws Throwable { + String name = lambdaMetaFactory.apply(student); + blackhole.consume(name); + } + + private record Student(String name, String surname) { + } +} From 115a3e1fd720df9adbd2582e74456212b8b623ee Mon Sep 17 00:00:00 2001 From: ardyc Date: Wed, 13 Dec 2023 20:59:36 +0300 Subject: [PATCH 2/4] Fixes project 5 --- src/main/java/edu/project5/ReflectionBenchmark.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/edu/project5/ReflectionBenchmark.java b/src/main/java/edu/project5/ReflectionBenchmark.java index 97f9263..c294973 100644 --- a/src/main/java/edu/project5/ReflectionBenchmark.java +++ b/src/main/java/edu/project5/ReflectionBenchmark.java @@ -53,7 +53,7 @@ public static void main(String[] args) throws RunnerException { private Function lambdaMetaFactory; @SneakyThrows @Setup - public void setup() throws NoSuchMethodException, IllegalAccessException { + public void setup() { student = new Student("Alexander", "Biryukov"); method = Student.class.getMethod(TESTING_METHOD); @@ -91,7 +91,7 @@ public void methodHandle(Blackhole blackhole) throws Throwable { } @Benchmark - public void lambdaMetaFactory(Blackhole blackhole) throws Throwable { + public void lambdaMetaFactory(Blackhole blackhole) { String name = lambdaMetaFactory.apply(student); blackhole.consume(name); } From dde5b2ebb914ef96b6e48933b9484ddc8405e773 Mon Sep 17 00:00:00 2001 From: ardyc Date: Thu, 14 Dec 2023 22:12:55 +0300 Subject: [PATCH 3/4] Done project 5 --- src/main/java/edu/project5/ReflectionBenchmark.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/edu/project5/ReflectionBenchmark.java b/src/main/java/edu/project5/ReflectionBenchmark.java index c294973..8cb38aa 100644 --- a/src/main/java/edu/project5/ReflectionBenchmark.java +++ b/src/main/java/edu/project5/ReflectionBenchmark.java @@ -54,7 +54,7 @@ public static void main(String[] args) throws RunnerException { @SneakyThrows @Setup public void setup() { - student = new Student("Alexander", "Biryukov"); + student = new Student("Egor", "Gulyakin"); method = Student.class.getMethod(TESTING_METHOD); From c9df0d87795696004eb6efb2a0159d50f90e06ae Mon Sep 17 00:00:00 2001 From: ardyc Date: Thu, 14 Dec 2023 22:33:23 +0300 Subject: [PATCH 4/4] Move project 5 to test to increase coverage --- src/{main => test}/java/edu/project5/ReflectionBenchmark.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{main => test}/java/edu/project5/ReflectionBenchmark.java (100%) diff --git a/src/main/java/edu/project5/ReflectionBenchmark.java b/src/test/java/edu/project5/ReflectionBenchmark.java similarity index 100% rename from src/main/java/edu/project5/ReflectionBenchmark.java rename to src/test/java/edu/project5/ReflectionBenchmark.java