网站首页 > 开源技术 正文
作者:唐尤华 来源:http://www.importnew.com/30647.html
“Spring有多快?”
这是 2018 Spring One Platform 中的一场会议。看完会议视频,我自己动手试了一下。下面是我做出的测试结果。
还没有观看视频的朋友推荐看一下,非常有意思。
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。
现在,以这个结果作为基线。让我们从这里开始。
2. WebMVC
我很好奇为什么要用 WebMVC 而不是 WebFlux?我尝试了一下。也许仅仅是为了比较 Tomcat 和 Netty?
WebFlux 快了一点,难道不是吗?
3. spring-context-indexer
接下来,我试了 spring-context-indexer,似乎创建了 component index。
4. 惰性初始化
尝试了惰性初始化。
启动变得快了一点。不知道为什么有这个结果,稍后需要仔细了解一下。
6. TieredStopAtLevel
运行加 -XX:TieredStopAtLevel=1 选项:
嗯,快多了!减少了几乎2秒。还是不知道这个参数有什么含义,稍后需要仔细了解一下。
7. 指定 SpringConfigLocation 参数
运行加 -Dspring.config.location=classpath:/application.properties 选项:
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
嗯,又变慢了。
8. 关闭 JMX
运行加 -Dspring.jmx.enabled=false 选项:
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
变得快了一点。
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>
结果如下:
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
嗯…似乎有一点点改进?
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>
结果如下:
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op MyBenchmark.case10_WithoutJackson ss 10 2.789 ± 0.093 s/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>
结果如下:
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
也有一点效果。
到这里为止,不再取消函数库了。
12. AppCDS
AppCDS (Application Class Data Sharing) 是 Oracle JDK 的一个企业版功能。OpenJDK 10 开始包含了这个功能。
看起来 AppCDS 转储信息存到了一个共享压缩文件,所以启动时间变短了。
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
嗯…并没有变快…然后我阅读了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 让启动尽可能快一些。
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
嗯,快了一点,对吗?
14. Thin Launcher + CDS
现在,我要使用 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
喔!变得更快了!
15. 所有操作都上
最终,我把所有操作都用上。
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
小于1秒钟!(∩′?`)∩耶
更进一步
在 Dave 的视频中,他提到了“函数式 Bean 定义”,尝试仅使用 Spring 不用 SpringBoot,app变得更快了。其中的道理还需要进一步了解。
结果:
真的很有意思。感谢!
猜你喜欢
- 2024-11-18 在SpringBoot中应该避免的反模式 - Ali
- 2024-11-18 大厂面试系列(四):Spring相关
- 2024-11-18 SpringBoot入门系列(一)如何快速创建SpringBoot项目
- 2024-11-18 Spring Boot 2.4 部署你的第一个应用需要的环境
- 2024-11-18 跟着振锁撸全栈之JAVA入门 | 创建自己的第一个网站项目
- 2024-11-18 SpringBoot这玩意儿当然不能更好了
- 2024-11-18 Java基础学习路线之SpringBoot入门
- 2024-11-18 为什么 IDEA 把 Spring Boot 宠上天,这些神仙技巧告诉你答案
- 2024-11-18 Tomcat回显技术学习汇总
- 2024-11-18 年末的一波美团面试,被一波连环炮给轰了回来,做足准备来年再战
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)