Spring MVC 與 Spring Webflux 性能測(cè)試
本文翻譯自國(guó)外論壇 medium,原文地址:本文翻譯自國(guó)外論壇 medium,原文地址:https://medium.com/deno-the-complete-reference/spring-boot-vs-spring-webflux-performance-comparison-for-hello-world-case-386da4e9c418
如果你已經(jīng)使用 Spring 一段時(shí)間或者是編程初學(xué)者,你一定聽(tīng)說(shuō)過(guò)使用響應(yīng)式編程比傳統(tǒng)的線程池風(fēng)格更好。
自 Spring 誕生以來(lái),開(kāi)發(fā)者創(chuàng)建 Java 企業(yè)應(yīng)用程序就變得更加容易。它提供了在企業(yè)環(huán)境中使用 Java 語(yǔ)言所需的一切,支持 Groovy 和 Kotlin 作為 JVM 上的替代語(yǔ)言,并且可以根據(jù)應(yīng)用程序的需求靈活地創(chuàng)建多種架構(gòu)。
在 Spring 4.0 以前,Spring 框架中包含的原始 Web 框架是 Spring Web MVC,它是專(zhuān)門(mén)為 Servlet API 和 Servlet 容器構(gòu)建的。響應(yīng)式 Web 框架 Spring WebFlux 是在 5.0 版本中添加的。它是完全非阻塞的,支持 Reactive Streams 背壓,運(yùn)行在 Netty、Undertow、Servlet 容器等服務(wù)器上。
這兩個(gè) Web 框架名稱(chēng)相似(spring-webmvc 和 spring-webflux),并在 Spring 框架中并存。每個(gè)模塊都是可選的。應(yīng)用程序可以使用其中一個(gè)模塊,或者在某些情況下,同時(shí)使用兩者,例如在 Spring MVC 控制器中可以使用帶有響應(yīng)式編程功能的 WebClient 對(duì)象。
本文將給大家介紹使用響應(yīng)式編程帶來(lái)的潛在性能優(yōu)勢(shì)。我將使用一個(gè)簡(jiǎn)單的 hello world 案例。
測(cè)試設(shè)置
配置
測(cè)試在一臺(tái) 16G 內(nèi)存的 MacBook Pro M1 上執(zhí)行。
軟件版本如下:
- Go 1.20.2
- Spring Boot 3.0.5
- Java 17
Spring MVC 與 Spring Webflux 的兩種測(cè)試總共執(zhí)行 500 萬(wàn)個(gè)請(qǐng)求。
代碼
Spring MVC 與 Spring Webflux 的 hello world 代碼如下:
Spring Boot
傳統(tǒng)的 Spring Boot 項(xiàng)目,單個(gè) 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
與傳統(tǒng)的 Spring Boot 項(xiàng)目不同,Spring Webflux 至少需要四個(gè) 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);
}
}
執(zhí)行
每個(gè)測(cè)試都接受 500 萬(wàn)個(gè)請(qǐng)求執(zhí)行。
測(cè)試中包含 25、100 和 300 個(gè)并發(fā)測(cè)試。
使用 Bombardier HTTP 測(cè)試工具進(jìn)行負(fù)載測(cè)試。
Bombardier HTTP 是一個(gè)用 Go 編寫(xiě)的快速跨平臺(tái) HTTP 基準(zhǔn)測(cè)試命令行工具。
下面是測(cè)試結(jié)果圖表,
圖片
請(qǐng)求耗時(shí),越小越好
圖片
每秒請(qǐng)求數(shù),越大越好
圖片
響應(yīng)時(shí)間/ms,越小越好
圖片
中值響應(yīng)時(shí)間/ms,越小越好
圖片
圖片
圖片
圖片
最大響應(yīng)時(shí)間/ms,越小越好
圖片
平均CPU占用/%,越小越好
圖片
平均內(nèi)存占用/MBs,越小越好
分析
通過(guò)以上結(jié)果,很容易得出結(jié)論,Spring Webflux(響應(yīng)式編程)確實(shí)比 Spring Boot(線程池)帶來(lái)了一些顯著的性能優(yōu)勢(shì)。Spring Webflux 在資源成本相當(dāng)?shù)那闆r下提供大約兩倍的 RPS。
RPS:指客戶(hù)端每秒發(fā)出的請(qǐng)求數(shù),有些地方也叫做 QPS。
首先由于 Spring MVC 處理這些一次性請(qǐng)求花費(fèi)的總時(shí)間太長(zhǎng),Spring MVC 的平均響應(yīng)時(shí)間并不是那么好。
在低并發(fā)情況下,Spring Webflux 的中值響應(yīng)時(shí)間更好。高并發(fā)時(shí) Spring Boot 更好。
隨著測(cè)量值移至第三個(gè)四分位和第 90 個(gè)百分位,Spring Webflux 變得更好。即使有差異,也只有 1-2 毫秒左右。
最后
我們宣布 Spring MVC 與 Spring Webflux:hello world 性能測(cè)試案例的獲勝者是 Spring Webflux。