Cglib 和 JDK动态代理是 Spring AOP的实现核心
这两种效率 哪个高一点
今天来看下
public interface Test {
public int test(int i);
}
//Native 原生实现
public class TestImpl implements Test{
public int test(int i) {
return i+1;
}
}
//装饰模式
public class DecoratorTest implements Test{
private Test target;
public DecoratorTest(Test target) {
this.target = target;
}
public int test(int i) {
return target.test(i);
}
}
//动态代理
public class DynamicProxyTest implements InvocationHandler {
private Test target;
private DynamicProxyTest(Test target) {
this.target = target;
}
public static Test newProxyInstance(Test target) {
return (Test) Proxy
.newProxyInstance(DynamicProxyTest.class.getClassLoader(),
new Class<?>[] { Test.class },
new DynamicProxyTest(target));
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return method.invoke(target, args);
}
}
//Cglib
public class CglibProxyTest implements MethodInterceptor {
private CglibProxyTest() {
}
public static <T extends Test> Test newProxyInstance(Class<T> targetInstanceClazz){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(targetInstanceClazz);
enhancer.setCallback(new CglibProxyTest());
return (Test) enhancer.create();
}
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj, args);
}
}
开始测试
public class ProxyPerfTester {
public static void main(String[] args) {
//创建测试对象;
Test nativeTest = new TestImpl();
Test decorator = new DecoratorTest(nativeTest);
Test dynamicProxy = DynamicProxyTest.newProxyInstance(nativeTest);
Test cglibProxy = CglibProxyTest.newProxyInstance(TestImpl.class);
//预热一下;
int preRunCount = 10000;
runWithoutMonitor(nativeTest, preRunCount);
runWithoutMonitor(decorator, preRunCount);
runWithoutMonitor(dynamicProxy, preRunCount);
runWithoutMonitor(cglibProxy, preRunCount);
//执行测试;
Map<String, Test> tests = new LinkedHashMap<String, Test>();
tests.put("Native ", nativeTest);
tests.put("Decorator", decorator);
tests.put("Dynamic ", dynamicProxy);
tests.put("Cglib ", cglibProxy);
int repeatCount = 3;
int runCount = 1000000;
runTest(repeatCount, runCount, tests);
runCount = 50000000;
runTest(repeatCount, runCount, tests);
}
private static void runTest(int repeatCount, int runCount, Map<String, Test> tests) {
System.out.println(String.format("\n==================== run test : [repeatCount=%s] [runCount=%s] [java.version=%s] ====================", repeatCount, runCount, System.getProperty("java.version")));
for (int i = 0; i < repeatCount; i++) {
System.out.println(String.format("\n--------- test : [%s] ---------", (i + 1)));
for (String key : tests.keySet()) {
runWithMonitor(tests.get(key), runCount, key);
}
}
}
private static void runWithoutMonitor(Test test, int runCount) {
for (int i = 0; i < runCount; i++) {
test.test(i);
}
}
private static void runWithMonitor(Test test, int runCount, String tag) {
long start = System.currentTimeMillis();
for (int i = 0; i < runCount; i++) {
test.test(i);
}
long end = System.currentTimeMillis();
System.out.println("[" + tag + "] Elapsed Time:" + (end - start) + "ms");
}
}
结果
==================== run test : [repeatCount=3] [runCount=1000000] [java.version=1.8.0_144] ====================
--------- test : [1] ---------
[Native ] Elapsed Time:8ms
[Decorator] Elapsed Time:14ms
[Dynamic ] Elapsed Time:223ms
[Cglib ] Elapsed Time:120ms
--------- test : [2] ---------
[Native ] Elapsed Time:7ms
[Decorator] Elapsed Time:15ms
[Dynamic ] Elapsed Time:51ms
[Cglib ] Elapsed Time:52ms
--------- test : [3] ---------
[Native ] Elapsed Time:6ms
[Decorator] Elapsed Time:6ms
[Dynamic ] Elapsed Time:50ms
[Cglib ] Elapsed Time:112ms
==================== run test : [repeatCount=3] [runCount=50000000] [java.version=1.8.0_144] ====================
--------- test : [1] ---------
[Native ] Elapsed Time:428ms
[Decorator] Elapsed Time:383ms
[Dynamic ] Elapsed Time:1975ms
[Cglib ] Elapsed Time:999ms
--------- test : [2] ---------
[Native ] Elapsed Time:194ms
[Decorator] Elapsed Time:213ms
[Dynamic ] Elapsed Time:1004ms
[Cglib ] Elapsed Time:1045ms
--------- test : [3] ---------
[Native ] Elapsed Time:182ms
[Decorator] Elapsed Time:199ms
[Dynamic ] Elapsed Time:961ms
[Cglib ] Elapsed Time:1093ms
- 本机版本是 JDK 1.8.0_144
- Cglib 使用的是 SpringBoot 包中带的 版本(5.0.6.RELEASE)
从运行结果来看 当执行1000000 次的时候
除了一开始 动态代理和Cglib 相差两倍之外
动态代理有时还快一点
当执行次数加大到50000000
动态代理比 Cglib 稍微快一点
//调整 预热 关系
==================== run test : [repeatCount=3] [runCount=1000000] [java.version=1.8.0_144] ====================
--------- test : [1] ---------
[Native ] Elapsed Time:10ms
[Decorator] Elapsed Time:16ms
[Dynamic ] Elapsed Time:79ms
[Cglib ] Elapsed Time:74ms
--------- test : [2] ---------
[Native ] Elapsed Time:5ms
[Decorator] Elapsed Time:7ms
[Dynamic ] Elapsed Time:46ms
[Cglib ] Elapsed Time:46ms
--------- test : [3] ---------
[Native ] Elapsed Time:5ms
[Decorator] Elapsed Time:5ms
[Dynamic ] Elapsed Time:23ms
[Cglib ] Elapsed Time:23ms
==================== run test : [repeatCount=3] [runCount=50000000] [java.version=1.8.0_144] ====================
--------- test : [1] ---------
[Native ] Elapsed Time:247ms
[Decorator] Elapsed Time:491ms
[Dynamic ] Elapsed Time:1725ms
[Cglib ] Elapsed Time:988ms
--------- test : [2] ---------
[Native ] Elapsed Time:209ms
[Decorator] Elapsed Time:214ms
[Dynamic ] Elapsed Time:1403ms
[Cglib ] Elapsed Time:1113ms
--------- test : [3] ---------
[Native ] Elapsed Time:198ms
[Decorator] Elapsed Time:193ms
[Dynamic ] Elapsed Time:1327ms
[Cglib ] Elapsed Time:1014ms
先预热 Cglib 然后在预热 动态代理
发现运行次数少时 一样快
然后多时 Cglib 比动态代理 300 ms 左右 大概1.3 倍
传言的 cglib 比 jdk动态代理高出10倍的情况也许是出现在更低版本的 jdk 上吧。
、
网友评论