我需要在node.js中构建一个REST API,并且正在寻找一个比express.js更轻量级的框架,这可能会避免不必要的功能,并且像构建REST API的定制框架一样。对于同样的情况,推荐从其介绍中重新开始。
阅读Why use restify and not express?似乎restify是一个不错的选择。
但惊喜来了,当我尝试了两个负载。
我在Restify上做了一个示例REST API,并且每秒1000次请求。对我惊讶的路线开始没有反应一段时间后。基于express.js构建的同一个应用程序处理所有。
我目前正在通过API应用负载
var FnPush = setInterval(function() { for(i=0;i<1000;i++) SendMsg(makeMsg(i)); },1000); function SendMsg(msg) { var post_data = querystring.stringify(msg); var post_options = { host: target.host,port: target.port,path: target.path,agent: false,method: 'POST',headers: { 'Content-Type': 'application/x-www-form-urlencoded','Content-Length': post_data.length,"connection": "close" } }; var post_req = http.request(post_options,function(res) {}); post_req.write(post_data); post_req.on('error',function(e) { }); post_req.end(); }
我的结果是否明智?如果是这样的话,express在这种情况下比restify更有效率?或者在我测试他们的方式有任何错误?
响应评论更新
重置行为
>当喂入超过1000 req.s的负载时,它停止处理只需1秒接收直到1015 req.s然后什么也不做。即。用于对输入请求计数的计数器i在1015之后停止增量。
>供给甚至100 reqs的负载。每秒它收到,直到1015和后无响应。
解决方法
在这个
blog中,PerfectAPI和Express.js和Restify.js之间进行了比较,结果是Express比大量的查询的Restify更好,所以我使用当前版本的Express和Restify做了一个简单的基准
这里是测试express的代码:
var express = require('express'); var app = express(); app.get('/hello/:name',function(req,res){ res.send('hello ' + req.params.name); }); app.listen(3000); console.log('Listening on port 3000');
这里是Restify的代码:
var restify = require('restify'); var server = restify.createServer(); server.get('/hello/:name',res,next) { res.send('hello ' + req.params.name); }); server.listen(3000,function() { console.log('Listening on port 3000'); });
我使用ApacheBench测试,这是一个simple example使用它。
你可以使用sudo apt-get install apache2-utils来安装它
那么可以运行此命令来测试ab -n 10000 -c 100 http://127.0.0.1:3000/。这将以10000个请求命中服务器,并发数为100。
重新生成的结果
Server Hostname: 127.0.0.1 Server Port: 3000 Document Path: /hello/mark Document Length: 12 bytes Concurrency Level: 100 Time taken for tests: 2.443 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 1390000 bytes HTML transferred: 120000 bytes Requests per second: 4092.53 [#/sec] (mean) Time per request: 24.435 [ms] (mean) Time per request: 0.244 [ms] (mean,across all concurrent requests) Transfer rate: 555.53 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.5 0 8 Processing: 5 24 4.5 23 40 Waiting: 5 24 4.5 23 40 Total: 12 24 4.5 23 40
和快递:
Server Hostname: 127.0.0.1 Server Port: 3000 Document Path: /hello/mark Document Length: 10 bytes Concurrency Level: 100 Time taken for tests: 2.254 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 1890000 bytes HTML transferred: 100000 bytes Requests per second: 4436.76 [#/sec] (mean) Time per request: 22.539 [ms] (mean) Time per request: 0.225 [ms] (mean,across all concurrent requests) Transfer rate: 818.89 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.5 0 7 Processing: 17 22 4.7 21 55 Waiting: 16 22 4.7 21 55 Total: 18 22 4.9 21 58
从比较,你可以看到Express比Restify更快,但Restify没有阻止和响应所有请求。