| 
 | 1 | +'use strict';  | 
 | 2 | + | 
 | 3 | +const { mustCall, platformTimeout, hasCrypto, skip, isSunOS } = require('../common');  | 
 | 4 | + | 
 | 5 | +if (!hasCrypto) {  | 
 | 6 | +  skip('missing crypto');  | 
 | 7 | +};  | 
 | 8 | + | 
 | 9 | +// This block can be removed once SmartOS support is fixed in  | 
 | 10 | +// https://github.com/libuv/libuv/issues/4706  | 
 | 11 | +// The behavior on SunOS is tested in  | 
 | 12 | +// test/parallel/test-process-threadCpuUsage-main-thread.js  | 
 | 13 | +if (isSunOS) {  | 
 | 14 | +  skip('Operation not supported yet on SmartOS');  | 
 | 15 | +}  | 
 | 16 | + | 
 | 17 | +const { ok } = require('assert');  | 
 | 18 | +const { randomBytes, createHash } = require('crypto');  | 
 | 19 | +const { once } = require('events');  | 
 | 20 | +const { Worker, parentPort, workerData } = require('worker_threads');  | 
 | 21 | + | 
 | 22 | +const FREQUENCIES = [100, 500, 1000];  | 
 | 23 | + | 
 | 24 | +function performLoad() {  | 
 | 25 | +  const buffer = randomBytes(1e8);  | 
 | 26 | + | 
 | 27 | +  // Do some work  | 
 | 28 | +  return setInterval(() => {  | 
 | 29 | +    createHash('sha256').update(buffer).end(buffer);  | 
 | 30 | +  }, platformTimeout(workerData?.frequency ?? 100));  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +function getUsages() {  | 
 | 34 | +  return { process: process.cpuUsage(), thread: process.threadCpuUsage() };  | 
 | 35 | +}  | 
 | 36 | + | 
 | 37 | +function validateResults(results) {  | 
 | 38 | +  // This test should have checked that the CPU usage of each thread is greater  | 
 | 39 | +  // than the previous one, while the process one was not.  | 
 | 40 | +  // Unfortunately, the real values are not really predictable on the CI so we  | 
 | 41 | +  // just check that all the values are positive numbers.  | 
 | 42 | +  for (let i = 0; i < 3; i++) {  | 
 | 43 | +    ok(typeof results[i].process.user === 'number');  | 
 | 44 | +    ok(results[i].process.user >= 0);  | 
 | 45 | + | 
 | 46 | +    ok(typeof results[i].process.system === 'number');  | 
 | 47 | +    ok(results[i].process.system >= 0);  | 
 | 48 | + | 
 | 49 | +    ok(typeof results[i].thread.user === 'number');  | 
 | 50 | +    ok(results[i].thread.user >= 0);  | 
 | 51 | + | 
 | 52 | +    ok(typeof results[i].thread.system === 'number');  | 
 | 53 | +    ok(results[i].thread.system >= 0);  | 
 | 54 | +  }  | 
 | 55 | +}  | 
 | 56 | + | 
 | 57 | +// The main thread will spawn three more threads, then after a while it will ask all of them to  | 
 | 58 | +// report the thread CPU usage and exit.  | 
 | 59 | +if (!workerData?.frequency) { // Do not use isMainThread here otherwise test will not run in --worker mode  | 
 | 60 | +  const workers = [];  | 
 | 61 | +  for (const frequency of FREQUENCIES) {  | 
 | 62 | +    workers.push(new Worker(__filename, { workerData: { frequency } }));  | 
 | 63 | +  }  | 
 | 64 | + | 
 | 65 | +  setTimeout(mustCall(async () => {  | 
 | 66 | +    clearInterval(interval);  | 
 | 67 | + | 
 | 68 | +    const results = [getUsages()];  | 
 | 69 | + | 
 | 70 | +    for (const worker of workers) {  | 
 | 71 | +      const statusPromise = once(worker, 'message');  | 
 | 72 | + | 
 | 73 | +      worker.postMessage('done');  | 
 | 74 | +      const [status] = await statusPromise;  | 
 | 75 | +      results.push(status);  | 
 | 76 | +      worker.terminate();  | 
 | 77 | +    }  | 
 | 78 | + | 
 | 79 | +    validateResults(results);  | 
 | 80 | +  }), platformTimeout(5000));  | 
 | 81 | + | 
 | 82 | +} else {  | 
 | 83 | +  parentPort.on('message', () => {  | 
 | 84 | +    clearInterval(interval);  | 
 | 85 | +    parentPort.postMessage(getUsages());  | 
 | 86 | +    process.exit(0);  | 
 | 87 | +  });  | 
 | 88 | +}  | 
 | 89 | + | 
 | 90 | +// Perform load on each thread  | 
 | 91 | +const interval = performLoad();  | 
0 commit comments