因为自己突发奇想,打算做个小网页,想简单做个技术选型,由于钱少B事多,想尽量节省点儿服务器性能,所以简单做了个性能测试。尽量能用比较小的代价做更多事情。
斟酌了下对这几个web框架做性能测试
- Nginx+lua
- Netty
- Gin
- Spring Webflux
- Spring MVC
- Flask
测试机器
虚拟机
| 内存 | cpu |
|---|---|
| 2g | 4核 |
测试工具 wrk
由于家里没有那么多资源做性能测试,并且测试不复杂,只是打算对api做性能测试,不需要做复杂的监控以及鉴权一类的逻辑处理。所以选用性能较高切资源消耗较少的工具做性能测试。
测试方法
Netty,Gin,Spring Webflux,Spring MVC 没做特别的优化,简单的helloworld程序。
Nginx简单优化 ,8 worker 用epoll模式 worker connection 65535
Flask由于单独跑性能太差,所以稍微做了一些优化用gunicorn web 容器 8个worker
承认这个测试有些不公平,比如java的运行方式就会让内存显得很大,实际使用中如果优化得当,随着请求压力增长并不会对内存带来更多的负担。但是由于测试只是为了自己的小程序考虑,并且不想把每个框架都做过深的研究。并且实际也不会有多大的访问量,所以不对上面的框架做深入学习及优化,参数一律默认。
测试结果
| 工具 | RPS(Requests/sec) | CPU | 内存 |
|---|---|---|---|
| Nginx | 73495.42 | 240% | 12.8% |
| Netty | 44248.07 | 152% | 8.4% |
| Gin | 26113.99 | 208% | 1% |
| Spring Webflux | 26767.03 | 311% | 17.1% |
| Spring MVC | 18742.23 | 269% | 13.6% |
| Flask | 2632.20 | 360% | 9.6% |
选型
因为这次主要是做网页,并没有很复杂的逻辑,预估也不会有很大的代码量,根据上面的结果Gin可能更适合,golang有不差于Python的开发效率,很高的性能并且内存cpu使用率都不是特别高。Nginx还是打算用来做前端的反向代理,由于对lua的脚本不特别熟悉所以不打算用nginx做后端api开发。
详细如下
Nginx:30 * 8=240% 内存:1.6 * 8=12.8%
[root@master ~]# wrk -t 4 -c 160 -d 20 --latency http://192.168.31.220:80/
Running 20s test @ http://192.168.31.220:80/
4 threads and 160 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 3.26ms 5.26ms 131.69ms 91.36%
Req/Sec 18.52k 3.83k 32.09k 75.82%
Latency Distribution
50% 1.63ms
75% 3.87ms
90% 7.74ms
99% 24.93ms
1473915 requests in 20.05s, 222.02MB read
Requests/sec: 73495.42
Transfer/sec: 11.07MB
Netty cpu 152% 内存 8.4%
[root@master ~]# wrk -t 4 -c 160 -d 20 --latency http://192.168.31.220:9011/
Running 20s test @ http://192.168.31.220:9011/
4 threads and 160 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 32.34ms 43.18ms 471.88ms 87.16%
Req/Sec 11.12k 3.94k 24.68k 68.25%
Latency Distribution
50% 17.13ms
75% 46.56ms
90% 86.57ms
99% 195.82ms
887301 requests in 20.05s, 96.47MB read
Requests/sec: 44248.07
Transfer/sec: 4.81MB
golang Gin 208% 内存1%
[root@master ~]# wrk -t 4 -c 160 -d 20 --latency http://192.168.31.220:8080/v1/
Running 20s test @ http://192.168.31.220:8080/v1/
4 threads and 160 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 7.19ms 6.72ms 83.81ms 77.29%
Req/Sec 6.58k 0.89k 15.31k 73.84%
Latency Distribution
50% 5.63ms
75% 9.58ms
90% 15.72ms
99% 31.39ms
524407 requests in 20.08s, 64.01MB read
Requests/sec: 26113.99
Transfer/sec: 3.19MB
Spring Webflux cpu 311% 17.1%
[root@master ~]# wrk -t 4 -c 160 -d 20 --latency http://192.168.31.220:8080/
Running 20s test @ http://192.168.31.220:8080/
4 threads and 160 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 6.32ms 4.69ms 51.61ms 79.12%
Req/Sec 6.74k 1.26k 12.89k 73.10%
Latency Distribution
50% 4.73ms
75% 8.13ms
90% 12.43ms
99% 23.59ms
537664 requests in 20.09s, 38.97MB read
Requests/sec: 26767.03
Transfer/sec: 1.94MB
Spring MVC cpu 269% 内存 13.6%
[root@master ~]# wrk -t 4 -c 160 -d 20 --latency http://192.168.31.220:8080/
Running 20s test @ http://192.168.31.220:8080/
4 threads and 160 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 8.60ms 4.79ms 82.16ms 76.73%
Req/Sec 4.72k 742.19 8.25k 71.28%
Latency Distribution
50% 7.88ms
75% 10.65ms
90% 14.37ms
99% 25.02ms
376002 requests in 20.06s, 44.89MB read
Requests/sec: 18742.23
Transfer/sec: 2.24MB
Flask cpu:45 * 8=360% 内存:1.2 * 8=9.6%
[root@master ~]# wrk -t 4 -c 160 -d 20 --latency http://192.168.31.220:5000/
Running 20s test @ http://192.168.31.220:5000/
4 threads and 160 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 50.66ms 16.06ms 297.32ms 94.49%
Req/Sec 662.43 99.26 0.95k 72.51%
Latency Distribution
50% 47.95ms
75% 52.80ms
90% 60.01ms
99% 101.04ms
52925 requests in 20.11s, 8.68MB read
Requests/sec: 2632.20
Transfer/sec: 442.13KB
单核机器
测试机器
虚拟机
内存 2g
cpu 1核
Netty
[root@master ~]# wrk -t 20 -c 20 -d 50 --latency http://192.168.31.220:9011/
Running 50s test @ http://192.168.31.220:9011/
20 threads and 20 connections
^C Thread Stats Avg Stdev Max +/- Stdev
Latency 1.17ms 521.86us 16.70ms 86.18%
Req/Sec 1.60k 47.22 1.90k 79.58%
Latency Distribution
50% 1.20ms
75% 1.26ms
90% 1.39ms
99% 2.18ms
76624 requests in 2.42s, 8.33MB read
Requests/sec: 31723.90
Transfer/sec: 3.45MB
gin
[root@master ~]# wrk -t 20 -c 20 -d 20 --latency http://192.168.31.220:8080/v1/
Running 20s test @ http://192.168.31.220:8080/v1/
20 threads and 20 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.72ms 15.99ms 252.84ms 98.86%
Req/Sec 0.85k 69.36 1.19k 93.86%
Latency Distribution
50% 1.14ms
75% 1.29ms
90% 1.43ms
99% 47.41ms
336915 requests in 20.05s, 41.13MB read
Requests/sec: 16807.31
Transfer/sec: 2.05MB
Spring webflux
[root@master ~]# wrk -t 20 -c 20 -d 20 --latency http://192.168.31.220:8080/hello
Running 20s test @ http://192.168.31.220:8080/hello
20 threads and 20 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.49ms 363.81us 16.38ms 81.33%
Req/Sec 674.38 47.03 0.90k 93.45%
Latency Distribution
50% 1.42ms
75% 1.51ms
90% 1.83ms
99% 2.84ms
268808 requests in 20.04s, 19.48MB read
Requests/sec: 13413.00
Transfer/sec: 0.97MB
Spring MVC
[root@master ~]# wrk -t 20 -c 20 -d 20 --latency http://192.168.31.220:8080/
Running 20s test @ http://192.168.31.220:8080/
20 threads and 20 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.59ms 1.11ms 41.46ms 97.15%
Req/Sec 670.55 45.97 821.00 84.12%
Latency Distribution
50% 1.39ms
75% 1.54ms
90% 1.80ms
99% 5.98ms
267979 requests in 20.09s, 31.99MB read
Requests/sec: 13339.89
Transfer/sec: 1.59MB
flask
[root@master ~]# wrk -t 20 -c 20 -d 20 --latency http://192.168.31.220:5000/
Running 20s test @ http://192.168.31.220:5000/
20 threads and 20 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 23.55ms 1.92ms 37.84ms 90.03%
Req/Sec 42.33 5.15 60.00 68.08%
Latency Distribution
50% 23.04ms
75% 23.54ms
90% 25.20ms
99% 32.07ms
16966 requests in 20.10s, 2.65MB read
Requests/sec: 844.16
Transfer/sec: 135.20KB