Spring MVC 与 Spring Webflux 性能测试

本文介绍了Spring MVC和Spring Webflux两个Web框架,通过一个简单的hello world案例进行性能测试。在一台16G内存的MacBook Pro M1上,对两种框架分别执行500万个请求,包含不同并发测试。结果显示,Spring Webflux比Spring Boot有显著性能优势,最终Spring Webflux获胜。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

10ab111852f1ec710827fc24172c64db.jpeg

程序员的成长之路

互联网/程序员/技术/资料共享 

关注

阅读本文大概需要 3.5 分钟。

来自:51cto.com/article/765424.html

如果你已经使用 Spring 一段时间或者是编程初学者,你一定听说过使用响应式编程比传统的线程池风格更好。

自 Spring 诞生以来,开发者创建 Java 企业应用程序就变得更加容易。它提供了在企业环境中使用 Java 语言所需的一切,支持 Groovy 和 Kotlin 作为 JVM 上的替代语言,并且可以根据应用程序的需求灵活地创建多种架构。

在 Spring 4.0 以前,Spring 框架中包含的原始 Web 框架是 Spring Web MVC,它是专门为 Servlet API 和 Servlet 容器构建的。响应式 Web 框架 Spring WebFlux 是在 5.0 版本中添加的。它是完全非阻塞的,支持 Reactive Streams 背压,运行在 Netty、Undertow、Servlet 容器等服务器上。

这两个 Web 框架名称相似(spring-webmvc 和 spring-webflux),并在 Spring 框架中并存。每个模块都是可选的。应用程序可以使用其中一个模块,或者在某些情况下,同时使用两者,例如在 Spring MVC 控制器中可以使用带有响应式编程功能的 WebClient 对象。

本文将给大家介绍使用响应式编程带来的潜在性能优势。我将使用一个简单的 hello world 案例。

一、测试设置


1.1 配置

测试在一台 16G 内存的 MacBook Pro M1 上执行。

软件版本如下:

  • Go 1.20.2

  • Spring Boot 3.0.5

  • Java 17

Spring MVC 与 Spring Webflux 的两种测试总共执行 500 万个请求。

1.2 代码

Spring MVC 与 Spring Webflux 的 hello world 代码如下:

Spring Boot

传统的 Spring Boot 项目,单个 Java 文件。

package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class DemoApplication {


  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }


  @GetMapping("/")
  public String handleRequest() {
    return "Hello World!";
  }
}
Spring Webflux

与传统的 Spring Boot 项目不同,Spring Webflux 至少需要四个 Java 文件。代码如下:

package hello;


import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;


import reactor.core.publisher.Mono;


@Component
public class HelloWorldHandler {


  public Mono<ServerResponse> hello(ServerRequest request) {
    return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
      .body(BodyInserters.fromValue("Hello World!"));
  }
}

HelloWorldRouter.java

package hello;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;


import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;


@Configuration(proxyBeanMethods = false)
public class HelloWorldRouter {


  @Bean
  public RouterFunction<ServerResponse> route(HelloWorldHandler helloWorldHandler) {


    return RouterFunctions
      .route(GET("/"), helloWorldHandler::hello);
  }
}

HelloWorldClient.java

package hello;


import reactor.core.publisher.Mono;


import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;


@Component
public class HelloWorldClient {


  private final WebClient client;


  public HelloWorldClient(WebClient.Builder builder) {
    this.client = builder.baseUrl("http://localhost:3000").build();
  }


  public Mono<ClientResponse> getMessage() {
    return this.client.get()
      .uri("/")
      .exchange();
  }


}

Application.java

package hello;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;


@SpringBootApplication
public class Application {


  public static void main(String[] args) {
    ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
    HelloWorldClient helloWorldClient = context.getBean(HelloWorldClient.class);
  }
}

1.3 执行

每个测试都接受 500 万个请求执行。

测试中包含 25、100 和 300 个并发测试。

使用 Bombardier HTTP 测试工具进行负载测试。

Bombardier HTTP 是一个用 Go 编写的快速跨平台 HTTP 基准测试命令行工具。

下面是测试结果图表:

24eb7399795a39746b824bce274d28e9.png

请求耗时,越小越好。

2ba01042720008de9779ce5ff9fb10db.png

每秒请求数,越大越好。

f532c9536610d07d73b87b0051d42abb.png

响应时间/ms,越小越好。

77264d7b14579a98b58f54df6479ba62.png

中值响应时间/ms,越小越好。

d083b805365683e3585d85cb2156b181.png0d61b4189b887d9bf1a81128bdb48bb5.pngb59be57f88e4cdeaa0f14d7ce95d8bdf.png2b23b6b759ce61c3bd3cb19346adfd25.png

最大响应时间/ms,越小越好。

1d864fb1899e85d304011156708736d3.png

平均CPU占用/%,越小越好。

4d607d63f33d49b05057b66bd99d482c.png

平均内存占用/MBs,越小越好。

二、分析

通过以上结果,很容易得出结论,Spring Webflux(响应式编程)确实比 Spring Boot(线程池)带来了一些显着的性能优势。Spring Webflux 在资源成本相当的情况下提供大约两倍的 RPS。

RPS:指客户端每秒发出的请求数,有些地方也叫做 QPS。

首先由于 Spring MVC 处理这些一次性请求花费的总时间太长,Spring MVC 的平均响应时间并不是那么好。

在低并发情况下,Spring Webflux 的中值响应时间更好。高并发时 Spring Boot 更好。

随着测量值移至第三个四分位和第 90 个百分位,Spring Webflux 变得更好。即使有差异,也只有 1-2 毫秒左右。

最后

我们宣布 Spring MVC 与 Spring Webflux:hello world 性能测试案例的获胜者是 Spring Webflux。

<END>

推荐阅读:

面试官:post为什么会发送两次请求?

别再用Future了,CompletableFuture 异步多线程是真的优雅!

互联网初中高级大厂面试题(9个G)
内容包含Java基础、JavaWeb、MySQL性能优化、JVM、锁、百万并发、消息队列、高性能缓存、反射、Spring全家桶原理、微服务、Zookeeper......等技术栈!
⬇戳阅读原文领取!                                  朕已阅
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值