NodeJS MySQL:测量查询执行时间

我在一个共享的主机平台,并希望限制在我的应用程序的查询,以便如果总的执行时间超过一定的数量在一个可变的时间段,那么我可以让应用冷静,然后恢复。

要做到这一点,我想知道我的每个查询需要多长时间,并在应用程序内进行pipe理,而不是通过外部分析。

我已经看到了在PHP查询前后logging时间( 甚至phpMyAdmin这样做 )的例子 ,但是这不会在NodeJS或asynchronous运行查询的任何东西。

所以问题是:我将如何去得到在NodeJS查询的实际执行时间?

为了参考,我使用这个模块来查询MySQL数据库: https : //github.com/felixge/node-mysql/

一个选项是在查询之后的时间戳之前加上时间戳,然后检查差异,如下所示:

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