如何让SpringBoot启动更快一点?

 

“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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值