[英]REST API measuring server-side response times (performance).
I developed some rest APIs based nodejs, I want to test the performance of the APIs. 我开发了一些基于nodejs的rest API,我想测试API的性能。 Is any tool can easily count the time of each API call? 是否有任何工具可以轻松计算每次API调用的时间?
Or how to implement measuring of time required for REST API to response on requests. 或者如何实现REST API响应请求所需的时间测量。
Here is example of how to make event injection with precise time measuring using express.js. 以下是如何使用express.js进行精确时间测量的事件注入示例。
Add this before your routes: 在您的路线之前添加:
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();
});
It will save start time of each request, and will trigger finish
after response is sent to client. 它将保存每个请求的开始时间,并在响应发送到客户端后触发finish
。
Thanks for user419127 pointing to 'finish' event 感谢user419127指向'完成'事件
What about a performance measuring tool like Apache JMeter . 那么像Apache JMeter这样的性能测量工具呢? You can easily use it to simulate a (heavy) load on your server and then measure response times and other performance indicators. 您可以轻松地使用它来模拟服务器上的(重)负载,然后测量响应时间和其他性能指标。 It provides multiple graphical representations for this. 它为此提供了多种图形表示。
This Blog Post shows how you can setup an HTTP based performance test for Web-APIs. 本博文显示了如何为Web-API设置基于HTTP的性能测试。 I do it this way to test my RESTful webservices. 我这样做是为了测试我的RESTful webservices。
Keep it simple use the console.time('timerName');
保持简单使用console.time('timerName');
console.timeEnd('timerName')
features in Node. console.timeEnd('timerName')
在Node中的功能。 'timerName' is obviously configurable. 'timerName'显然是可配置的。
Example: 例:
console.time('getCustomers');
console.timeEnd('getCustomers')
Output: 输出:
getCustomers: 58ms
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.