How to use perf_hooks to measure async operations?

173
May 23, 2019, at 10:10 PM

Here is a code that describes two async operations performed by the foo method:

const foo = async () => {
  await new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, Math.random() * 100);
  });
  await new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, 100);
  });
};
Promise.all([
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo()
])

I want to measure how long does each async operation takes and then how long does each foo execution takes in total.

My first attempt was:

const { PerformanceObserver, performance } = require('perf_hooks');
const obs = new PerformanceObserver((items) => {
  console.log('performance-observer', items.getEntries());
});
obs.observe({
  entryTypes: ['measure']
});
const foo = async () => {
  performance.mark('A');
  await new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, Math.random() * 100);
  });
  performance.mark('B');
  await new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, 100);
  });
  performance.mark('C');
  performance.measure('A to B', 'A', 'B');
  performance.measure('A to C', 'A', 'C');
  performance.measure('B to C', 'B', 'C');
  performance.clearMarks();
};
Promise.all([
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo(),
  foo()
]);

However, this (as expected) throws an error:

(node:29277) UnhandledPromiseRejectionWarning: Error [ERR_INVALID_PERFORMANCE_MARK]: The "B" performance mark has not been set
    at Performance.measure (perf_hooks.js:396:13)
    at foo (/Users/gajus/Documents/dev/temp/test.js:30:15)
    at async Promise.all (index 0)

This happens because there is no relation between an instance of PerformanceObserver and individual performance marks.

How to use perf_hooks to measure async operations?

Rent Charter Buses Company
READ ALSO
How to perform equals operation on two string variables in node.js

How to perform equals operation on two string variables in node.js

Could some one please let me know how to check if two variables have same value in it or not

136
How does Tinder know who's been seen? (Backend)

How does Tinder know who's been seen? (Backend)

I have a dynamodb source of modelsNow I query them via ElasticSearch, so I can make a geo search

143
Javascript: Is there any way to clear history of user input?

Javascript: Is there any way to clear history of user input?

I'm creating a Simon Says game where the pattern gets progressively longer as the user gets the pattern correct, but the input history shows their previous pattern inputsHow do I clear this history?

130
Cant figure out how to get bootstrap table to work with electron renderer

Cant figure out how to get bootstrap table to work with electron renderer

I am trying to integrate bootstrap data table with an electron appI got the table itself up and running on the HTML side but I can't seem to figure out the way to get the renderer (js) to communicate with the DataTable element

139