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 fluffy.mo.rule_jmh;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import fluffy.mo.rules.HelloIface;
import fluffy.mo.rules.HelloRunning;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import javax.script.*;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 8, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 8, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class MyGyBenchmark {
@Benchmark
public void testCompiledScript(MyState st, Blackhole blackhole) throws ScriptException {
blackhole.consume(st.groovyScript.eval(st.bindings));
}
@Benchmark
public void testCompiledScriptWithObj(MyState st, Blackhole blackhole) throws ScriptException {
blackhole.consume(st.groovyScriptWithObj.eval(st.bindings));
}
@Benchmark
public void testAlibabaQle(MyState st, Blackhole blackhole) throws Exception {
String express = "xx + yy";
blackhole.consume(st.runner.execute(express, st.context, null, true, false));
}
@Benchmark
public void testGloaderByReflect(MyState st, Blackhole blackhole) throws Exception {
blackhole.consume(st.method.invoke(st.helloGroovy, 11, 22));
}
@Benchmark
public void testGloaderByIface(MyState st, Blackhole blackhole) {
blackhole.consume(st.helloGroovy.sum(11, 22));
}
@Benchmark
public void testNativeJava(MyState st, Blackhole blackhole) {
blackhole.consume(st.helloNativeJava.sum(11, 22));
}
@Benchmark
public void testCompiledJava(MyState st, Blackhole blackhole) {
blackhole.consume(st.helloCompiledJava.sum(11, 22));
}
@State(Scope.Benchmark)
public static class MyState {
HelloIface helloNativeJava;
HelloIface helloCompiledJava;
HelloIface helloGroovy;
Method method = HelloRunning.sumMethod();
CompiledScript groovyScript;
CompiledScript groovyScriptWithObj;
ExpressRunner runner = new ExpressRunner();
// -- other
javax.script.Bindings bindings;
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
@Setup
public void myInit() {
HelloRunning myClass = new HelloRunning();
helloGroovy = myClass.getHelloGroovy();
helloNativeJava = myClass.getHelloNativeJava();
helloCompiledJava = myClass.getHelloCompiledJava();
groovyScriptWithObj = myClass.getGroovyScriptWithObj();
groovyScript = myClass.getGroovyScript();
bindings = new SimpleBindings();
bindings.put("xx", 1);
bindings.put("yy", 2);
context.put("xx", 1);
context.put("yy", 2);
System.out.println("初始化状态");
}
}
public static void main(String[] args) throws Exception {
Options opt = new OptionsBuilder()
// 导入要测试的类 -- 正则匹配
.include(MyGyBenchmark.class.getSimpleName())
.forks(2)
// 打印结果时使用的单位
.build();
new Runner(opt).run();
}
}
|