-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Done project 5 * Fixes project 5 * Done project 5 * Move project 5 to test to increase coverage
- Loading branch information
1 parent
1cdd777
commit 4f8260e
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |