Sunday, August 10, 2008

Benchmarking the cost of dynamic proxies

I needed to quickly compare the cost of dynamic proxies versus direct access to objects, and also position it in relation to RMI. In theory we know that they add overhead, but how much? I just wanted more precise arguments to reinforce a rather obvious statement: “Dynamic proxies are not that bad. Take RMI for example...” :)

I could not find exact and up to date info saying that RMI is X times slower than direct method calls, nor dynamic proxies. In this link they’ve said that RMI is at least 1000 times slower than local calls. Since that appears to be some info from 2001, I imagine that we’ve had some nice optimizations in JVMs in the last 7 years.

I’ve changed a benchmark code that has been used in my lab, so I could have an approximate value of each method call in different approaches. This post (which actually points to here) that I’ve just read this Sunday helped me a lot in adapting the code.

My benchmark consisted of calling a million times a method in a given object which implements the tested "dummy" interface. The method was void with no parameters, and the implementation had just one line of code assigning an integer variable. The idea was to get the closest possible to the actual invocation time, with the minimum possible for method execution time.

I’ve executed the benchmark a few times and I’ve always got the same minimum values for direct calls and dynamic proxy, respectively. However the minimum time for RMI calls varied a lot.

Dynamic proxies: 1.63 times slower than direct calls, which is not bad.
RMI calls: At least 200 times slower.

For info, here is my not so performing platform:
JVM: Sun Hotspot/JRE 1.6.0 07.
OS: Windows XP SP2.
Hardware: Pentium 1.7 GHz 1GB RAM

The cost of dynamic proxies surprised me. This article from developerWorks shows similar conclusions. I thought they would be more expensive. However, we are talking about a minimal overhead level here, as in actual usages of proxies we usually add more code for decoration, verifications, etc which would obviously lead to more than the 1.63 times that I’ve measured. Well, this info provided here cannot considered sufficient as more tests would need to be performed in order to be more precise.

No comments: