REST API测量服务器端响应时间(性能)。

我开发了一些基于nodejs的其他API,我想testingAPI的性能。 任何工具都可以轻松地统计每个API调用的时间吗?

或者如何实现测量REST API响应请求所需的时间。

下面是使用express.js进行精确时间测量的事件注入示例。

在您的路线之前添加这个:

app.all('*', function(req, res, next) { var start = process.hrtime(); // event triggers when express is done sending response res.on('finish', function() { var hrtime = process.hrtime(start); var elapsed = parseFloat(hrtime[0] + (hrtime[1] / 1000000).toFixed(3), 10); console.log(elapsed + 'ms'); }); next(); }); 

它将保存每个请求的开始时间,并在响应发送到客户端后触发finish
感谢user419127指向“完成”事件

那么像Apache JMeter这样的性能测量工具呢? 您可以轻松地使用它来模拟服务器(负载)的负载,然后测量响应时间和其他性能指标。 它为此提供了多个graphics表示。

本博客文章展示了如何为Web-API设置基于HTTP的性能testing。 我这样做来testing我的REST风格的web服务。

保持简单的使用console.time('timerName'); console.timeEnd('timerName')节点中的function。 'timerName'显然是可configuration的。

例:

console.time('getCustomers'); console.timeEnd('getCustomers')

输出:

getCustomers: 58ms