讓SpringBoot啟動更快一點
“Spring有多快?”
這是 2018 Spring One Platform 中的一場會議。看完會議視訊,我自己動手試了一下。下面是我做出的測試結果。
還沒有觀看視訊的朋友推薦看一下,非常有意思。
ofollow,noindex" target="_blank">https://springoneplatform.io/2018/sessions/how-fast-is-spring-
本文使用的程式碼
https://github.com/bufferings/spring-boot-startup-mybench
↓我使用的是 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)
↓可以按照下面這樣執行基準測試。執行起來可能需要一些時間,下面會執行所有的測試。
❯ ./mvnw clean package ❯ (cd benchmarks/; java -jar target/benchmarks.jar)
1. FluxBaseline
↓我使用 SpringInitializr 建立專案,僅包含 Reactive Web。接下來,我會寫一個 WebMVC 風格的極簡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 版本是 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>
↓啟動結果 2.938 ± 0.287 s/op。
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op
現在,以這個結果作為基線。讓我們從這裡開始。
2. WebMVC
↓我很好奇為什麼要用 WebMVC 而不是 WebFlux? 我嘗試了一下。也許僅僅是為了比較 Tomcat 和 Netty?
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case02_Webss103.281 ± 0.342s/op
WebFlux 快了一點,難道不是嗎?
3. spring-context-indexer
接下來,我試了 spring-context-indexer,似乎建立了 component index。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-indexer</artifactId> <optional>true</optional> </dependency>
↓嗯…慢了一點點?
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case03_WithContextIndexerss103.063 ± 0.102s/op
↓我檢查了spring.components,發現只包含了一個元件。瞭解...我應該嘗試更大一點的專案,這樣才能看到效果。
# #Sun Nov 04 18:42:59 JST 2018 com.example.DemoApplication=org.springframework.stereotype.Component
4. 惰性初始化
嘗試了惰性初始化。
@Configuration public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { for (String beanName : beanFactory.getBeanDefinitionNames()) { beanFactory.getBeanDefinition(beanName).setLazyInit(true); } } }
↓檢視結果,啟動變快了一點。
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case04_WithLazyInitss102.844 ± 0.129s/op
5. NoVerify
執行加-noverify 選項:
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case05_WithNoVerifyOptionss102.582 ± 0.060s/op
啟動變得快了一點。不知道為什麼有這個結果,稍後需要仔細瞭解一下。
6. TieredStopAtLevel
執行加-XX:TieredStopAtLevel=1 選項:
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case06_WithTieredStopAtLevel1Optionss101.980 ± 0.037s/op
嗯,快多了!減少了幾乎2秒。還是不知道這個引數有什麼含義,稍後需要仔細瞭解一下。
7. 指定 SpringConfigLocation 引數
執行加 -Dspring.config.location=classpath:/application.properties 選項:
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case07_WithSpringConfigLocationOptionss103.026 ± 0.139s/op
嗯,又變慢了。
8. 關閉 JMX
執行加-Dspring.jmx.enabled=false 選項:
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case08_WithJmxDisabledOptionss102.877 ± 0.097s/op
變得快了一點。
9. 取消 Logback
從這裡開始,我開始減少函式庫。開始,取消 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>
結果如下:
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case09_WithoutLogbackss102.904 ± 0.096s/op
嗯…似乎有一點點改進?
10. 取消 Jackson
接下來是 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>
結果如下:
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case10_WithoutJacksonss102.789 ± 0.093s/op
結果變快了一點。
11. 取消 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>
結果如下:
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case11_WithoutHibernateValidatorss102.857 ± 0.084s/op
也有一點效果。
到這裡為止,不再取消函式庫了。
12. AppCDS
AppCDS (Application Class Data Sharing) 是 Oracle JDK 的一個企業版功能。OpenJDK 10 開始包含了這個功能。
看起來 AppCDS 轉儲資訊存到了一個共享壓縮檔案,所以啟動時間變短了。
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case12_WithAppCdsss102.957 ± 0.079s/op
嗯…並沒有變快…然後我閱讀了CDS的相關文章,找到原因了。
SpringBoot FatJAR 不在 CDS 管理範圍內。
13. 使用 Thin Launcher 的 Flux
嗯,對不起,“Exploded” 基準測試錯了。我曾經試著使用 FatJAR,但是 CDS 不能這麼幹。所以,我轉而使用 Thin Launcher,所以 “Exploded” 就變成了 “ Thin Launche ”。
使用 CDS 以前,我會測試使用 Thin Launcher 打包 JAR 檔案的啟動速度。
<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>
儘管我使用 Thin Launcher 打包 app,但並沒有使用 Thin Launcher 啟動類,而是使用 Main class 讓啟動儘可能快一些 。
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case13_Explodedss102.476 ± 0.091s/op
嗯,快了一點,對嗎?
14. Thin Launcher + CDS
現在,我要使用 AppCDS 。
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case14_ExplodedWithAppCdsss101.535 ± 0.036s/op
喔!變得更快了!
15. 所有操作都上
最終,我把所有操作都用上。
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case15_AllAppliedss100.801 ± 0.037s/op
小於1秒鐘! (∩´∀`)∩耶
更進一步
在 Dave 的視訊中,他提到了“函式式 Bean 定義”,嘗試僅使用 Spring 不用 SpringBoot,app變得更快了。其中的道理還需要進一步瞭解。
結果:
BenchmarkModeCntScoreErrorUnits MyBenchmark.case01_FluxBaseliness102.938 ± 0.287s/op MyBenchmark.case02_Webss103.281 ± 0.342s/op MyBenchmark.case03_WithContextIndexerss103.063 ± 0.102s/op MyBenchmark.case04_WithLazyInitss102.844 ± 0.129s/op MyBenchmark.case05_WithNoVerifyOptionss102.582 ± 0.060s/op MyBenchmark.case06_WithTieredStopAtLevel1Optionss101.980 ± 0.037s/op MyBenchmark.case07_WithSpringConfigLocationOptionss103.026 ± 0.139s/op MyBenchmark.case08_WithJmxDisabledOptionss102.877 ± 0.097s/op MyBenchmark.case09_WithoutLogbackss102.904 ± 0.096s/op MyBenchmark.case10_WithoutJacksonss102.789 ± 0.093s/op MyBenchmark.case11_WithoutHibernateValidatorss102.857 ± 0.084s/op MyBenchmark.case12_WithAppCdsss102.957 ± 0.079s/op MyBenchmark.case13_Explodedss102.476 ± 0.091s/op MyBenchmark.case14_ExplodedWithAppCdsss101.535 ± 0.036s/op MyBenchmark.case15_AllAppliedss100.801 ± 0.037s/op
真的很有意思。感謝!
原文連結:dev.to 翻譯:ImportNew.com -唐尤華
譯文連結:[]