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也是这样做的),但是这在node . js或任何异步运行查询的东西中都行不通。
So the question is: how would I go about getting the actual execution time of a query in NodeJS?
因此,问题是:如何在node . js中获取查询的实际执行时间?
For reference I am using this module to query the MySQL db: https://github.com/felixge/node-mysql/
作为参考,我正在使用这个模块查询MySQL db: https://github.com/felixge/node-mysql/
1 个解决方案
#1
4
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;
});
#1
4
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;
});