简体   繁体   English

计算多次调用的异步函数的执行时间

[英]calculate execution times of async function over multiple calls

If the do work function was performing some operation, say, picking an item from queue and performing some operation. 如果do work函数正在执行某些操作,比如从队列中挑选一个项目并执行某些操作。 How would I get the execution times for doWork function over time. 如何随着时间的推移获得doWork函数的执行时间。 I want to find out how much time doWork takes to complete at an average. 我想知道doWork在平均时间内完成了多少时间。

Sample Code 示例代码

function doWork () {
  return Promise.resolve({first: 'Tony', last: 'Starks'})
}

async function wrapper () {
  console.time('wrapper')
  const response = await doWork()
  console.timeEnd('wrapper')
  return response
}

Promise.all([
  wrapper(),
  wrapper(),
  wrapper()
]).then((result) => console.info(result))

Output 产量

wrapper: 0.388ms
[ { first: 'Tony', last: 'Starks' },
  { first: 'Tony', last: 'Starks' },
  { first: 'Tony', last: 'Starks' } ]
(node:2749) Warning: No such label 'wrapper' for console.timeEnd()
(node:2749) Warning: No such label 'wrapper' for console.timeEnd()

If you're just trying to get your console.time() and console.end() calls to work around your async function, you can generate a unique label each time you call console.time() and console.end() so that the measurement will still work when there are multiple calls in flight at the same time (since each call will then be using its own label): 如果你只是想让你的console.time()console.end()调用来解决你的异步函数,你可以在每次调用console.time()console.end()时生成一个唯一的标签。当同时有多个呼叫在飞行中时,测量仍然有效(因为每个呼叫将使用自己的标签):

let wrapperCntr = 0;

async function wrapper () {
  let cnt = wrapperCntr++;
  console.time('wrapper' + cnt);
  const response = await doWork();
  console.timeEnd('wrapper' + cnt);
  return response;
}

If you're hard-set on testing them in parallel, I recommend this approach: 如果你很难并行测试它们,我推荐这种方法:

 function doWork () { return Promise.resolve({ first: 'Tony', last: 'Stank' }) } async function wrapper (index) { console.time(index) const response = await doWork() console.timeEnd(index) return response } Promise.all( Array(3) // some big number .fill(wrapper) .map((wrapper, index) => wrapper(index)) ).then((results) => console.info(results)) 

However, JavaScript is single-threaded. 但是,JavaScript是单线程的。 You're initializing 3 asynchronous functions in the same tick. 您正在同一个tick中初始化3个异步函数。 They end up competing for CPU time on each asynchronous callback, which as you observed, causes undue timing delays. 它们最终在每个异步回调上竞争CPU时间,正如您所观察到的那样,会导致不适当的时序延迟。

Compare the times for above and below; 比较上下的时间; below will be an order of magnitude faster because they are initialized in series and do not compete for CPU time: 以下将快一个数量级,因为它们是串行初始化的,不会争夺CPU时间:

 function doWork () { return Promise.resolve({ first: 'Tony', last: 'Stank' }) } async function wrapper ({ index, responses }) { console.time(index) responses.push(await doWork()) console.timeEnd(index) return { index: ++index, responses } } Array(3) // some big number .fill(wrapper) .reduce( (promise, wrapper) => promise.then(wrapper), Promise.resolve({ index: 0, responses: [] }) ) .then(({ responses: results }) => console.info(results)) 

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

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