天天看點

Spring WebFlux和Spring WebMVC性能對比

這兩天在看有關WebFlux相關的,測試了一下性能,确實差距挺大的。剛開始看了網上的文章,自己測試,得到的結果并不一緻,又仔細研究了一下響應式程式設計相關的内容,應該是之前自己寫的代碼有些問題吧,記錄一下測試結果吧。

測試結果,WebFlux:

Spring WebFlux和Spring WebMVC性能對比
Spring WebFlux和Spring WebMVC性能對比
Spring WebFlux和Spring WebMVC性能對比
Spring WebFlux和Spring WebMVC性能對比
Spring WebFlux和Spring WebMVC性能對比

WebMVC:

Spring WebFlux和Spring WebMVC性能對比
Spring WebFlux和Spring WebMVC性能對比

結果說明:

對比測試結果,WebFlux的優勢還是非常明顯的吞吐量相差了一倍多,95%響應時間幾乎有數量級的差距了。

測試配置:

Spring WebFlux和Spring WebMVC性能對比

編譯後的應用放在相同的Linux伺服器上進行的,下面的代碼。

WebFlux主要代碼:

@GetMapping("/")
    public Mono<String> hello()
    {
        log.info("Hello");
        return Mono.just("Hello WebFlux!").publishOn(Schedulers.elastic()).map(s->{return this.service.test(s);});
    }
           
public String test(String s)
    {
        log.info("Service::Test, the args: {}",s);
        try
        {
            TimeUnit.MILLISECONDS.sleep(200);
        }
        catch (Exception ignored){}
        return s.toUpperCase();
    }
           

WebMVC主要代碼:

@GetMapping("/")
    public String hello()
    {
        return this.service.test("Hello WebFlux!");
    }
           
public String test(String s)
    {
        try
        {
            TimeUnit.MILLISECONDS.sleep(200);
        }
        catch (Exception ignored){}
        return s.toUpperCase();
    }