# Nginx/Unit vs Gunicorn 的效能與消耗資源測試 [toc] ## Nginx/Unit vs Gunicorn 的Apache(ab)效能與消耗資源測試 ###### tags: `Ngix` `unit` `gunicorn` `ab` `wrt` 我開了一個1 core,記憶體只有256mb的 lxc (openVZ) 做為測試平台。先說結論,unit的速度較gunicorn快一些, 使用記憶體也差不多,但對於使用cpu就差非常多了,unit只用了gunicorn的2/5的算力。 ### 一支程式 使用 unit做為微伺服器,以 apache/ab 作壓力測試 ```bash= ab -n 50000 -c 200 http://XXXXXX This is ApacheBench, Version 2.3 <$Revision: 1843412 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking XXXXXX.mirai.tw (be patient) Completed 5000 requests Completed 10000 requests Completed 15000 requests Completed 20000 requests Completed 25000 requests Completed 30000 requests Completed 35000 requests Completed 40000 requests Completed 45000 requests Completed 50000 requests Finished 50000 requests Server Software: Unit/1.29.0 Server Hostname: XXXXXX Server Port: 2101 Document Path: / Document Length: 46 bytes Concurrency Level: 200 Time taken for tests: 18.290 seconds Complete requests: 50000 Failed requests: 0 Total transferred: 8100000 bytes HTML transferred: 2300000 bytes Requests per second: 2733.72 [#/sec] (mean) Time per request: 73.160 [ms] (mean) Time per request: 0.366 [ms] (mean, across all concurrent requests) Transfer rate: 432.48 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 24 34 13.8 32 205 Processing: 24 39 15.5 37 545 Waiting: 15 34 13.7 32 544 Total: 48 73 21.2 70 574 Percentage of the requests served within a certain time (ms) 50% 70 66% 74 75% 77 80% 79 90% 85 95% 92 98% 108 99% 193 100% 574 (longest request) ``` ### 同一支程式 使用 gunicorn做為微伺服器,以 apache/ab 作壓力測試 ```bash= ab -n 50000 -c 200 http://XXXXXX This is ApacheBench, Version 2.3 <$Revision: 1843412 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking XXXXXX.mirai.tw (be patient) Completed 5000 requests Completed 10000 requests Completed 15000 requests Completed 20000 requests Completed 25000 requests Completed 30000 requests Completed 35000 requests Completed 40000 requests Completed 45000 requests Completed 50000 requests Finished 50000 requests Server Software: gunicorn Server Hostname: XXXXXX.tw Server Port: 2110 Document Path: / Document Length: 46 bytes Concurrency Level: 200 Time taken for tests: 21.456 seconds Complete requests: 50000 Failed requests: 0 Total transferred: 8900000 bytes HTML transferred: 2300000 bytes Requests per second: 2330.40 [#/sec] (mean) Time per request: 85.822 [ms] (mean) Time per request: 0.429 [ms] (mean, across all concurrent requests) Transfer rate: 405.09 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 24 32 36.0 30 3042 Processing: 24 53 26.3 46 527 Waiting: 2 50 26.1 43 525 Total: 48 85 43.8 78 3081 Percentage of the requests served within a certain time (ms) 50% 78 66% 83 75% 87 80% 91 90% 112 95% 118 98% 201 99% 235 100% 3081 (longest request) ``` ### 消耗資源比較 unit 勝 ![unit](https://app3.mirai.tw/static/ab-test-unit.png) gunicorn 敗 ![gunicorn](https://app3.mirai.tw/static/ab-test-gunicorn.png) ## Nginx/Unit vs Gunicorn 的wrt效能與消耗資源測試 wrk 是用C語言寫的http benchmark 工具,是一種簡易的HTTP 性能測試。 1. 安裝 wrt ``` 作者將原始檔放在git上,所以要安裝它最直接的方法 就是去git抓下來make就可以了!以下是指令 yum groupinstall 'Development Tools' yum -y install openssl-devel git git clone https://github.com/wg/wrk.git wrk cd wrk make \# copy wrk 到 PATH 可執行的目錄中,我的習慣釋放到/usr/local/bin cp wrk /usr/local/bin ``` 2. 使用方法 ``` Usage: wrk <options> <url> Options: -c, --connections <N> Connections to keep open -d, --duration <T> Duration of test -t, --threads <N> Number of threads to use -s, --script <S> Load Lua script file -H, --header <H> Add header to request --latency Print latency statistics --timeout <T> Socket/request timeout -v, --version Print version details Numeric arguments may include a SI unit (1k, 1M, 1G) Time arguments may include a time unit (2s, 2m, 2h) ``` 例如: wrk -t12 -c400 -d30s http://127.0.0.1:8300/ ``` 輸出 (output) Running 30s test @ http://127.0.0.1:8300/ 12 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 244.87ms 16.11ms 400.26ms 93.68% Req/Sec 137.86 93.22 333.00 59.80% 48060 requests in 30.06s, 3.55GB read Requests/sec: 1598.63 Transfer/sec: 121.00MB ``` ##### -t12 用 12 個線程 # -c400 模擬 400 個併發連接 # -d30s 持續 30 秒 ``` Unit Server wrk -t12 -c400 -d30s http://XXXX Running 30s test @ http://XXXX 12 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 66.79ms 109.34ms 1.98s 91.51% Req/Sec 758.14 204.73 1.38k 65.03% 271528 requests in 30.09s, 57.23MB read Requests/sec: 9023.89 Transfer/sec: 1.90MB Gunicorn Server wrk -t12 -c400 -d30s http://XXXX Running 30s test @ http://XXXX 12 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 132.75ms 170.39ms 1.95s 90.85% Req/Sec 157.16 85.44 666.00 67.08% 49022 requests in 30.06s, 11.08MB read Socket errors: connect 0, read 0, write 0, timeout 24 Requests/sec: 1630.85 Transfer/sec: 377.45KB ``` 結論:unit完勝,速度約為gunicorn的5倍!!! ##### 調整參數進行第二次測試 ``` Unit: wrk -t12 -c600 -d60s http://xxxxxx:2101/ Running 1m test @ http://xxxxxx:2101/ 12 threads and 600 connections Thread Stats Avg Stdev Max +/- Stdev Latency 48.06ms 15.69ms 345.87ms 76.38% Req/Sec 0.99k 256.93 2.02k 71.44% 700511 requests in 1.00m, 147.64MB read Requests/sec: 11657.24 Transfer/sec: 2.46MB Gunicorn: wrk -t12 -c600 -d60s http://xxxxxx:2110/ Running 1m test @ http://xxxxxx:2110/ 12 threads and 600 connections Thread Stats Avg Stdev Max +/- Stdev Latency 107.34ms 179.78ms 1.95s 92.98% Req/Sec 165.13 107.57 640.00 67.50% 23435 requests in 1.00m, 5.30MB read Socket errors: connect 0, read 296, write 0, timeout 738 Requests/sec: 390.13 Transfer/sec: 90.29KB ```