一篇关于django,tornado性能测试的非专业性报告
Posted python自动化运维之美
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一篇关于django,tornado性能测试的非专业性报告相关的知识,希望对你有一定的参考价值。
为什么要写这篇文章?
确实是因为博主最近实在太无聊了。闲的蛋疼只能装作很忙的样子。。。故有了这篇。
测试说明
!!这里先说明一下,由于本文出自博主实验环境(windows 笔记本+VirtualBox虚拟linux)条件下,由于条件简陋,且测试代码均比较简单,故测试报告不能保证严谨准确,仅供参考和娱乐。
环境介绍
由于条件简陋,测试环境为本人自用thinkpad x240笔记本电脑(intel core i3 1.9GHZ + 8G内存)+VirtualBOX虚拟机(虚拟环境为centos6.9 64bit 处理器数量1 内存1024M) ,python版本2.7.13 ,django版本1.8.3,tornado版本4.5.2。测试中使用了gunicorn 版本19.7.1。nginx。(多么详细。。。),压力测试工具使用apache自带的 ab工具进行
简单说一下本次测试所写的代码(下面会详细贴出代码),在如此简陋的情况下,测试代码均为hello work状态。(即只利用框架输出index hello word)。
为了保证测试尽量严谨,每个项目压力测试均测试3次以上取平均值(如差异不大随机贴出一次结果。),更换项目后重启虚拟机测试。
不啰嗦了正式开始
一、django、tornado最基本代码测试对比
最基本测试,直接使用项目本身启动程序,即python xxx.py形式 不使用任何转发。
django
#由于代码简单 这里只贴出views.py代码,其余代码均为初始化项目后默认未动。(urls.py除添加访问url外无其他修改)
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from django.shortcuts import render
from django.template.context import RequestContext
import os
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world ! ")
#启动项目,开始测试
[root@centos1 untitled1]# python manage.py runserver 127.0.0.1:8888
Performing system checks...
System check identified no issues (0 silenced).
September 18, 2017 - 06:53:50
Django version 1.8.3, using settings 'untitled1.settings'
Starting development server at http://127.0.0.1:8888/
Quit the server with CONTROL-C
[root@centos1 ~]# ab -n 1000 -c 1000 http://127.0.0.1:8888/index/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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 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.1
Server Hostname: 127.0.0.1
Server Port: 8888
Document Path: /index/
Document Length: 14 bytes
Concurrency Level: 1000
Time taken for tests: 3.010 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 177017 bytes
html transferred: 14000 bytes
Requests per second: 332.22 [#/sec] (mean) ##看这里!!
Time per request: 3010.077 [ms] (mean)
Time per request: 3.010 [ms] (mean, across all concurrent requests)
Transfer rate: 57.43 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 46 234.5 0 3000
Processing: 3 52 159.3 32 1664
Waiting: 1 49 159.3 29 1662
Total: 20 98 334.0 33 3007
Percentage of the requests served within a certain time (ms)
50% 33
66% 34
75% 36
80% 38
90% 46
95% 82
98% 1036
99% 2631
100% 3007 (longest request)
可以看到默认情况下,吞吐率为332.22 [#/sec]
tornado
import textwrap
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import os
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
class SyncHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.finish('hello word')
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[
(r"/index", SyncHandler)
],
#debug = True,
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
[root@centos1 ~]# python tornadotest.py
[root@centos1 ~]# ab -n 1000 -c 1000 http://127.0.0.1:8888/index
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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 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/4.5.2
Server Hostname: 127.0.0.1
Server Port: 8888
Document Path: /index
Document Length: 8 bytes
Concurrency Level: 1000
Time taken for tests: 2.878 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 202000 bytes
HTML transferred: 8000 bytes
Requests per second: 347.47 [#/sec] (mean) ##看这里!!!
Time per request: 2877.963 [ms] (mean)
Time per request: 2.878 [ms] (mean, across all concurrent requests)
Transfer rate: 68.54 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 279 441.2 0 1001
Processing: 1 580 545.3 303 1858
Waiting: 0 579 545.3 302 1858
Total: 44 859 905.7 362 2858
Percentage of the requests served within a certain time (ms)
50% 362
66% 606
75% 1059
80% 1284
90% 2701
95% 2789
98% 2827
99% 2842
100% 2858 (longest request)
可以看到默认情况下,吞吐率为347.47 [#/sec]
结果,默认情况下 django和tornado吞吐率方面几乎是没区别的。
二、django、tornado最基本代码模拟访问数据库测试对比
最基本测试,直接使用项目本身启动程序,即python xxx.py形式 不使用任何转发。模拟数据库访问 这里使用加一段ping代码来进行。(机器不行就不要真的搞个mysql访问了。抱歉抱歉。)
django
#这里根据上面的而是代码修改
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from django.shortcuts import render
from django.template.context import RequestContext
import os
from django.http import HttpResponse
def index(request):
os.system("ping -c 1 www.baidu.com") #增加个这个代码,咱们ping 1次百度来模拟这里进行了数据库select访问。
return HttpResponse("Hello world ! ")
[root@centos1 untitled1]# python manage.py runserver 127.0.0.1:8888
Performing system checks...
System check identified no issues (0 silenced).
September 18, 2017 - 07:09:00
Django version 1.8.3, using settings 'untitled1.settings'
Starting development server at http://127.0.0.1:8888/
Quit the server with CONTROL-C.
[root@centos1 ~]# ab -n 1000 -c 1000 http://127.0.0.1:8888/index/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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 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.1
Server Hostname: 127.0.0.1
Server Port: 8888
Document Path: /index/
Document Length: 14 bytes
Concurrency Level: 1000
Time taken for tests: 29.218 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 177000 bytes
HTML transferred: 14000 bytes
Requests per second: 34.23 [#/sec] (mean) ##看这里!!!
Time per request: 29217.821 [ms] (mean)
Time per request: 29.218 [ms] (mean, across all concurrent requests)
Transfer rate: 5.92 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 680 2661.3 0 15001
Processing: 29 1555 3123.2 412 13510
Waiting: 28 1548 3124.4 403 13509
Total: 31 2235 4130.7 434 17915
Percentage of the requests served within a certain time (ms)
50% 434
66% 514
75% 726
80% 1526
90% 10198
95% 10465
98% 15329
99% 17629
100% 17915 (longest request)
可以看到这种情况下,吞吐率为34.23 [#/sec] 明显下降了很多
tornado
import textwrap
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import os
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
class SyncHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
os.system("ping -c 1 www.baidu.com")
self.finish('hello word')
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[
(r"/index", SyncHandler)
],
#debug = True,
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
[root@centos1 ~]# ab -n 1000 -c 1000 http://127.0.0.1:8888/index
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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)
apr_socket_recv: Connection reset by peer (104)
Total of 45 requests completed
[root@centos1 ~]# ab -n 1000 -c 1000 http://127.0.0.1:8888/index
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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)
apr_poll: The timeout specified has expired (70007)
###由于tornado框架的设计理念问题。这种阻塞的请求方式无法完成本次测试。
#在多次调低了参数之后 终于成功了一次
ab -n 500 -c 500 http://127.0.0.1:8888/index。。。
ab -n 200 -c 200 http://127.0.0.1:8888/index。。。
ab -n 100 -c 100 http://127.0.0.1:8888/index。。。
[root@centos1 ~]# ab -n 100 -c 100 http://127.0.0.1:8888/index
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
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).....done
Server Software: TornadoServer/4.5.2
Server Hostname: 127.0.0.1
Server Port: 8888
Document Path: /index
Document Length: 8 bytes
Concurrency Level: 100
Time taken for tests: 23.339 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 20200 bytes
HTML transferred: 800 bytes
Requests per second: 4.28 [#/sec] (mean) ##看这里看这里
Time per request: 23338.514 [ms] (mean)
Time per request: 233.385 [ms] (mean, across all concurrent requests)
Transfer rate: 0.85 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 6 1.1 6 9
Processing: 33 13564 10476.5 21515 23290
Waiting: 33 13564 10476.5 21515 23289
Total: 39 13570 10476.6 21522 23295
Percentage of the requests served within a certain time (ms)
50% 21522
66% 22118
75% 22405
80% 22568
90% 22954
95% 23143
98% 23255
99% 23295
100% 23295 (longest request)
可以看到这种情况下,吞吐率为4.28 [#/sec] 惨不忍睹!
结果,很显然这个对比是没有意义的,由于tornado的单线程的设计逻辑,导致在不使用非阻塞是方法编程时性能简直惨不忍睹。(ps:由于这里是模拟了数据库访问操作,在实际的环境中访问mysql的速度理论上远大于ping 1次的速度)
限于篇幅关系,我们下一篇将继续进行django和tornado的性能测试。
以上是关于一篇关于django,tornado性能测试的非专业性报告的主要内容,如果未能解决你的问题,请参考以下文章