Spring MVC 與 Spring Webflux 性能測試
本文翻譯自國外論壇 medium,原文地址:本文翻譯自國外論壇 medium,原文地址:https://medium.com/deno-the-complete-reference/spring-boot-vs-spring-webflux-performance-comparison-for-hello-world-case-386da4e9c418
如果你已經使用 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 案例。
測試設置
配置
測試在一臺 16G 內存的 MacBook Pro M1 上執行。
軟件版本如下:
- Go 1.20.2
- Spring Boot 3.0.5
- Java 17
Spring MVC 與 Spring Webflux 的兩種測試總共執行 500 萬個請求。
代碼
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);
}
}
執行
每個測試都接受 500 萬個請求執行。
測試中包含 25、100 和 300 個并發測試。
使用 Bombardier HTTP 測試工具進行負載測試。
Bombardier HTTP 是一個用 Go 編寫的快速跨平臺 HTTP 基準測試命令行工具。
下面是測試結果圖表,
圖片
請求耗時,越小越好
圖片
每秒請求數,越大越好
圖片
響應時間/ms,越小越好
圖片
中值響應時間/ms,越小越好
圖片
圖片
圖片
圖片
最大響應時間/ms,越小越好
圖片
平均CPU占用/%,越小越好
圖片
平均內存占用/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。