nodejs 获取mysql时间格式化,NodeJS MySQL:衡量查询执行时间

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/

解决方案

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;

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值