node.js - 为什么我应该使用 Restify?

标签 node.js express restify

我需要在 node.js 中构建一个 REST API,并且正在寻找一个比 express.js 更轻量级的框架,它可能会避免不需要的功能,并且可以作为构建 REST API 的定制框架. Restify from its intro 推荐用于相同的情况。

阅读 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 更有效吗?还是我测试它们的方式有什么错误?

根据评论更新

restify 的行为

  1. 当负载超过 1000 个 req.s 时,它会在 1 秒内停止处理,直到 1015 个 req.s 接收,然后什么也不做。 IE。我实现的用于计算传入请求的计数器在 1015 之后停止递增。

  2. 当负载甚至 100 个请求时。每秒它接收到 1015 并且在那之后没有响应。

最佳答案

更正:此信息现在有误,请继续滚动!

there was an issue with the script causing the Restify test to be conducted on an unintended route. This caused the connection to be kept alive causing improved performance due to reduced overhead.


这是 2015 年,我认为自那以后情况发生了很大变化。 Raygun.io 发布了 recent benchmark comparing hapi, express and restify .

上面写着:

We also identified that Restify keeps connections alive which removes the overhead of creating a connection each time when getting called from the same client. To be fair, we have also tested Restify with the configuration flag of closing the connection. You’ll see a substantial decrease in throughput in that scenario for obvious reasons.

Benchmark image from Raygun.io

看起来 Restify 是更容易部署服务的赢家。特别是如果您正在构建一个接收来自相同客户端的大量请求并希望快速移动的服务。你当然比裸 Node 更划算,因为你有像 DTrace 支持这样的功能。

关于node.js - 为什么我应该使用 Restify?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17589178/

相关文章:

javascript - EJS 噩梦——无法包含外部 View

GitHub Atom 中导入模块的 JavaScript 自动完成

node.js - 无法解析来自 uglifyjs-webpack-plugin@2.2.0 的依赖项 : npm ERR! 对等 webpack @"^4.0.0"

PHP REDIS NODE.JS 多发布者/订阅者

javascript - 将全局配置变量传递给 handlebars 模板的最佳方法是什么?

javascript - NodeJS Express 多异步已完成

javascript - *ngIf 创建无限循环并且永远不会解析

node.js - Restify:URL 中的 API 版本

javascript - 读取查询变量和主体变量有什么区别?

mysql - Restify:使用 promise 链复制文件和查询数据库时发生套接字挂起错误