前言
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)
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>
Benchmark Mode Cnt Score Error Units MyBenchmark.case01_FluxBaseline ss 10 2.938 ± 0.287 s/op
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
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
惰性初始化
@Configuration public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public 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
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
启动变得快了一点。不知道为什么有这个结果,稍后需要仔细了解一下。
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秒。还是不知道这个参数有什么含义,稍后需要仔细了解一下。
指定 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
关闭 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
取消 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
取消 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
取消 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
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 管理范围内。
使用 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
Thin Launcher + CDS
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
所有操作都上
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
更进一步
在 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
最后
- 让Spring Boot启动更快
关注公众号:锅外的大佬, 每日推送国外技术好文,帮助每位开发者更好成长 原文链接:https://dev.to/bufferings/lets-make-springboot-app-start-fa ...
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
- Spring Boot启动原理解析
Spring Boot启动原理解析http://www.cnblogs.com/moonandstar08/p/6550758.html 前言 前面几章我们见识了SpringBoot为我们做的自动配置 ...
- Spring Boot启动流程分析
引言 早在15年的时候就开始用spring boot进行开发了,然而一直就只是用用,并没有深入去了解spring boot是以什么原理怎样工作的,说来也惭愧.今天让我们从spring boot启动开始 ...
- Spring boot 启动图片
生成工具:http://patorjk.com/software/taag/#p=testall&h=0&v=0&f=Graffiti&t=Type%20Somethi ...
- Spring Boot -- 启动彩蛋
使用Spring Boot启动的jar包总是会显示一个Spring的图标: . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\_ ...
- Spring Boot 启动原理分析
https://yq.aliyun.com/articles/6056 转 在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启 ...
- Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动
之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...
- Spring Boot启动过程(七):Connector初始化
Connector实例的创建已经在Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动中提到了: Connector是LifecycleMBeanBase的子类,先是设置L ...
随机推荐
- Python数据强制类型转换
本文链接:https://www.cnblogs.com/zyuanlbj/p/11909992.html 常用转换函数 函数 作用 int(x) 将x转换成整数类型 float(x) 将 x 转换成 ...
- 迁移桌面程序到MS Store(12)——WPF使用UWP InkToolbar和InkCanvas
我们在<迁移桌面程序到MS Store(4)——桌面程序调用Win10 API>提到了对Win10 API的调用,但仍存在无法在WPF中使用UWP控件的问题,虽然都是XAML控件,但却是两 ...
- 什么是PHP Socket?
什么是 Socket? Socket 的中文翻译过来就是“套接字”.套接字是什么,我们先来看看它的英文含义:插座. Socket 就像一个电话插座,负责连通两端的电话,进行点对点通信,让电话可以进行通 ...
- mysql安装与sqlyog安装
首先是mysql安装,参考下面两个链接 https://www.cnblogs.com/gengyufei/p/11735358.html#_label4 https://www.jb51.net/a ...
- 题解 P3954 【成绩】
题目评级: ★ (水题) 内容及算法: 无,简单模拟计算即可 代码: /** *@author little_frog */ #include <bits/stdc++.h> using ...
- mr的partition分区
1.Partitioner 组件通过让 Map 对 Key 进行分区,从而将不同分区的 Key 交由不同的 Reduce 处理.Partition属于map端 2.分区的总数与任务的reduce任务数 ...
- 程序员的进阶课-架构师之路(13)-B-树
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...
- C语言I作业003
1 本周作业头 这个作业属于哪个课程 C语言程序设计Ⅰ 这个作业要求在哪里 [班级作业链接](http://edu.cnblogs.com/campus/zswxy/SE2019-1/join?id= ...
- 使用 sroll-snap-type 优化滚动
根据 CSS Scroll Snap Module Level 1 规范,CSS 新增了一批能够控制滚动的属性,让滚动能够在仅仅通过 CSS 的控制下,得到许多原本需要 JS 脚本介入才能实现的美好交 ...
- 设置更改root密码、连接mysql、mysql常用命令
6月19日任务 13.1 设置更改root密码13.2 连接mysql13.3 mysql常用命令 13.1 设置更改root密码 使用场景:例如长时间不用忘记了mysql的root密码,那么就需要去 ...