简体   繁体   English

NodeJS MySQL:测量查询执行时间

[英]NodeJS MySQL: measure query execution time

I'm on a shared hosting platform and would like to throttle the queries in my app so that if the total execution time exceeds a certain amount over a variable time period then I can make the app cool off and then resume later on.我在一个共享托管平台上,希望限制我的应用程序中的查询,以便如果总执行时间在可变时间段内超过一定数量,那么我可以让应用程序冷却,然后在稍后恢复。

To do this I would like to find out how long each of my queries take in real time and manage it within the app and not by profiling it externally.为此,我想了解我的每个查询实时需要多长时间并在应用程序内对其进行管理,而不是通过在外部对其进行分析。

I've seen examples in PHP where the time is recorded before and after the query ( even phpMyAdmin does this ), but this won't work in NodeJS or anything that runs the query asynchronously.我在 PHP 中看到过一些示例,其中在查询之前和之后记录时间( 甚至 phpMyAdmin 也这样做),但这在 NodeJS 或任何异步运行查询的东西中都不起作用。

So the question is: how would I go about getting the actual execution time of a query in NodeJS?所以问题是:我将如何在 NodeJS 中获取查询的实际执行时间?

For reference I am using this module to query the MySQL db: https://github.com/felixge/node-mysql/作为参考,我使用这个模块来查询 MySQL 数据库: https : //github.com/felixge/node-mysql/

One option is just to timestamp before the query and timestamp after the query and check the difference like this:一种选择是在查询之前和查询之后加上时间戳并检查差异,如下所示:

// get a timestamp before running the query
var pre_query = new Date().getTime();
// run the job
connection.query(query, function(err, rows, fields) {
  // get a timestamp after running the query
  var post_query = new Date().getTime();
  // calculate the duration in seconds
  var duration = (post_query - pre_query) / 1000;
});

If you are using rest api then you can use postman.如果您使用的是rest api,那么您可以使用邮递员。 Run the query in postman and then look for Time: on middle right side of section as show in image attached.在邮递员中运行查询,然后在部分的中间右侧查找时间:如附图所示。 在此处输入图片说明

console.time('100-elements');
for (let i = 0; i < 100; i++) {}
console.timeEnd('100-elements');
// prints 100-elements: 225.438ms

Label is must be unique and same as start and End time标签必须是唯一的并且与开始和结束时间相同

Working well in nodejs.在 nodejs 中运行良好。

here is documentation on nodejs这是关于 nodejs 的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM