看到网上都说nodejs处理并发请求速度很快.对比了一下pypy + twisted 单线程模型下
pypy+twisted完胜 nodejs 最少比nodejs快2倍以上
nodejs 结果:
Benchmarking 127.0.0.1 (be patient)
Completed 4000 requestsCompleted 8000 requestsCompleted 12000 requestsCompleted 16000 requestsCompleted 20000 requestsCompleted 24000 requestsCompleted 28000 requestsCompleted 32000 requestsCompleted 36000 requestsCompleted 40000 requestsFinished 40000 requestsServer Software: Server Hostname: 127.0.0.1Benchmarking 127.0.0.1 (be patient)Completed 4000 requestsCompleted 8000 requestsCompleted 12000 requestsCompleted 16000 requestsCompleted 20000 requestsCompleted 24000 requestsCompleted 28000 requestsCompleted 32000 requestsCompleted 36000 requestsCompleted 40000 requestsFinished 40000 requestsServer Software: Server Hostname: 127.0.0.1Server Port: 8080Document Path: /Document Length: 12 bytesConcurrency Level: 10000Time taken for tests: 3.465 secondsComplete requests: 40000Failed requests: 0Total transferred: 2280000 bytesHTML transferred: 480000 bytesRequests per second: 11543.84 [#/sec] (mean)Time per request: 866.263 [ms] (mean)Time per request: 0.087 [ms] (mean, across all concurrent requests)Transfer rate: 642.58 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 200 350.6 7 1026Processing: 4 105 170.3 12 1658Waiting: 2 101 167.4 10 1658Total: 5 304 427.1 20 2677Percentage of the requests served within a certain time (ms) 50% 20 66% 446 75% 461 80% 518 90% 1029 95% 1064 98% 1418 99% 1811 100% 2677 (longest request)Server Port: 8888Document Path: /Document Length: 11 bytesConcurrency Level: 10000Time taken for tests: 6.965 secondsComplete requests: 40000Failed requests: 0Total transferred: 4480000 bytesHTML transferred: 440000 bytesRequests per second: 5742.94 [#/sec] (mean)Time per request: 1741.267 [ms] (mean)Time per request: 0.174 [ms] (mean, across all concurrent requests)Transfer rate: 628.13 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 713 968.6 200 3010Processing: 19 121 200.9 47 3277Waiting: 19 121 200.9 47 3277Total: 22 834 987.6 676 6283Percentage of the requests served within a certain time (ms) 50% 676 66% 1046 75% 1054 80% 1173 90% 3047 95% 3059 98% 3090 99% 3248 100% 6283 (longest request)
pypy + twisted
Benchmarking 127.0.0.1 (be patient)
Completed 4000 requestsCompleted 8000 requestsCompleted 12000 requestsCompleted 16000 requestsCompleted 20000 requestsCompleted 24000 requestsCompleted 28000 requestsCompleted 32000 requestsCompleted 36000 requestsCompleted 40000 requestsFinished 40000 requestsServer Software: Server Hostname: 127.0.0.1Server Port: 8080Document Path: /Document Length: 12 bytesConcurrency Level: 10000Time taken for tests: 3.465 secondsComplete requests: 40000Failed requests: 0Total transferred: 2280000 bytesHTML transferred: 480000 bytesRequests per second: 11543.84 [#/sec] (mean)Time per request: 866.263 [ms] (mean)Time per request: 0.087 [ms] (mean, across all concurrent requests)Transfer rate: 642.58 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 200 350.6 7 1026Processing: 4 105 170.3 12 1658Waiting: 2 101 167.4 10 1658Total: 5 304 427.1 20 2677Percentage of the requests served within a certain time (ms) 50% 20 66% 446 75% 461 80% 518 90% 1029 95% 1064 98% 1418 99% 1811 100% 2677 (longest request)
nodejs代码:
var http = require("http");
http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"});response.write("Hello World");response.end();}).listen(8888);
twisted代码:
import sys
from twisted.internet import epollreactorepollreactor.install();from twisted.internet import reactorfrom twisted.web import httpclass MyRequestHandler(http.Request): pages={ '/':'Hello world!', '/test':'<h1>Test</h1>Test Page', } def process(self): if self.pages.has_key(self.path): self.setHeader("Content-Type", "text/plain") 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=MyRequestHandlerclass MyHttpFactory(http.HTTPFactory): protocol=MyHttpif __name__=="__main__": reactor.listenTCP(8080,MyHttpFactory()) reactor.run()