Spring Boot 2.0 |
Spring Boot 2.1 |
TL;DR How do I make my app go faster?
spring-context-indexer
spring.config.location
spring.jmx.enabled=false
)-noverify
. Also consider -XX:TieredStopAtLevel=1
Static benchmarks from https://github.com/dsyer/spring-boot-startup-bench. New JVM (separate process) per application startup, explicit classpath (not fat jar).
class method sample beans classes heap memory median mean range
MainBenchmark main demo 121.000 5643.000 13.360 76.726 0.935 0.948 0.017
MainBenchmark main jdbc 156.000 5794.000 14.137 78.331 1.008 1.017 0.012
MainBenchmark main actr 222.000 6182.000 15.569 83.695 1.140 1.155 0.020
StripBenchmark strip slim 103.000 5465.000 13.815 76.140 0.875 0.897 0.041
StripBenchmark strip thin 62.000 5247.000 12.810 73.781 0.810 0.827 0.023
StripBenchmark strip lite 30.000 5012.000 11.432 70.928 0.715 0.727 0.012
StripBenchmark strip func 26.000 4967.000 11.351 70.516 0.686 0.701 0.026
class method sample beans classes heap memory median mean range
MainBenchmark main demo 93.000 4365.000 8.024 49.564 0.766 0.773 0.011
MainBenchmark main jlog 80.000 3598.000 6.141 43.006 0.667 0.679 0.019
MiniBenchmark boot jlog 28.000 3336.000 7.082 41.949 0.588 0.597 0.014
MiniBenchmark mini jlog 27.000 3059.000 5.487 38.953 0.534 0.545 0.018
MiniBenchmark micro jlog 2.000 2176.000 4.608 32.886 0.336 0.345 0.013
-noverify
-XX:TieredStopAtLevel=1
-Djava.security.egd=file:/dev/./urandom
-Xquickstart -Xshareclasses -Xscmx128m
YMMV, but consider excluding:
spring-boot-starter-json
)spring-boot-starter-logging
)org.hibernate.validator:hibernate-validator
)spring-boot-starter-actuator
)@EnableJpaRepositories(bootstrapMode=BootstrapMode.LAZY)
or spring.data.jpa.repositories.bootstrap-mode=lazy
@PostConstruct
spring.main.lazy-initialization=true
Choose your own autoconfigurations: "a la carte" instead of "all you can eat".
@SpringBootConfiguration
@ImportAutoConfiguration({
WebFluxAutoConfiguration.class,
ReactiveWebServerFactoryAutoConfiguration.class,
ErrorWebFluxAutoConfiguration.class,
HttpHandlerAutoConfiguration.class,
ConfigurationPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class
})
@RestController
public class DemoApplication {
...
}
Before:
@Configuration
public class SampleConfiguration {
@Bean
public Foo foo() {
return new Foo();
}
@Bean
public Bar bar(Foo foo) {
return new Bar(foo);
}
}
After:
public class SampleConfiguration
implements ApplicationContextInitializer<GenericApplicationContext> {
public Foo foo() {
return new Foo();
}
public Bar bar(Foo foo) {
return new Bar(foo);
}
@Override
public void initialize(GenericApplicationContext context) {
context.registerBean(SampleConfiguration.class);
context.registerBean(Foo.class,
() -> context.getBean(SampleConfiguration.class).foo());
context.registerBean(Bar.class, () -> context.getBean(SampleConfiguration.class)
.bar(context.getBean(Foo.class)));
}
}
FuncApplication
and BuncApplication
): https://github.com/dsyer/spring-boot-micro-appsspring.functional.enabled=true
container cpus startup(ms)
===========================
riff 4 2817
scf 4 664
riff 2 4614
scf 2 653
riff 1 16782
scf 1 2121
scf:n 1 1091
Native images can be very efficient (see Graal VM).
$ native-image -H:Name=target/bunc ... com.example.func.BuncApplication
$ ./target/bunc
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::
...
Aug 07, 2018 11:25:13 AM org.springframework.boot.web.embedded.netty.NettyWebServer start
INFO: Netty started on port(s): 8080
Aug 07, 2018 11:25:13 AM org.springframework.boot.StartupInfoLogger logStarted
INFO: Started application in 0.036 seconds (JVM running for 0.04)
Benchmark app started
Started HttpServer: 40ms
Downside: you lose all of JVM benefits (debugging, manageability, dynamic compilation, optimization, garbage collection).
Many issues have been solved. Still work in progress. Native image feature is not part of GraalVM 19.0.0 release.
/