SpringMVC异步化请求初探(续-压测效果对比)

SpringMVC异步化请求初探(续-压测效果对比)

上篇总结

上次主要介绍的Servlet3.0异步请求,SpringMVC的deferredResult对异步的封装使用,以及使用高大上的RxJava2配合SpringMVC写出优雅的代码…但是上一次压测的结果并不太好,所以我要再开一片文章,争取测出好结果。

基准数据

上次压测的数据有点问题,需要修改下系统配置然后重新测试,原因如下:

macOS has only 16K ports available that won’t be released until socket
TIME_WAIT is passed. The default timeout for TIME_WAIT is 15 seconds.
Consider reducing in case of available port bottleneck.

修改方法:

sudo sysctl -w net.inet.tcp.msl=200

同步请求代码:

    Controller:

    @GetMapping("/sayHello")
    String say() {
        return observerService.helloWorld();
    }

    Service:
        String helloWorld() {
        //throw new IllegalArgumentException();
        return "hello world";
    }

压测结果:

# tomcat 200线程
# 60秒 400并发
siege -c400 -t60s http://localhost:8080/update/sayHello

Lifting the server siege...
Transactions:              91793 hits
Availability:             100.00 %
Elapsed time:              59.81 secs
Data transferred:           0.96 MB
Response time:              0.01 secs
Transaction rate:        1534.74 trans/sec
Throughput:             0.02 MB/sec
Concurrency:               11.35
Successful transactions:       91793
Failed transactions:               0
Longest transaction:            0.12
Shortest transaction:           0.00

异步请求代码,具体请看上篇代码:


    Controller:

    @GetMapping("/sayHelloAsync")
    Single<String> sayHello() {
        return observerService.sayHello();
    }

    Service:

    Single<String> sayHello() {
        return Single
                .create((SingleOnSubscribe<String>) e -> e.onSuccess(helloWorld()))
                .subscribeOn(Schedulers.from(executor));
    }

压测结果:


# tomcat 1线程 线程池200县城
# 60秒 400并发
siege -c400 -t60s http://localhost:8080/update/sayHelloAsync

Lifting the server siege...
Transactions:              91140 hits
Availability:             100.00 %
Elapsed time:              59.82 secs
Data transferred:           0.96 MB
Response time:              0.01 secs
Transaction rate:        1523.57 trans/sec
Throughput:             0.02 MB/sec
Concurrency:               15.77
Successful transactions:       91140
Failed transactions:               0
Longest transaction:            0.25
Shortest transaction:           0.00

可以发现当业务逻辑所占时间比较小的时候,同步请求的相应速度和并发量都是占优的,但是差距并不是很大。

我们修改一下代码,增加业务逻辑的处理时长为100ms:

    Service:

    String helloWorld() throws InterruptedException {
        //throw new IllegalArgumentException();
        TimeUnit.SECONDS.sleep(100L);
        return "hello world";
    }

    Single<String> sayHello() {
        return Single
                .create((SingleOnSubscribe<String>) e -> e.onSuccess(helloWorld()))
                .subscribeOn(Schedulers.from(executor));
    }

压测结果:

siege -c100 -t1 -b http://localhost:8080/update/sayHello

Lifting the server siege...
Transactions:              44115 hits
Availability:             100.00 %
Elapsed time:              59.02 secs
Data transferred:           0.46 MB
Response time:              0.13 secs
Transaction rate:         747.46 trans/sec
Throughput:             0.01 MB/sec
Concurrency:               99.03
Successful transactions:       44115
Failed transactions:               0
Longest transaction:            0.16
Shortest transaction:           0.10

siege -c100 -t1 -b http://localhost:8080/update/sayHelloAsync

Lifting the server siege...
Transactions:              51143 hits
Availability:             100.00 %
Elapsed time:              59.70 secs
Data transferred:           0.54 MB
Response time:              0.12 secs
Transaction rate:         856.67 trans/sec
Throughput:             0.01 MB/sec
Concurrency:               99.81
Successful transactions:       51143
Failed transactions:               0
Longest transaction:            0.16
Shortest transaction:           0.10

可以发现,仅仅因为业务逻辑的处理时长增加100ms,异步请求的并发量已经明显高于同步请求。这也是异步请求性能优势。

你可能感兴趣的:(架构)