自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring MVC 與 Spring Webflux 性能測(cè)試

開(kāi)發(fā) 架構(gòu)
通過(guò)以上結(jié)果,很容易得出結(jié)論,Spring Webflux(響應(yīng)式編程)確實(shí)比 Spring Boot(線程池)帶來(lái)了一些顯著的性能優(yōu)勢(shì)。Spring Webflux 在資源成本相當(dāng)?shù)那闆r下提供大約兩倍的 RPS。

本文翻譯自國(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。

責(zé)任編輯:武曉燕 來(lái)源: waynblog
相關(guān)推薦

2023-11-02 18:01:24

SpringMVC配置

2020-03-24 09:54:57

SpringMVCWebFlux

2024-01-10 09:59:19

虛擬線程信息

2022-07-04 09:15:10

Spring請(qǐng)求處理流程

2023-02-09 08:01:12

核心組件非阻塞

2022-11-04 08:39:46

SpringWebFlux

2019-03-04 08:48:23

Spring WebFJavaIO

2020-07-07 07:00:00

Spring WebFREST APIReactive AP

2024-03-06 07:52:21

Spring框架響應(yīng)式編程微服務(wù)架構(gòu)

2022-09-26 08:54:39

Spring函數(shù)式編程

2021-07-15 11:16:31

Spring WebWebFlux架構(gòu)

2020-05-25 07:00:00

雙因素認(rèn)證身份認(rèn)證密碼

2021-06-13 08:55:25

Spring NatiWebFluxJVM

2012-06-17 20:19:29

2017-07-20 09:23:32

Springjavacio

2011-12-05 13:44:34

JavaSpringMVC

2009-06-11 10:37:58

netbeans spMVC基礎(chǔ)

2009-06-22 11:54:28

Spring MVCSpringframe

2011-05-24 09:22:44

Spring3異常處理

2023-07-10 08:00:13

架構(gòu)Rest返回值
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)