cglib 與 JDK動態代理的執行效能比較
都說 Cglib 建立的動態代理的執行效能比 JDK 動態代理能高出大概 10 倍,今日抱著懷疑精神驗證了一下,發現情況有所不同,遂貼出實驗結果,以供參考和討論。
程式碼很簡單,首先,定義一個 Test 介面,和一個實現 TestImpl 。Test 介面僅定義一個方法 test,對傳入的 int 引數加 1 後返回。程式碼如下:
package my.test; public interface Test { public int test(int i); }
package my.test; public class TestImpl implementsTest{ public int test(int i) { return i+1; } }
然後,定義了三種代理的實現:裝飾者模式實現的代理(decorator),JDK 動態代理(dynamic proxy) 和 Cglib 動態代理 (cglib proxy)。程式碼如下:
package my.test; public class DecoratorTest implements Test{ private Test target; public DecoratorTest(Test target) {this.target = target; } public int test(int i) { return target.test(i); } }
package my.test; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class DynamicProxyTest implements InvocationHandler { privateTest 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); } }
package my.test; import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; 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); } }
以 TestImpl 的呼叫耗時作為基準,對比通過其它三種代理進行呼叫的耗時。測試程式碼如下:
package my.test; import java.util.LinkedHashMap; import java.util.Map; 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(cglibProxy, preRunCount); runWithoutMonitor(dynamicProxy, 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"); } }
測試用例分別在 jdk6、 jdk7、jdk8 下進行了測試,每次測試分別以 1,000,000 和 50,000,000 迴圈次數呼叫 test 方法,並重復3次。
- jdk6 下的測試結果如下:
==================== run test : [repeatCount=3] [runCount=1000000] [java.version=1.6.0_45] ==================== --------- test : [1] --------- [Native ] Elapsed Time:2ms [Decorator] Elapsed Time:12ms [Dynamic ] Elapsed Time:31ms [Cglib ] Elapsed Time:31ms --------- test : [2] --------- [Native ] Elapsed Time:7ms [Decorator] Elapsed Time:7ms [Dynamic ] Elapsed Time:31ms [Cglib ] Elapsed Time:27ms --------- test : [3] --------- [Native ] Elapsed Time:7ms [Decorator] Elapsed Time:6ms [Dynamic ] Elapsed Time:23ms [Cglib ] Elapsed Time:29ms ==================== run test : [repeatCount=3] [runCount=50000000] [java.version=1.6.0_45] ==================== --------- test : [1] --------- [Native ] Elapsed Time:212ms [Decorator] Elapsed Time:226ms [Dynamic ] Elapsed Time:1054ms [Cglib ] Elapsed Time:830ms --------- test : [2] --------- [Native ] Elapsed Time:184ms [Decorator] Elapsed Time:222ms [Dynamic ] Elapsed Time:1020ms [Cglib ] Elapsed Time:826ms --------- test : [3] --------- [Native ] Elapsed Time:184ms [Decorator] Elapsed Time:208ms [Dynamic ] Elapsed Time:979ms [Cglib ] Elapsed Time:832ms
測試結果表明:jdk6 下,在執行次數較少的情況下,jdk動態代理與 cglib 差距不明顯,甚至更快一些;而當呼叫次數增加之後, cglib 表現稍微更快一些,然而僅僅是“稍微”好一些,遠沒達到 10 倍差距。
- jdk7 下的測試結果如下:
==================== run test : [repeatCount=3] [runCount=1000000] [java.version=1.7.0_60] ==================== --------- test : [1] --------- [Native ] Elapsed Time:2ms [Decorator] Elapsed Time:12ms [Dynamic ] Elapsed Time:19ms [Cglib ] Elapsed Time:26ms --------- test : [2] --------- [Native ] Elapsed Time:3ms [Decorator] Elapsed Time:5ms [Dynamic ] Elapsed Time:17ms [Cglib ] Elapsed Time:20ms --------- test : [3] --------- [Native ] Elapsed Time:4ms [Decorator] Elapsed Time:4ms [Dynamic ] Elapsed Time:13ms [Cglib ] Elapsed Time:27ms ==================== run test : [repeatCount=3] [runCount=50000000] [java.version=1.7.0_60] ==================== --------- test : [1] --------- [Native ] Elapsed Time:208ms [Decorator] Elapsed Time:210ms [Dynamic ] Elapsed Time:551ms [Cglib ] Elapsed Time:923ms --------- test : [2] --------- [Native ] Elapsed Time:238ms [Decorator] Elapsed Time:210ms [Dynamic ] Elapsed Time:483ms [Cglib ] Elapsed Time:872ms --------- test : [3] --------- [Native ] Elapsed Time:217ms [Decorator] Elapsed Time:208ms [Dynamic ] Elapsed Time:494ms [Cglib ] Elapsed Time:881ms
測試結果表明:jdk7 下,情況發生了逆轉!在執行次數較少(1,000,000)的情況下,jdk動態代理比 cglib 快了差不多30%;而當呼叫次數增加之後(50,000,000), 動態代理比 cglib 快了接近1倍。
接下來再看看jdk8下的表現如何。
- jdk8 下的測試結果如下:
==================== run test : [repeatCount=3] [runCount=1000000] [java.version=1.8.0_05] ==================== --------- test : [1] --------- [Native ] Elapsed Time:5ms [Decorator] Elapsed Time:11ms [Dynamic ] Elapsed Time:27ms [Cglib ] Elapsed Time:52ms --------- test : [2] --------- [Native ] Elapsed Time:4ms [Decorator] Elapsed Time:6ms [Dynamic ] Elapsed Time:11ms [Cglib ] Elapsed Time:24ms --------- test : [3] --------- [Native ] Elapsed Time:4ms [Decorator] Elapsed Time:5ms [Dynamic ] Elapsed Time:9ms [Cglib ] Elapsed Time:26ms ==================== run test : [repeatCount=3] [runCount=50000000] [java.version=1.8.0_05] ==================== --------- test : [1] --------- [Native ] Elapsed Time:194ms [Decorator] Elapsed Time:211ms [Dynamic ] Elapsed Time:538ms [Cglib ] Elapsed Time:965ms --------- test : [2] --------- [Native ] Elapsed Time:194ms [Decorator] Elapsed Time:214ms [Dynamic ] Elapsed Time:503ms [Cglib ] Elapsed Time:969ms --------- test : [3] --------- [Native ] Elapsed Time:190ms [Decorator] Elapsed Time:209ms [Dynamic ] Elapsed Time:495ms [Cglib ] Elapsed Time:939ms
測試結果表明:jdk8 下,延續了 JDK7 下的驚天大逆轉!不過還觀察另外有一個細微的變化,從絕對值來看 cglib 在 jdk8 下的表現似乎比 jdk7 還要差一點點,儘管只是一點點,但經過反覆多次的執行仍然是這個趨勢(注:這個趨勢的結論並不嚴謹,只是捎帶一提,如需得出結論還需進行更多樣的對比實驗)。
結論:從 jdk6 到 jdk7、jdk8 ,動態代理的效能得到了顯著的提升,而 cglib 的表現並未跟上,甚至可能會略微下降。傳言的 cglib 比 jdk動態代理高出 10 倍的情況也許是出現在更低版本的 jdk 上吧。
以上測試用例雖然簡單,但揭示了 jdk 版本升級可能會帶來一些新技術改變,會使我們以前的經驗失效。放在真實業務場景下時,還需要按照實際情況進行測試後才能得出特定於場景的結論。
總之,實踐出真知,還要與時俱進地去檢視更新一些以往經驗。