-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionBenchmark.java
More file actions
99 lines (85 loc) · 3.2 KB
/
ReflectionBenchmark.java
File metadata and controls
99 lines (85 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package edu.project5;
import java.lang.invoke.CallSite;
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.Supplier;
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 {
@SuppressWarnings({"checkstyle:UncommentedMain", "checkstyle:MagicNumber"})
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(5))
.measurementIterations(1)
.measurementTime(TimeValue.seconds(5))
.build();
new Runner(options).run();
}
private static final String METHOD_NAME = "name";
private Student student;
private Method method;
private MethodHandle methodHandle;
private Supplier<String> lambda;
@Setup
@SuppressWarnings("unchecked")
public void setup() throws Throwable {
MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(String.class);
student = new Student("Daniil", "Lokosov");
method = student.getClass().getMethod(METHOD_NAME);
methodHandle = publicLookup.findVirtual(Student.class, METHOD_NAME, mt);
CallSite callSite = LambdaMetafactory.metafactory(
lookup,
"get",
MethodType.methodType(Supplier.class, Student.class),
MethodType.methodType(Object.class),
methodHandle,
mt
);
lambda = (Supplier<String>) callSite.getTarget().invokeExact(student);
}
@Benchmark
public void directAccess(Blackhole bh) {
String name = student.name();
bh.consume(name);
}
@Benchmark
public void reflection(Blackhole bh) throws InvocationTargetException, IllegalAccessException {
String name = (String) method.invoke(student);
bh.consume(name);
}
@Benchmark
public void methodHandles(Blackhole bh) throws Throwable {
String name = (String) methodHandle.invokeExact(student);
bh.consume(name);
}
@Benchmark
public void lambdaMetafactory(Blackhole bh) {
String name = lambda.get();
bh.consume(name);
}
}