google / guice

Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 11 and above, brought to you by Google.
https://github.com/google/guice
Apache License 2.0
12.51k stars 1.67k forks source link

Guice Servlet should allow for on-demand instantiation of servlets #505

Open gissuebot opened 10 years ago

gissuebot commented 10 years ago

From mmastrac on June 24, 2010 18:10:11

When using guice-servlet on AppEngine, you can incur a startup penalty on the first request as Guice attempts to instantiate all bound servlets and dependencies, potentially forcing a large number of class loads.

It would be useful if guice-servlet allowed a configuration mode where servlets were loaded on-demand. This would let us amortize the startup costs of the application over the first few requests, rather than paying it all upfront on the first request.

Original issue: http://code.google.com/p/google-guice/issues/detail?id=505

gissuebot commented 10 years ago

From dhanji on July 05, 2010 00:26:56

What would the advantage of this be? I think the first request is really where you want to take the hit, correct? I mean, I can only think of development speed being an issue, but you develop mostly on the local devserver anyway.

The reason I say this is that it's quite complex to split up the lifecycle this way--particularly because servlets can depend on each other (how do I know it has been initialized if it is called from another servlet?)

gissuebot commented 10 years ago

From mmastrac on July 05, 2010 10:05:06

When running on AppEngine, the first request after a JVM shutdown always includes this Guice startup time.  Unfortunately, for low-volume applications, this first request can happen as often as once per minute. In the case of our application, the total time to run the Guice module code, load all required classes from everything in the module, bind the servlets and serve the first request ends up being around 10s. The end result of this is that the first person to hit our application every minute basically has to wait 10s for it to come alive.

I used YourKit and Guice's stopwatch logs to profile my application's startup and there's not much going on there beyond simple classloading. I hacked up a filter that lived in front of GuiceFilter (and subclassed GuiceFilter to lazily initialize the module on the first request rather than using a servlet context listener). This filter manages to serve the first request in under 3s. This is probably around the fastest you can get a JVM to come back to life on AppEngine.

I believe that solving this would cut down our application's startup time by approximately 50% by not initializing every class to serve the first request (which is likely the homepage and can be served by memcache in most cases).

gissuebot commented 10 years ago

From sberlin on February 21, 2011 17:44:13

(No comment was entered for this change.)

Labels: Extension-Servlet