天天看点

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();
    }