“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/
现在,以这个结果作为基线。让我们从这里开始。
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>