javascript
原始性能数字– Spring Boot 2 Webflux与Spring Boot 1
我對(duì)性能測(cè)試的設(shè)置如下:
示例應(yīng)用程序公開(kāi)了一個(gè)端點(diǎn)(/ passthrough / message),該端點(diǎn)又調(diào)用下游服務(wù)。 到端點(diǎn)的請(qǐng)求消息如下所示:
{"id": "1","payload": "sample payload","delay": 3000 }下游服務(wù)將基于消息中的“延遲”屬性(以毫秒為單位)進(jìn)行延遲。
Spring Boot 1應(yīng)用程序
我已經(jīng)將Spring Boot 1.5.8.RELEASE用于該應(yīng)用程序的Boot 1版本。 端點(diǎn)是一個(gè)簡(jiǎn)單的Spring MVC控制器,它依次使用Spring的RestTemplate進(jìn)行下游調(diào)用。 一切都是同步且阻塞的,我已使用默認(rèn)的嵌入式Tomcat容器作為運(yùn)行時(shí)。 這是下游調(diào)用的原始代碼:
public MessageAck handlePassthrough(Message message) {ResponseEntity<MessageAck> responseEntity = this.restTemplate.postForEntity(targetHost + "/messages", message, MessageAck.class);return responseEntity.getBody(); }Spring Boot 2應(yīng)用程序
該應(yīng)用程序的Spring Boot 2版本公開(kāi)了一個(gè)基于Spring Webflux的終結(jié)點(diǎn),并使用WebClient ,這是RestTemplate的新的非阻塞,反應(yīng)性替代方法,可以進(jìn)行下游調(diào)用–我也使用Kotlin進(jìn)行了實(shí)現(xiàn),這與性能無(wú)關(guān)。 運(yùn)行時(shí)服務(wù)器為Netty :
import org.springframework.http.HttpHeaders import org.springframework.http.MediaType import org.springframework.web.reactive.function.BodyInserters.fromObject import org.springframework.web.reactive.function.client.ClientResponse import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.bodyToMono import org.springframework.web.reactive.function.server.ServerRequest import org.springframework.web.reactive.function.server.ServerResponse import org.springframework.web.reactive.function.server.bodyToMono import reactor.core.publisher.Monoclass PassThroughHandler(private val webClient: WebClient) {fun handle(serverRequest: ServerRequest): Mono<ServerResponse> {val messageMono = serverRequest.bodyToMono<Message>()return messageMono.flatMap { message ->passThrough(message).flatMap { messageAck ->ServerResponse.ok().body(fromObject(messageAck))}}}fun passThrough(message: Message): Mono<MessageAck> {return webClient.post().uri("/messages").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).body(fromObject<Message>(message)).exchange().flatMap { response: ClientResponse ->response.bodyToMono<MessageAck>()}} }性能測(cè)試的詳細(xì)信息
測(cè)試很簡(jiǎn)單,對(duì)于不同組的并發(fā)用戶(300、1000、1500、3000、5000),我發(fā)送了一個(gè)延遲屬性設(shè)置為300 ms的消息,每個(gè)用戶重復(fù)該場(chǎng)景30次,延遲為1-2請(qǐng)求之間的秒數(shù)。 我正在使用出色的加特林工具來(lái)生成此負(fù)載。
結(jié)果
這些是加特林捕獲的結(jié)果:
300個(gè)并發(fā)用戶:
1000個(gè)并發(fā)用戶:
1500個(gè)并發(fā)用戶:
3000個(gè)并發(fā)用戶:
5000個(gè)并發(fā)用戶:
可以預(yù)期的是,當(dāng)并發(fā)用戶數(shù)保持較低水平(例如,少于1000個(gè))時(shí),Spring Boot 1和Spring Boot 2都能很好地處理負(fù)載,并且95%的響應(yīng)時(shí)間仍比預(yù)期的300 ms延遲高出幾毫秒。
在更高的并發(fā)級(jí)別上,Spring Boot 2中的異步非阻塞IO和響應(yīng)式支持開(kāi)始顯示其顏色-即使負(fù)載非常大的5000個(gè)用戶,第95個(gè)百分位時(shí)間仍保持在312ms左右! 在這些并發(fā)級(jí)別上,Spring Boot 1記錄了很多故障和高響應(yīng)時(shí)間。
我在github回購(gòu)中提供了所有示例和加載腳本– https://github.com/bijukunjummen/boot2-load-demo。
翻譯自: https://www.javacodegeeks.com/2017/10/raw-performance-numbers-spring-boot-2-webflux-vs-spring-boot-1.html
總結(jié)
以上是生活随笔為你收集整理的原始性能数字– Spring Boot 2 Webflux与Spring Boot 1的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 蓝牙音箱选择时要注意哪几个要点
- 下一篇: Spring Boot&Angular