并发编程-springboot的tomcat和netty性能初步对比

背景

spring boot2.0以后,web容器类型增加了webflux(使用了netty nio框架)。都说nio快,而且netty的nio更快(tomcat 8以后也引入了nio,但性能并不是最优)。

现在来通过简单的helloworld测试一下性能对比

环境

cpu:i7 7700
内存:32G 2400hz
硬盘:ssd 读取速度 3.2G,写入速度2.4G(西数n750)

压测软件apach-ab(不管是哪个压测软件,需要对自己的系统限制进行设置,比如说windows和mac都对系统资源有使用限制,这个需要根据系统去进行相应的设置)
博主使用的是linux系统,可以直接更改/etc/system.conf进行调整。
更改为:

fs.file-max=65535
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
#net.ipv4.tcp_tw_reuse = 1
#net.ipv4.tcp_mem = 94500000 915000000 927000000
#net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_tw_recycle = 1
#net.ipv4.ip_local_port_range = 1024 65000
#net.nf_conntrack_max = 6553500
#net.netfilter.nf_conntrack_max = 6553500
#net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
#net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
#net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
#net.netfilter.nf_conntrack_tcp_timeout_established = 3600
#net.ipv4.neigh.default.gc_stale_time=120
#net.ipv4.conf.default.rp_filter=0
#net.ipv4.conf.all.rp_filter=0
#net.ipv4.conf.all.arp_announce=2
#net.ipv4.conf.lo.arp_announce=2
#vm.max_map_count=655360

其中你的参数可以通过度娘科普一番

代码实现

非常简单,就是写一个controller,直接返回hello

@RestController
public class DemoController {

    @RequestMapping("/hello")
    public Object hello(Map map) {
        return "hello";
    }
}

使用的spring boot版本:

org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE

性能对比

web-tomcat

pom依赖如下


      
          org.springframework.boot
          spring-boot-starter-webflux
      

压测结果如下:

ab -c 500 -n 500000 http://127.0.0.1:8080/hello

This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 50000 requests
Completed 100000 requests
Completed 150000 requests
Completed 200000 requests
Completed 250000 requests
Completed 300000 requests
Completed 350000 requests
Completed 400000 requests
Completed 450000 requests
Completed 500000 requests
Finished 500000 requests


Server Software:        
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /hello
Document Length:        5 bytes

Concurrency Level:      500
Time taken for tests:   17.597 seconds
Complete requests:      500000
Failed requests:        0
Total transferred:      68500000 bytes
HTML transferred:       2500000 bytes
Requests per second:    28413.76 [#/sec] (mean)  // 重点看下这里2.8w的qps
Time per request:       17.597 [ms] (mean)
Time per request:       0.035 [ms] (mean, across all concurrent requests)
Transfer rate:          3801.45 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   11  85.5      4    1042 // 连接耗时,max比较长,是1s多
Processing:     0    6   4.2      6     215
Waiting:        0    5   4.0      5     213
Total:          1   17  86.2     10    1249 // total耗时的max很长,1s多

Percentage of the requests served within a certain time (ms)
  50%     10
  66%     12
  75%     13
  80%     14
  90%     16
  95%     17
  98%     18
  99%     19
 100%   1249 (longest request) // 到秒级了

web-flux

pom配置

        
        
            org.springframework.boot
            spring-boot-starter-webflux
        

压测结果如下:
ab -c 500 -n 500000 http://127.0.0.1:8080/hello

This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 50000 requests
Completed 100000 requests
Completed 150000 requests
Completed 200000 requests
Completed 250000 requests
Completed 300000 requests
Completed 350000 requests
Completed 400000 requests
Completed 450000 requests
Completed 500000 requests
Finished 500000 requests


Server Software:        
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /hello
Document Length:        5 bytes

Concurrency Level:      500
Time taken for tests:   17.448 seconds
Complete requests:      500000
Failed requests:        0
Total transferred:      41500000 bytes
HTML transferred:       2500000 bytes
Requests per second:    28657.20 [#/sec] (mean) 
Time per request:       17.448 [ms] (mean)
Time per request:       0.035 [ms] (mean, across all concurrent requests)
Transfer rate:          2322.80 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    7   0.8      7      10
Processing:     0   10   1.7     11      24
Waiting:        0    8   1.9      8      22
Total:          0   17   1.7     17      30

Percentage of the requests served within a certain time (ms)
  50%     17
  66%     18
  75%     18
  80%     19
  90%     20
  95%     20
  98%     21
  99%     22
 100%     30 (longest request)

评价

tomcat和netty的对比如下:

  • qps类似,都在2.8w左右
  • 最长响应时间,netty的明显变短,在连接的时间上非常有优势

在小数据的请求上,netty优于tomcat

你可能感兴趣的:(并发编程)