一篇关于django,tornado性能测试的非专业性报告
Posted python自动化运维之美
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一篇关于django,tornado性能测试的非专业性报告相关的知识,希望对你有一定的参考价值。
上一篇里我们简单了说了一下 django、tornado最基础代码 性能方面的对比 不知道的同学可以点这个 一篇关于django,tornado性能测试的非专业性报告(一)
那么这一篇我们稍微深入一些。继续对比两者之间的性能。
第一篇的第二个测试,由于tornado的设计问题我们采用阻塞的编程方式来测试,导致那是一个无意义的对比。那么这篇的第一个测试就采用tornado异步处理的方式来进行吧
ps:由于django不存在阻塞非阻塞设计理念的问题 所以这里就不写django的代码及测试报告了。这里仅仅将第一篇中 “二、django、tornado最基本代码模拟访问数据库测试对比” 的tornado部分代码改造后重新测试。第一篇
tornado异步处理模拟访问数据库测试
容我再啰嗦一句,简单介绍一下这里的代码编写逻辑。我们是通过ping 一次百度来模拟打开网页时从数据库中读取信息的过程。采用tornado异步方式,这里的异步方式不是传统的celery方式,而是tornado协程(异步非阻塞式返回),如果这里不理解那么你只能看tornado的官方文档了。(ps:传统的celery是后台异步,直接不返回处理结果的,这里虽然是异步但是依然返回结果。)
import textwrap
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient
import tornado.gen
import os
import functools
import tornado.concurrent
from tornado.options import define, options
import time
from tornado import escape
from tornado import gen
define("port", default=8888, help="run on the given port", type=int)
class AsyncHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self, *args, **kwargs):
yield gen.sleep(1)
self.finish('hello')
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[
(r"/aaa", FutureHandler2),
(r"/bbb", AsyncHandler),
],
# 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://localhost:8888/bbb
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 localhost (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: localhost
Server Port: 8888
Document Path: /bbb
Document Length: 5 bytes
Concurrency Level: 1000
Time taken for tests: 4.872 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 199000 bytes
html transferred: 5000 bytes
Requests per second: 205.26 [#/sec] (mean) ##看这里!!
Time per request: 4871.895 [ms] (mean)
Time per request: 4.872 [ms] (mean, across all concurrent requests)
Transfer rate: 39.89 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 450 492.7 35 1003
Processing: 1005 1466 511.4 1276 2842
Waiting: 1002 1466 511.4 1275 2842
Total: 1040 1916 873.0 1486 3843
Percentage of the requests served within a certain time (ms)
50% 1486
66% 2136
75% 2537
80% 2713
90% 3495
95% 3789
98% 3826
99% 3835
100% 3843 (longest request)
可以看到这种情况下,吞吐率为205.26 [#/sec]
!!!这里解释一下,由于tornado 在执行os.system的时候不能进行异步操作,google和查看官方文档后得证 os操作属于block形式 无法进行异步操作,故上述代码改成了sleep 1秒的形式来模拟访问数据库。
关于tornado的异步用法有好多种,这里就演示这一种。有兴趣的同学可以自行学习。
结果,当tornado采用异步方式进行代码构建时,性能确实远远超过django。虽然我们这个测试不够严谨(理论上django是不建议直接运行来承担访问的)。
半路杀出的go
当博主我正沉浸在编写此篇博文和测试的热潮中时,一位好友突然杀出,让我将同样的功能用go语言编写测试一下性能。为了友谊 ,我决定满足他这要求。(此段纯属娱乐)
//代码贴上,这里只是简单实现了一个web服务,功能也仅仅是返回helloword而已。
package main
import (
"fmt"
"log"
"net/http"
)
func sayHello(response http.ResponseWriter, request *http.Request) {
fmt.Println("Hello World!")
}
func main() {
http.HandleFunc("/hello", sayHello)
er := http.ListenAndServe(":8888", nil)
if er != nil {
log.Fatal("ListenAndServe: ", er)
}
}
#将代码编译一下
go bulid hello.go
#运行./hello
[root@centos1 ~]# ab -n 1000 -c 1000 http://localhost:8090/hello/
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 localhost (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:
Server Hostname: localhost
Server Port: 8090
Document Path: /hello/
Document Length: 39 bytes
Concurrency Level: 1000
Time taken for tests: 0.340 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 197028 bytes
HTML transferred: 49257 bytes
Requests per second: 2943.64 [#/sec] (mean) ##看这里!!
Time per request: 339.715 [ms] (mean)
Time per request: 0.340 [ms] (mean, across all concurrent requests)
Transfer rate: 566.39 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 75 21.1 69 113
Processing: 45 129 48.7 132 184
Waiting: 0 90 48.7 120 146
Total: 113 204 30.1 215 253
Percentage of the requests served within a certain time (ms)
50% 215
66% 227
75% 230
80% 233
90% 237
95% 241
98% 245
99% 248
100% 253 (longest request)
好吧如此炸裂的性能!
限于篇幅的原因,本次性能测试基本是接近尾声了。虽然测试的过程不够严谨,但是我们也能简单的看出。tornado的异步处理能力,性能确实优于直接运行的django。注意这个前提是django直接运行。那么如果采用Gunicorn+django的方式呢。我们以后再战!
以上是关于一篇关于django,tornado性能测试的非专业性报告的主要内容,如果未能解决你的问题,请参考以下文章