简体   繁体   English

使用NodeJS,MySQL测量查询执行时间

[英]Measure query execution time using NodeJS, MySQL

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 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). 本质上,所有函数都将指向相同的变量(pre_query)。 Have a look at this question for a description of the problem and a solution. 请查看此问题以获取问题描述和解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM