0

I am trying to measure execution times of my query. The problem is that the measuting times don't actually incorperate the query it just skips. For example my page loads for 2 seconds but in the console i get 24 miliseconds. Have tried in multiple methods as shown in the code below but none of them work.

Have also gone through multiple already made questions on stackoverflow and other websites but can't find the right solution.

I am also measuring execution times of a MongoDB query in Node.js but there it works. I don't why it doesn't work here and tried everything.

console.time("time");
var startTime = performance.now();
var startTime2 = Date.now();
var startTime3 = new Date().getTime();
var startTime4 = process.hrtime();
for(var i = 0; i < 10000; i++){


const selectForum = conn.query(
  "SELECT forum_id, forum_name, post_date, forum.user_id, user.user_id, user_name FROM forum LEFT JOIN user ON forum.user_id=user.user_id ORDER BY forum_id DESC"
);


}
var endTime = performance.now();

console.timeEnd("time");

console.log(`${endTime-startTime} ms with performance.now`);

var endTime2 = Date.now();
console.log(`${endTime2-startTime2} ms with Date`);
//with getTime
var endTime3 = new Date().getTime();
console.log(`${endTime3-startTime3} ms with getTime`);
//with hrtime
var endTime4 = process.hrtime(startTime4);
console.log(`${(endTime4[1] - endTime4[0]) / 1000000 } ms with hrtime`);

This is my full query i am using but it still doesn't work.

console.time("time");
var startTime = performance.now();
for(var i = 0; i < 10000; i++){

const selectForum = conn.query(
  "SELECT forum_id, forum_name, post_date, forum.user_id, user.user_id, user_name FROM forum LEFT JOIN user ON forum.user_id=user.user_id ORDER BY forum_id DESC", function (err, result){
  if (result && result.length) {
    console.log(result);
  } else {
    console.log(err);
  }
});

}
var endTime = performance.now();

console.timeEnd("time");

console.log(`${endTime-startTime} ms with performance.now`);
Anzeko
  • 15
  • 5
  • Javascript is asynchronous. Stuff like calls to `conn.query()` doesn't finish until it calls the callback (which you omitted from your sample code). But the call to `conn.query()` returns instantaneously. – O. Jones Feb 01 '22 at 11:35
  • Thanks. Will try to make a callback allthough i don't really know how. I have tried making a call back but i don't know how to properly make one. – Anzeko Feb 01 '22 at 11:53
  • Tutorials abound. For example, https://www.mysqltutorial.org/mysql-nodejs/select/ With respect, if you don't know how callbacks and async / await work, you don't know much Javascript. This is worth your time to learn. – O. Jones Feb 01 '22 at 12:01
  • So I figured out that I was using this what u send me but Its still tha same. Maybe i put the time measuring functions in a wrong place? – Anzeko Feb 01 '22 at 12:20

0 Answers0