How to improve Guice performance at startup

不羁的心 提交于 2019-12-10 15:32:35

问题


Ok, I know my computations are not objective and so on, but anyway, I hate to wait so much time when performing my unit-tests:

My guice swing application takes about 7 seconds to initialize. It's a simple IRC client. At that moment, no connection are open, I even haven't called any java.io or java.net classes yet. I've tried to narrow down what exactly is wrong and I get that 5.8 seconds (average) are used by Guice in order to create the injector with the 2 modules I'm using (one normal module and one built with FactoryModuleBuilder, installed within the original module).

When I remove all modules (so basically calling only and exactly Guice.createInjector()), it still takes 3.5 seconds.

The version of Guice I use is the 3.0 rc2. My computer is certainly not the latest, but it is still not older than 3 years.

So how can I improve Guice's performance, if possible?


For reference, here's the main method I'm using, causing the 3.5 seconds. Subsequent calls take 0.01 second

public static void main(String[] args) {

    long t = System.currentTimeMillis();
    Injector injector = Guice.createInjector();
    long t1 = System.currentTimeMillis();
    System.out.println(((t1 - t)) / 1000.0);
}

And the result

3.578

回答1:


  • You shouldn't need to use Guice in unit tests. If you do need to use it, they probably aren't really unit tests (which test things in isolation) or you aren't using Guice correctly or both.
  • Guice definitely shouldn't be taking any number of seconds to start up unless there's something in your code causing something weird. On my machine (with 3.0 rc2) it takes a little over 100ms to create an Injector with Guice.createInjector() and no modules.


来源:https://stackoverflow.com/questions/4748405/how-to-improve-guice-performance-at-startup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!