Olivier Grégoire
Olivier Grégoire

Reputation: 35407

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

Upvotes: 6

Views: 2970

Answers (1)

ColinD
ColinD

Reputation: 110036

  • 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.

Upvotes: 4

Related Questions