0

I am trying to write script that will measure execution time for query. For now I have script that I will include at the end of the question, but it seems that she collects results, add result on previous etc. So I wanted to ask you, do you maybe now how can I avoid that, where do I make a mistake?

var mysql = require('mysql');
var client = mysql.createConnection({
user: 'root',
password: '',
database: 'kong_centar'
//debug: true  
});

var ignore = [mysql.ERROR_DB_CREATE_EXISTS,
          mysql.ERROR_TABLE_EXISTS_ERROR];

client.on('error', function (err) {
if (ignore.indexOf(err.number) + 1) { return; }
throw err;
});
for(i=0; i<10; i++){
var pre_query = new Date().getTime();
client.query('select * from korisnik', function(err, result, fields){
    var post_query = new Date().getTime();
    var duration = (post_query - pre_query) / 1000;

    console.log(duration);

    if (err)
    {
        throw err;
    }
    else
    {

        //console.log(result);
        for (var i in result)
        {
            //var people = result[i];
            //console.log(people.firstname + ' ' + people.lastname);
        }
    }
});
}
client.end();
  • You use the same `pre_query` for all queries because you use `var`. Use `const` to make the variable local scope. Tip: Don't _ever_ use `var` any more in ES 2015 Javascript and above. -- By the way, next time please properly indent your posted code. – Mörre Oct 01 '17 at 17:18
  • I replaced var with const, for pre query, post query and duration, but it still collects results.And the results look like this > 2.614 5.268 7.923 10.551 13.2 15.86 18.538 21.167 23.838 26.473 – Milica Pavlovic Oct 01 '17 at 17:45
  • To debug you could add `console.log(pre_query)` (just before or after calculating duration).to check the variable's contents. By the way, unrelated, you can use `Date.now()` and save a few characters :-) – Mörre Oct 01 '17 at 17:47
  • Oh and maybe you should use the node.js `console.time` functions instead, since you log it in the end anyway: https://nodejs.org/dist/latest-v8.x/docs/api/console.html -- Note that you need a different label for each individual timer, which you can construct using the index of the loop. – Mörre Oct 01 '17 at 17:48
  • Actually, your start times SHOULD be equal: You submit the SELECT queries all at once. What I don't know is if `client` does some internal queuing, in which case you have a problem, because then you cannot know when the query is really started. You only know when you _submitted_ it. – Mörre Oct 01 '17 at 17:52

1 Answers1

0

You are experiencing a well know problem, when functions are created within a loop. In essence, all functions will point to the same variable (pre_query). Have a look at this question for a description of the problem and a solution.

Olivier Liechti
  • 3,138
  • 2
  • 19
  • 26
  • 1
    Well-known but entirely avoidable these days: With `const` or `let` it will work. The old `var` hoists the variable definition to the beginning of the function, but with let/const it remains in the for block. – Mörre Oct 01 '17 at 17:18