Monday, April 16, 2007

Competition is healthy! Prototype Spring Bean Creation: Faster than ever before ..

In my last post I had mentioned about some performance benchmarks of Guice and Spring. In one of the applications which I had ported from Spring to Guice, I had an instance of a lookup-method injection, where a singleton service bean contained a prototype bean that needed to be looked up from the context. Here is the sample configuration XML :


<bean id="trade"
  class="org.dg.misc.Trade"
  scope="prototype">
</bean>

<bean id="abstractTradingService"
  class="org.dg.misc.AbstractTradingService"
  lazy-init="true">
  <lookup-method name="getTrade" bean="trade"/>
</bean>



I ran a performance benchmark suite to exercise 10,000 gets of the prototype bean :


BeanFactory factory = new XmlBeanFactory(
    new ClassPathResource("trade_context.xml"));

ITradingService ts = (ITradingService) factory.getBean(
    "abstractTradingService");
StopWatch stopWatch = new StopWatch();
stopWatch.start("lookupDemo");

for (int x = 0; x < 10000; x++) {
  ITrade trade = ts.getTrade();
  trade.calculateValue(null, null);
}
stopWatch.stop();

System.out.println("10000 gets took " +
    stopWatch.getTotalTimeMillis() + " ms");



Spring 2.0.2 reported a timing of 359 milliseconds for the 10,000 gets. I performed the same exercise in Guice with a similar configuration :


public class TradeModule extends AbstractModule {
  @Override
  protected void configure() {
    bind(ITrade.class).to(Trade.class);
    bind(ITradingService.class).to(TradingService.class).in(Scopes.SINGLETON);
  }
}



and the corresponding test harness :


Injector injector = Guice.createInjector(new TradeModule());

long start = System.currentTimeMillis();
for(int i=0; i < 10000; ++i) {
  injector.getInstance(ITradingService.class).doTrade(injector.getInstance(ITrade.class));
}
long stop = System.currentTimeMillis();
System.out.println("10000 gets took " + (stop - start) + " ms");



For this exercise of 10,000 gets, Guice reported a timing of staggering 31 milliseconds.

Then a couple of days back Juergen Hoeller posted in the release news for Spring 2.0.4, that repeated creation of prototype bean instances has improved up to 12 times faster in this release. I decided to run the benchmark once again after a drop-in replacement of 2.0.2 jars by 2.0.4 ones. And voila ! Indeed there is a significant improvement in the figures. The same test harness now takes 109 milliseconds on the 2.0.4 jars. Looking at the changelog, you will notice several lineitems that have been addressed as part of improving bean instantiation timings.

This is what competition does even for the best .. Keep it up Spring guys ! Spring rocks !

Updated: Have a look at the comments by Bob and the followups for some more staggering benchmark results.

7 comments:

Solomon said...

Creating a competing product in a space where there already is a clear top dog is "crazy" :)

A.A.A said...

As long as spring stucks to Java 1.3 compatibiltiy, it will keep sucking.

Solomon said...

@A.A.A. I respectfully disagree. Spring can support 1.3 and still support plenty of JDK 5.0 features :)

Bob said...

Your Guice example actually isn't equivalent to your Spring example. Based on the code you've provided, it will be more fair to replace ITradingService with Provider<ITrade> in the Guice example.

Your configuration code should look like this:

  public class TradeModule extends AbstractModule {
    @Override
    protected void configure() {
      bind(ITrade.class).to(Trade.class);
    }
  }

And the test harness should look like this:

  Injector injector = Guice.createInjector(new TradeModule());
  Provider<ITrade> tradeProvider
    = injector.getProvider(ITrade.class);

  long start = System.currentTimeMillis();
  for(int i=0; i < 10000; ++i) {
    ITrade trade = tradeProvider.get();
    trade.calculateValue(null, null);
  }
  long stop = System.currentTimeMillis();
  System.out.println("10000 gets took " + (stop - start) + " ms");

If you want to keep ITradingService around, you can just inject Provider<Trade> into it.

Please let us know the new results.

Debasish said...

Hi Bob -

Thanks a lot for your suggestions. I made the following changes :

public class TradingService implements ITradingService {
  @Inject
  private Provider<ITrade> tradeProvider;

  public ITrade getTrade() {
    return tradeProvider.get();
  }
  // ..
}

and the test harness ..

Injector injector = Guice.createInjector(new TradeModule());
ITradingService ts = injector.getInstance(ITradingService.class);

long start = System.currentTimeMillis();
for(int i=0; i < 10000; ++i) {
  ITrade trade = ts.getTrade();
  trade.calculateValue(null, null);
}
long stop = System.currentTimeMillis();
System.out.println("10000 gets took " + (stop - start) + " ms");

and guess what happened !! The time reported is 16 ms. Previously it was 31 ms. But I realized that this will be a more proper benchmark than the previous one. Thanks for the suggestion and thanks a ton for Guice.

Just one point I would like to clarify from you - what is your opinion regarding annotations like @Inject coming from an external framework (I mean non-JDK) being inserted into business class ?

Thanks.
- Debasish

Bob said...

Early adopters depend on 3rd party frameworks all the time. Before JPA, we depended on Hibernate (you still have to to some extent). There's Joda time. Quartz. Log4j. With Spring, you write lots of XML which is Spring-specific.

Anonymous said...

情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,按摩棒,震動按摩棒,微調按摩棒,情趣按摩棒,逼真按摩棒,G點,跳蛋,跳蛋,跳蛋,性感內衣,飛機杯,充氣娃娃,情趣娃娃,角色扮演,性感睡衣,SM,潤滑液,威而柔,香水,精油,芳香精油,自慰套,自慰,性感吊帶襪,吊帶襪,情趣用品加盟AIO交友愛情館,情人歡愉用品,美女視訊,情色交友,視訊交友,辣妹視訊,美女交友,嘟嘟成人網,成人網站,A片,A片下載,免費A片,免費A片下載愛情公寓,情色,舊情人,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,一葉情貼圖片區,情色小說,色情,色情遊戲,情色視訊,情色電影,aio交友愛情館,色情a片,一夜情,辣妹視訊,視訊聊天室,免費視訊聊天,免費視訊,視訊,視訊美女,美女視訊,視訊交友,視訊聊天,免費視訊聊天室,情人視訊網,影音視訊聊天室,視訊交友90739,成人影片,成人交友,美女交友,微風成人,嘟嘟成人網,成人貼圖,成人電影,A片,豆豆聊天室,聊天室,UT聊天室,尋夢園聊天室,男同志聊天室,UT男同志聊天室,聊天室尋夢園,080聊天室,080苗栗人聊天室,6K聊天室,女同志聊天室,小高聊天室,上班族聊天室,080中部人聊天室,同志聊天室,聊天室交友,中部人聊天室,成人聊天室,一夜情聊天室,情色聊天室,寄情築園小遊戲情境坊歡愉用品,情境坊歡愉用品,情趣用品,成人網站,情人節禮物,情人節,AIO交友愛情館,情色,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,七夕情人節,色情,情色電影,色情網站,辣妹視訊,視訊聊天室,情色視訊,免費視訊聊天,美女視訊,視訊美女,美女交友,美女,情色交友,成人交友,自拍,本土自拍,情人視訊網,視訊交友90739,生日禮物,情色論壇,正妹牆,免費A片下載,AV女優,成人影片,色情A片,成人論壇,情趣,免費成人影片,成人電影,成人影城,愛情公寓,成人影片,保險套,舊情人,微風成人,成人,成人遊戲,成人光碟,色情遊戲,跳蛋,按摩棒,一夜情,男同志聊天室,肛交,口交,性交,援交,免費視訊交友,視訊交友,一葉情貼圖片區,性愛,視訊,視訊聊天,A片,A片下載,免費A片,嘟嘟成人網,寄情築園小遊戲,女同志聊天室,免費視訊聊天室,一夜情聊天室,聊天室