让 Spring Boot 启动更快一点
点击上方“方志朋”,选择“设为星标”
回复”666“获取新整理的面试资料
“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。
@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。
Benchmark Mode Cnt Score Error Units
MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op
现在,以这个结果作为基线。让我们从这里开始。
2. WebMVC
我很好奇为什么要用 WebMVC 而不是 WebFlux?我尝试了一下。也许仅仅是为了比较 Tomcat 和 Netty?
Benchmark Mode Cnt Score Error Units
MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op
MyBenchmark.case02_Web ss 10 3.281 ± 0.342 s/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>
嗯…慢了一点点?
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
我检查了 spring.components,发现只包含了一个组件。了解…我应该尝试更大一点的项目,这样才能看到效果。
#
#Sun Nov 04 18:42:59 JST 2018
com.example.DemoApplication=org.springframework.stereotype.Component
4. 惰性初始化
尝试了惰性初始化。
@Configuration
public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {for (String beanName : beanFactory.getBeanDefinitionNames()) {beanFactory.getBeanDefinition(beanName).setLazyInit(true);}}
}
查看结果,启动变快了一点。
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
5. NoVerify
运行加 -noverify 选项:
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
启动变得快了一点。不知道为什么有这个结果,稍后需要仔细了解一下。
6. TieredStopAtLevel
运行加 -XX:TieredStopAtLevel=1 选项:
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
嗯,快多了!减少了几乎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变得更快了。其中的道理还需要进一步了解。
结果:
Benchmark Mode Cnt Score Error Units
MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op
MyBenchmark.case02_Web ss 10 3.281 ± 0.342 s/op
MyBenchmark.case03_WithContextIndexer ss 10 3.063 ± 0.102 s/op
MyBenchmark.case04_WithLazyInit ss 10 2.844 ± 0.129 s/op
MyBenchmark.case05_WithNoVerifyOption ss 10 2.582 ± 0.060 s/op
MyBenchmark.case06_WithTieredStopAtLevel1Option ss 10 1.980 ± 0.037 s/op
MyBenchmark.case07_WithSpringConfigLocationOption ss 10 3.026 ± 0.139 s/op
MyBenchmark.case08_WithJmxDisabledOption ss 10 2.877 ± 0.097 s/op
MyBenchmark.case09_WithoutLogback ss 10 2.904 ± 0.096 s/op
MyBenchmark.case10_WithoutJackson ss 10 2.789 ± 0.093 s/op
MyBenchmark.case11_WithoutHibernateValidator ss 10 2.857 ± 0.084 s/op
MyBenchmark.case12_WithAppCds ss 10 2.957 ± 0.079 s/op
MyBenchmark.case13_Exploded ss 10 2.476 ± 0.091 s/op
MyBenchmark.case14_ExplodedWithAppCds ss 10 1.535 ± 0.036 s/op
MyBenchmark.case15_AllApplied ss 10 0.801 ± 0.037 s/op
真的很有意思。感谢!
来源:http://rrd.me/ekc64
热门内容:
接私活必备的10个开源项目!
这道字节跳动的面试题,据说多数人都不会
中台禁区:为什么最关键的组织架构却鲜少人谈?
职场老鸟的焦虑与出路
fastjson又被发现漏洞,这次危害可导致服务瘫痪!
惊呆了,Spring Boot居然这么耗内存!
微服务架构如何影响软件开发文化?
System.currentTimeMillis()竟然存在性能问题,这我能信?
团队开发中 Git 最佳实践,不给队友拖后腿!
最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。
明天见(。・ω・。)ノ♡
让 Spring Boot 启动更快一点相关推荐
- 快做这 15点,让 SpringBoot 启动更快一点!
"Spring有多快?" 这是 2018 Spring One Platform 中的一场会议.看完会议视频,我自己动手试了一下.下面是我做出的测试结果. 还没有观看视频的朋友推荐 ...
- Spring Boot 启动Banner设置--启动欢迎词!
Spring Boot启动时会显示如下图标: . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | ...
- 在Spring Boot启动时运行代码
Spring Boot会自动为我们执行很多配置,但是迟早您将不得不做一些自定义工作. 在本文中,您将学习如何进入应用程序引导生命周期并在Spring Boot启动时执行代码 . 因此,让我们看看该框架 ...
- spring boot 启动类
做项目用到spring boot 感觉spring boot用起来比较流畅.想总结一下,别的不多说,从入口开始. spring boot启动类Application.class 不能直接放在main/ ...
- 让计算机启动更快的十五招
嫌计算机启动太慢是每个计算机迷的共同心病,让计算机启动更快是大家的共同心愿,本人在使用计算机过程中总结了加快计算机启动速度的"十五式",与您分享. 一.bios的优化设置 在bio ...
- [Spring Boot] 2. Spring Boot 启动过程定制化
在上一篇文章中,从源码角度介绍了Spring Boot的启动过程.启动的代码虽然只有短短的一行,但是背后所做的工作还真不少,其中有一些可以定制化的部分,主要分为以下几个方面: 初始化器(Initial ...
- Spring Boot启动过程(七):Connector初始化
Connector实例的创建已经在Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动中提到了: Connector是LifecycleMBeanBase的子类,先是设置L ...
- Spring boot 启动后执行特定的操作
有时候我们需要在应用启动完成后执行一些特定的操作,比如: 删除一些临时文件或者Redis中的缓存 将一些字典类的数据加载到缓存,这样就不用每次去数据库中查了,有些关联数据从缓存中取得赋值就可以了,不再 ...
- Spring Boot启动过程源码分析--转
https://blog.csdn.net/dm_vincent/article/details/76735888 关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Sp ...
最新文章
- ASP.NET MVC 4 (十一) Bundles和显示模式
- codeforce 570 problem E 51Nod-1503-猪和回文
- 北京全年天气状况分析
- python中用来回溯异常的模块_python学习笔记(异常)
- Makefile学习(二)[第二版]
- JQuery选择器及radio,checkbox,select取值和反选
- Java 学习笔记·十二 —— Java 案例·网上商城系统
- unity3d 之时间
- matlab爱心函数代码画图
- AFLGO插桩代码分析记录
- 微商新传奇奢瑞小黑裙、两家小程序内测成员都来这了,SDCC2016微信开发专题议题揭晓...
- 手动更新HomeAssistant版本
- 《软件工程》实验一:可行性分析
- 【题解】DZY Loves Math
- 市场规模一年猛增24% 这个靠“打印”出来的市场正在韩国崛起
- 实验一 熟悉常用的Linux操作和Hadoop操作
- 毕业论文评审意见范例
- 安装office2007安装程序找不到office.zh-cn\*三种详细解决方法
- c#五子棋实验报告_基于c#的五子棋游戏的设计与实现毕业论文.doc
- 安装php_soap.dll,php如何安装soap扩展