1'use strict';
2require('../common');
3const assert = require('assert');
4
5const start = process.cpuUsage();
6
7// Run a busy-loop for specified # of milliseconds.
8const RUN_FOR_MS = 500;
9
10// Define slop factor for checking maximum expected diff values.
11const SLOP_FACTOR = 2;
12
13// Run a busy loop.
14const now = Date.now();
15while (Date.now() - now < RUN_FOR_MS);
16
17// Get a diff reading from when we started.
18const diff = process.cpuUsage(start);
19
20const MICROSECONDS_PER_MILLISECOND = 1000;
21
22// Diff usages should be >= 0, <= ~RUN_FOR_MS millis.
23// Let's be generous with the slop factor, defined above, in case other things
24// are happening on this CPU. The <= check may be invalid if the node process
25// is making use of multiple CPUs, in which case, just remove it.
26assert(diff.user >= 0);
27assert(diff.user <= SLOP_FACTOR * RUN_FOR_MS * MICROSECONDS_PER_MILLISECOND);
28
29assert(diff.system >= 0);
30assert(diff.system <= SLOP_FACTOR * RUN_FOR_MS * MICROSECONDS_PER_MILLISECOND);
31