Node.js, Go, Python, OpenResty Benchmark

心血来潮,简单测试一下各种语言写的API Server的性能。

前言

我已经用过很多Web框架了。Python-httplib, Python-Flask,Python-Tornado,Node-http, Node-Express,Node-koa,Node-restify, Go-http。最近在做OpenAPI,用了一个开源组件Kong,后来觉得这玩意虽然设计的不错但是碍手碍脚,有一些功能还是需要深入底层去自己研究实现。后来发现Kong是基于OpenResty实现的,而OpenResty则是Nginx的一个“Bundle”,打好了很多方便的包,性能很不错的样子。正好籍由此次机会,测试一下各语言写的裸API性能。

所有Server端使用HelloWorld Server,即发送”Hello, World”字符串作为Body。
测试Client一并使用ab -kc10 -n50000进行。
测试环境Server与Client位于同一级房的两台相邻物理机。规格为: CPU: Intel(R) Xeon(R) CPU E5-2430 0 @ 2.20GHz 24核, 内存100G。

只是简单测试一下。

测试Server用例

Node.js单进程
 
 
  1. var http = require('http');
  2. var server = http.createServer( (req, res) => {
  3. res.writeHead(200, {'Content-Type': 'text/plain'});
  4. res.end("Hello, World");
  5. });
  6. server.listen(8080);
Node.js Cluster(24)
 
 
  1. const cluster = require('cluster');
  2. const http = require('http');
  3. const numCPUs = require('os').cpus().length;
  4. if (cluster.isMaster) {
  5. for (var i = 0; i < numCPUs; i++) {
  6. cluster.fork();
  7. }
  8. cluster.on('exit', (worker, code, signal) => {
  9. console.log(`worker ${worker.process.pid} died`);
  10. });
  11. } else {
  12. http.createServer((req, res) => {
  13. res.writeHead(200);
  14. res.end("Hello, World");
  15. }).listen(8080);
  16. }
Python-Tornado
 
 
  1. import tornado.ioloop
  2. import tornado.web
  3. class MainHandler(tornado.web.RequestHandler):
  4. def get(self):
  5. self.write("Hello, world")
  6. def make_app():
  7. return tornado.web.Application([
  8. (r"/", MainHandler),
  9. ])
  10. if __name__ == "__main__":
  11. app = make_app()
  12. app.listen(8080)
  13. tornado.ioloop.IOLoop.current().start()
Go-Http
 
 
  1. package main
  2. import (
  3. "io"
  4. "net/http"
  5. )
  6. func main() {
  7. http.HandleFunc("/", sayhello)
  8. http.ListenAndServe(":8080", nil)
  9. }
  10. func sayhello(w http.ResponseWriter, r *http.Request) {
  11. io.WriteString(w, "hello world")
  12. }

OpenResty(Nginx+lua)

 
 
  1. worker_processes 1;
  2. error_log logs/error.log;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. server {
  8. listen 8080;
  9. location / {
  10. default_type text/html;
  11. content_by_lua '
  12. ngx.say("<p>hello, world</p>")
  13. ';
  14. }
  15. }
  16. }

结果

对各种语言框架的最简EchoServer实现进行不同并发度的测试。结果如下:

c = 1
langrpstpr (ms)
node 1x2451.250.408
node 24x1119.810.893
Py-Tornado1301.680.768
Go-Http7108.640.141
Nginx-lua 1x7385.980.135
Nginx-lua 24x7368.340.136
c = 10
langrpstpr (ms)
node 1x3944.752.535
node 24x5645.111.771
Py-Tornado1318.857.582
Go-Http70085.240.143
Nginx-lua 1x24753.790.404
Nginx-lua 24x24824.980.403
c = 100
langrpstpr (ms)
node 1x4042.2724.739
node 24x5816.2317.193
Py-Tornado1283.4378.261
Go-Http77451.381.373
Nginx-lua 1x25001.294.080
Nginx-lua 24x70333.041.619

结论:

  • OpenResty(Nginx+Lua) 与 Go语言属于性能第一梯队。Node属于第二梯队,Python垫底……。
  • Go是特么的禽兽啊……
  • OpenResty也不错……。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值