Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

node.js - 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.

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/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...