想了解一下twisted单独作为web服务器的性能。
所以用webpy写一个简单的页面,挂上twisted的wsgi跑一下。
测试代码:
import web from twisted.web2 import wsgi,server, channel, static from twisted.application import service, strports urls = ( '/(.*)', 'hello' ) app = web.application(urls, globals()) class hello: def GET(self, name): if not name: name = 'world' return 'Hello, ' + name + '!' #application = web.wsgifunc(web.webpyfunc(urls, globals())) #application = web.application(urls, globals()).wsgifunc() #app.run() web_res = wsgi.WSGIResource(web.application(urls, globals()).wsgifunc()) site = server.Site(web_res) application = service.Application("demoserver") s = strports.service('tcp:8080', channel.HTTPFactory(site)) s.setServiceParent(application)
以 twistd -noy test.tac的方式运行。
测试环境:
Hardware Overview:
Model Name: MacBook
Model Identifier: MacBook2,1
Processor Name: Intel Core 2 Duo
Processor Speed: 2 GHz
Number Of Processors: 1
Total Number Of Cores: 2
L2 Cache: 4 MB
Memory: 2 GB
Bus Speed: 667 MHz
测试软件 siege
1. 20并发,各10次
Transactions: 200 hits
Availability: 100.00 %
Elapsed time: 9.65 secs
Data transferred: 0.00 MB
Response time: 0.11 secs
Transaction rate: 20.73 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 2.18
Successful transactions: 200
Failed transactions: 0
Longest transaction: 0.45
Shortest transaction: 0.01
结论:
还不能下结论。
再试试twisted.web自身的。
代码:
from twisted.web import http class MyRequestHandler(http.Request): pages={ '/':'Hello world!', '/test':'<h1>Test</h1>Test Page', } def process(self): if self.pages.has_key(self.path): self.write(self.pages[self.path]) else: self.setResponseCode(http.NOT_FOUND) self.write("<h1>Not Found</h1>Sorry, no such page.") self.finish() class MyHttp(http.HTTPChannel): requestFactory=MyRequestHandler class MyHttpFactory(http.HTTPFactory): protocol=MyHttp if __name__=="__main__": from twisted.internet import reactor reactor.listenTCP(8080,MyHttpFactory()) reactor.run()
注意,这个代码没有用twistd启动,而是直接运行了。应该区别不大。
1. 20并发,10请求
Transactions: 200 hits
Availability: 100.00 %
Elapsed time: 6.06 secs
Data transferred: 0.00 MB
Response time: 0.01 secs
Transaction rate: 33.00 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 0.18
Successful transactions: 200
Failed transactions: 0
Longest transaction: 0.02
Shortest transaction: 0.00
Transactions: 2000 hits
Availability: 100.00 %
Elapsed time: 56.31 secs
Data transferred: 0.02 MB
Response time: 0.00 secs
Transaction rate: 35.52 trans/sec
Throughput: 0.00 MB/sec
Concurrency: 0.11
Successful transactions: 2000
Failed transactions: 0
Longest transaction: 0.03
Shortest transaction: 0.00
结论:
1.不用wsgi要快不少,尤其是在高并发的时候。
2. 不用wsgi的时候,CPU占用相当低,10%-20%之间。 用的时候,在100%以上。
仅供参考。
可能测试的不太准。原因:
1. 用了不同的运行方式。
2. 用户web,web2