简体   繁体   English

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

[英]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.

相关问题 在SailsJs中同时开发Rest API和服务器端渲染 - Develop Rest API and server-side rendering simultaneously in SailsJs 通过性能API测量现场负载时间 - Measuring Site Load Times via performance api 切换到服务器端分页之前,REST / JSON中有多少项响应 - How many items in REST/JSON response before switching to server-side paging 配置Ajax调用以从Json Rest API进行服务器端数据过滤 - Configure Ajax call for Server-side filtering of Data from Json Rest API Node / Express - 使用API​​ JSON响应(服务器端)呈现应用程序 - Node/Express - use API JSON response to (server-side) render the app 调用Google Visualization API服务器端 - Calling Google Visualization API Server-Side 如何发出服务器端API请求? - How to make server-side API requests? 在服务器端存储API访问令牌 - Storing API access token server-side 如何在 Apollo Server 中设置服务器端完整响应缓存 - How to setup server-side full response caching in Apollo Server 服务器端的javascript函数在mongoDB中是否存在性能问题? - Does server-side javascript function have performance issues in mongoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM