Friday, 8 June 2007

Spring prototypes and auto-wire byType are expensive

In designing a new, very performance-sensitive part of our systems we investigated the runtime performance of retrieving beans (singleton and prototype) from a Spring bean factory versus creating them via a hand-coded factory. For the Spring code we also measured any additional overhead of auto-wiring beans and doing dependency checks on beans.

The test repeatedly retrieves 5 beans which are the roots of a highly interconnected object graph (comprising 4 other beans) from a Spring bean factory. In the prototype tests each bean in that graph is a Spring prototype bean, i.e. a new instance is create whenever a bean is needed from the bean factory. In the singleton tests each bean in that graph is a Spring singleton and so the same instance is returned every time a bean is retrieved. By comparison, the hand-coded factory always creates each object in that graph and hence behaves identical to the Spring prototype test.

The results give the time in nanoseconds for retrieving a bean (which is the root of the object graph) from the factory:

Bean retrieval / nanosecondsSpring 2.0.5Spring 2.0.4Spring 2.0.3Spring 2.0.2
Spring, prototype, autowire=byType, depend check442365500050945551958805
Spring, prototype, autowire=byName,
depend check
Spring, prototype, no autowire, depend check172412173539624171639640
Spring, prototype, no autowire,no depend check162300162950586144600320
Spring, singleton, autowire=byType, depend check60884110751134
Spring, singleton, autowire=byName,depend check71084010691132
Spring, singleton, no autowire,depend check60984210971161
Spring, singleton, no autowire, no depend check61483711021135
No Spring (hand-coded factory),always create (prototype)284284288292
The numbers speak for themselves, but the shown figures visualise them.

The code we used to perform these measurements and all results are available here.