4

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/

Hayko Koryun
  • 1,214
  • 2
  • 12
  • 30

3 Answers3

8

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;
});
Benjamin Kaiser
  • 2,177
  • 22
  • 24
  • 4
    The problem with this approach is that the query doesn't necessarily execute as soon as you call the `connection.query` function, so the `duration` includes the time NodeJS spent doing something else until the query was actually executed – Hayko Koryun Jan 24 '15 at 18:34
  • 1
    That's true. However if you really wanted to monitor it more closely you could modify the source for node-mysql. Just move into the node_modules folder and edit the parts of the files that trigger the query and wait for it to return. It may be in this file: https://github.com/felixge/node-mysql/blob/master/lib/protocol/sequences/Query.js This is still including the delay between nodejs and mysql, but it would be negligible compared to most query execution times for small response sets. – Benjamin Kaiser Jan 25 '15 at 11:06
  • cant you use ```console.timeLog()``` – Srinath Kamath Feb 25 '21 at 10:15
  • 1
    @SrinathKamath yes you can indeed. It was added in Node v10.7.0. Which was released 4 years after I wrote this answer – Benjamin Kaiser Mar 01 '21 at 01:10
  • Also I'm not sure timeLog would be useful here if you need to do some calculation with the duration, since it only prints it out. The performance API could be useful but also brings with it it's own complications. – Benjamin Kaiser Mar 01 '21 at 01:23
1
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.

here is documentation on nodejs

Praveen Patel
  • 199
  • 1
  • 8
0

If you are using rest api then you can use postman. Run the query in postman and then look for Time: on middle right side of section as show in image attached.enter image description here

DragonFire
  • 3,722
  • 2
  • 38
  • 51