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.
So the question is: how would I go about getting the actual execution time of a query in NodeJS?
For reference I am using this module to query the MySQL db: 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;
});
来源:https://stackoverflow.com/questions/26141129/nodejs-mysql-measure-query-execution-time