11 points to get your Spring Boot boot faster

Foreword
Using OpenJDK 11.
java --version openjdk 11.0.1 2018-10-16 OpenJDK Runtime Environment 18.9 (build 11.0.1+13) OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
According to this benchmark test run below. It may take some time to run, will perform all of the following tests.
./mvnw clean package (cd benchmarks/; java -jar target/benchmarks.jar)
FluxBaseline
Use SpringInitializr create a project that contains only Reactive Web. Next, I will write a WebMVC minimalist style controller.
@SpringBootApplication @RestController public class DemoApplication { @GetMapping("/") public String home() { return "Hello"; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Spring Boot version is 2.1.0.RELEASE.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
Results Start 2.938 ± 0.287 s / op.
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op
Now, with this result as a baseline. Let's start from here.
WebMVC
I'm curious why should WebMVC instead WebFlux? I tried it. Perhaps just for comparison Tomcat and Netty?
Benchmark mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s / ss on MyBenchmark.case02_Web 10 3.281 ± 0.342 s / on
WebFlux a little faster, does not it?
spring-context-indexer
Next, I tried the spring-context-indexer, seems to create a component index.
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-indexer</artifactId> <optional>true</optional> </dependency>
Ah ... a little slow?
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case03_WithContextIndexer ss 10 3.063 ± 0.102 s/op
I checked spring.components, found to contain only one component. ... I should try to understand a little more of the project, in order to see results.
# #Sun Nov 04 18:42:59 JST 2018 com.example.DemoApplication=org.springframework.stereotype.Component
Lazy initialization
Try the lazy initialization.
@Configuration public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { for (String beanName : beanFactory.getBeanDefinitionNames()) { beanFactory.getBeanDefinition(beanName).setLazyInit(true); } } }
See the results, start a little faster.
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case04_WithLazyInit ss 10 2.844 ± 0.129 s/op
NoVerify
Run plus -noverify options:
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case05_WithNoVerifyOption ss 10 2.582 ± 0.060 s/op
It started to become a little faster. I do not know why this result, you need to take a closer look later.
TieredStopAtLevel
Run add -XX: TieredStopAtLevel = 1 option:
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case06_WithTieredStopAtLevel1Option ss 10 1.980 ± 0.037 s/op
Ah, much faster! Decreased by almost two seconds. Still do not know what the meaning of this parameter, you need to take a closer look later.
Parameters specified SpringConfigLocation
Run plus -Dspring.config.location = classpath: /application.properties options:
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case07_WithSpringConfigLocationOption ss 10 3.026 ± 0.139 s/op
Ah, but also slower.
Close JMX
Run plus -Dspring.jmx.enabled = false options:
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case08_WithJmxDisabledOption ss 10 2.877 ± 0.097 s/op
It becomes a little faster.
Cancel Logback
From here on, I began to reduce library. Start, cancel Logback:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-logging</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> </dependency>
The results are as follows:
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case09_WithoutLogback ss 10 2.904 ± 0.096 s/op
Ah ... it seems there is a little improvement?
Cancel Jackson
Followed by Jackson
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-json</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency>
The results are as follows:
Benchmark mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s / ss on MyBenchmark.case10_WithoutJackson 10 2.789 ± 0.093 s / on
The results become a little faster.
Cancel HibernateValidator
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <exclusions> <exclusion> <artifactId>hibernate-validator</artifactId> <groupId>org.hibernate.validator</groupId> </exclusion> </exclusions> </dependency>
The results are as follows:
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case11_WithoutHibernateValidator ss 10 2.857 ± 0.084 s/op
There is also little effect.
Up to this point, no longer cancel the library.
AppCDS
AppCDS (Application Class Data Sharing) is an enterprise version of Oracle JDK functionality. OpenJDK 10 are beginning to include this feature.
Looks AppCDS dump information saved to a shared file compression, so the start-up time shorter.
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case12_WithAppCds ss 10 2.957 ± 0.079 s/op
Ah ... and faster ... then I have not read the relevant articles of the CDS, find the reasons.
SpringBoot FatJAR CDS is not within the scope of management.
Thin Launcher use of Flux
Ah, I'm sorry, "Exploded" benchmark wrong. I tried to use FatJAR, but the CDS can not do it. So, I turned to Thin Launcher, so "Exploded" becomes "Thin Launche".
Use CDS before, I will test the use of Thin Launcher packaged JAR file startup speed.
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot.experimental</groupId> <artifactId>spring-boot-thin-layout</artifactId> <version>1.0.15.RELEASE</version> </dependency> </dependencies> </plugin> </plugins>
Although I use Thin Launcher packaged app, but did not use Thin Launcher to start classes, but the use of Main class lets start some as fast as possible.
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case13_Exploded ss 10 2.476 ± 0.091 s/op
Ah, a little faster, right?
Thin Launcher + CDS
Now, I want to use AppCDS.
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case14_ExplodedWithAppCds ss 10 1.535 ± 0.036 s/op
Oh! Become faster!
All operations on
In the end, I have to spend all operations.
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case15_AllApplied ss 10 0.801 ± 0.037 s/op
Less than 1 second! (∩'∀ `) ∩ yeah
Further
In Dave's video, he referred to the "Bean-style function definitions" do not try to use only Spring SpringBoot, app becomes faster. Which further understanding of the truth.
result:
Benchmark mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s / ss on MyBenchmark.case02_Web 10 3.281 ± 0.342 s / ss on MyBenchmark.case03_WithContextIndexer 10 3.063 ± 0.102 s / ss on MyBenchmark.case04_WithLazyInit 10 2.844 ± 0.129 s / on MyBenchmark.case05_WithNoVerifyOption ss 10 2.582 ± 0.060 s / ss on MyBenchmark.case06_WithTieredStopAtLevel1Option 10 1.980 ± 0.037 s / ss on MyBenchmark.case07_WithSpringConfigLocationOption 10 3.026 ± 0.139 s / ss on MyBenchmark.case08_WithJmxDisabledOption 10 2.877 ± 0.097 s / ss on MyBenchmark.case09_WithoutLogback 10 2,904 ± 0.096 s / ss on MyBenchmark.case10_WithoutJackson 10 2.789 ± 0.093 s / ss on MyBenchmark.case11_WithoutHibernateValidator 10 2.857 ± 0.084 s / ss on MyBenchmark.case12_WithAppCds 10 2.957 ± 0.079 s / ss on MyBenchmark.case13_Exploded 10 2.476 ± 0.091 s / on MyBenchmark ,case14_ExplodedWithAppCds ss 10 1.535 ± 0.036 s/op MyBenchmark.case15_AllApplied ss 10 0.801 ± 0.037 s/op
Really interesting. thank!
 
At last
Welcome to share with everyone, like the point of a praise yo remember the article, thanks for the support

Guess you like

Origin www.cnblogs.com/zhuifeng523/p/11511605.html