python

django运行测试

文章暂存

systemime
2020-08-07
9 min

摘要.

# 静态页面测试

# ab -n 1000 -c 200 http://47.100.114.188:60013/index/

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 47.100.114.188 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        WSGIServer/0.2
Server Hostname:        47.100.114.188
Server Port:            60013

Document Path:          /index/
Document Length:        182 bytes

Concurrency Level:      200
Time taken for tests:   8.710 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      423000 bytes
HTML transferred:       182000 bytes
Requests per second:    114.81 [#/sec] (mean)
Time per request:       1742.007 [ms] (mean)
Time per request:       8.710 [ms] (mean, across all concurrent requests)
Transfer rate:          47.43 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       28   55  64.5     38     918
Processing:    30  919 1593.1    310    8405
Waiting:       30  253 596.4     47    7383
Total:         61  974 1595.7    347    8446

Percentage of the requests served within a certain time (ms)
  50%    347
  66%    577
  75%   1023
  80%   1125
  90%   3443
  95%   5238
  98%   7041
  99%   7261
 100%   8446 (longest request)

# 协程测试

ab -n 1000 -c 200 http://47.100.114.188:60013/
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 47.100.114.188 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        WSGIServer/0.2
Server Hostname:        47.100.114.188
Server Port:            60013

Document Path:          /
Document Length:        54 bytes

Concurrency Level:      200
Time taken for tests:   14.320 seconds
Complete requests:      1000
Failed requests:        448
   (Connect: 0, Receive: 0, Length: 448, Exceptions: 0)
Total transferred:      285497 bytes
HTML transferred:       53497 bytes
Requests per second:    69.83 [#/sec] (mean)
Time per request:       2863.991 [ms] (mean)
Time per request:       14.320 [ms] (mean, across all concurrent requests)
Transfer rate:          19.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       28   41  22.2     37     209
Processing:  2034 2180 305.1   2060    3717
Waiting:     2033 2179 305.2   2059    3717
Total:       2066 2221 306.9   2100    3762

Percentage of the requests served within a certain time (ms)
  50%   2100
  66%   2140
  75%   2161
  80%   2185
  90%   2700
  95%   3128
  98%   3201
  99%   3738
 100%   3762 (longest request)

# tronado加速测试

对实现django3.1以后对协程等方法可能有极大对限制,等待测试(这种方法绑定对是django对wsgi而不是asgi)

ab -n 1000 -c 200 http://47.100.114.188:60014/index/
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 47.100.114.188 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        TornadoServer/6.0.4
Server Hostname:        47.100.114.188
Server Port:            60014

Document Path:          /index/
Document Length:        182 bytes

Concurrency Level:      200
Time taken for tests:   5.529 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      377000 bytes
HTML transferred:       182000 bytes
Requests per second:    180.85 [#/sec] (mean)
Time per request:       1105.890 [ms] (mean)
Time per request:       5.529 [ms] (mean, across all concurrent requests)
Transfer rate:          66.58 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       28   55  76.2     37     917
Processing:    31  701 933.5    370    4342
Waiting:       31  697 935.1    369    4342
Total:         61  756 942.5    410    4382

Percentage of the requests served within a certain time (ms)
  50%    410
  66%    472
  75%    882
  80%   1043
  90%   2119
  95%   2904
  98%   4131
  99%   4267
 100%   4382 (longest request)

# Asgi部署测试

django异步介绍文档

# uvicorn部署测试

uvicorn是目前django3.1支持的三种异步启动方式之中,着重于速度的部署方式,其支持nginx和gunicorn进行在正式环境中部署,并对cdn、https等提供支持

# 静态请求测试

ab -n 1000 -c 200 http://47.100.114.188:60013/app01/index/
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 47.100.114.188 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        uvicorn
Server Hostname:        47.100.114.188
Server Port:            60013

Document Path:          /app01/index/
Document Length:        221 bytes

Concurrency Level:      200
Time taken for tests:   9.436 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      441000 bytes
HTML transferred:       221000 bytes
Requests per second:    105.98 [#/sec] (mean)
Time per request:       1887.127 [ms] (mean)
Time per request:       9.436 [ms] (mean, across all concurrent requests)
Transfer rate:          45.64 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       28   93 269.9     39    3174
Processing:    35  907 1390.2    449    8450
Waiting:       35  876 1401.1    435    8450
Total:         66 1001 1400.8    557    8507

Percentage of the requests served within a certain time (ms)
  50%    557
  66%    854
  75%    893
  80%    972
  90%   2057
  95%   3945
  98%   6354
  99%   8429
 100%   8507 (longest request)

对比测试发现,于django默认等wsgi部署方式相比,静态请求速度下降了1ms,在官方文档中,给出的解释是启动asgi服务器后会对同步请求进行翻译处理,降带来1ms左右的性能下降,在实际生产中,可以忽略不计

image.png

# gunicorn单进程与UvicornWorker部署静态测试

速度明显加快,但是没有tornado代理速度快,

ab -n 1000 -c 200 http://47.100.114.188:60013/app01/index/
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 47.100.114.188 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        uvicorn
Server Hostname:        47.100.114.188
Server Port:            60013

Document Path:          /app01/index/
Document Length:        221 bytes

Concurrency Level:      200
Time taken for tests:   7.475 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      441000 bytes
HTML transferred:       221000 bytes
Requests per second:    133.77 [#/sec] (mean)
Time per request:       1495.072 [ms] (mean)
Time per request:       7.475 [ms] (mean, across all concurrent requests)
Transfer rate:          57.61 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       27  100 325.1     38    3239
Processing:    34  603 952.9    396    7235
Waiting:       34  570 947.8    386    7235
Total:         63  703 1001.6    452    7271

Percentage of the requests served within a certain time (ms)
  50%    452
  66%    507
  75%    575
  80%    595
  90%   1830
  95%   3565
  98%   3946
  99%   4344
 100%   7271 (longest request)

# 部署命令记录

gunicorn day01.asgi:application -b 127.0.0.1:60013 --reload -p mygunicorn -w 2 -t 1 -k uvicorn.workers.UvicornWorker
uvicorn --host 0.0.0.0 --port 60013 --reload day01.asgi:application

# 进程信号

  • 优雅的重新加载
kill -HUP masterpid
  • 增加工作进程
kill -TTIN masterpid
  • 减少工作进程
kill -TTOU masterpid
上次编辑于: 2021/5/20 下午3:26:49