Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve Guice performance at startup

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
like image 532
Olivier Grégoire Avatar asked Oct 14 '22 17:10

Olivier Grégoire


1 Answers

  • 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.
like image 59
ColinD Avatar answered Oct 18 '22 20:10

ColinD