summaryrefslogtreecommitdiff
path: root/test/parallel/test-performance-measure.js
blob: 949258f96e1b2d4df20880cefd7c6f54b65c3a9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'use strict';

const common = require('../common');
const assert = require('assert');

const { PerformanceObserver, performance } = require('perf_hooks');
const DELAY = 1000;
const ALLOWED_MARGIN = 10;

const expected = ['Start to Now', 'A to Now', 'A to B'];
const obs = new PerformanceObserver(common.mustCall((items) => {
  items.getEntries().forEach(({ name, duration }) => {
    assert.ok(duration > (DELAY - ALLOWED_MARGIN));
    assert.strictEqual(expected.shift(), name);
  });
}));
obs.observe({ entryTypes: ['measure'] });

performance.mark('A');
setTimeout(common.mustCall(() => {
  performance.measure('Start to Now');
  performance.measure('A to Now', 'A');

  performance.mark('B');
  performance.measure('A to B', 'A', 'B');
}), DELAY);