|
| 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 | +constFREQUENCIES=[100,500,1000]; |
| 23 | + |
| 24 | +functionperformLoad(){ |
| 25 | +constbuffer=randomBytes(1e8); |
| 26 | + |
| 27 | +// Do some work |
| 28 | +returnsetInterval(()=>{ |
| 29 | +createHash('sha256').update(buffer).end(buffer); |
| 30 | +},platformTimeout(workerData?.frequency??100)); |
| 31 | +} |
| 32 | + |
| 33 | +functiongetUsages(){ |
| 34 | +return{process: process.cpuUsage(),thread: process.threadCpuUsage()}; |
| 35 | +} |
| 36 | + |
| 37 | +functionvalidateResults(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(leti=0;i<3;i++){ |
| 43 | +ok(typeofresults[i].process.user==='number'); |
| 44 | +ok(results[i].process.user>=0); |
| 45 | + |
| 46 | +ok(typeofresults[i].process.system==='number'); |
| 47 | +ok(results[i].process.system>=0); |
| 48 | + |
| 49 | +ok(typeofresults[i].thread.user==='number'); |
| 50 | +ok(results[i].thread.user>=0); |
| 51 | + |
| 52 | +ok(typeofresults[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 | +constworkers=[]; |
| 61 | +for(constfrequencyofFREQUENCIES){ |
| 62 | +workers.push(newWorker(__filename,{workerData: { frequency }})); |
| 63 | +} |
| 64 | + |
| 65 | +setTimeout(mustCall(async()=>{ |
| 66 | +clearInterval(interval); |
| 67 | + |
| 68 | +constresults=[getUsages()]; |
| 69 | + |
| 70 | +for(constworkerofworkers){ |
| 71 | +conststatusPromise=once(worker,'message'); |
| 72 | + |
| 73 | +worker.postMessage('done'); |
| 74 | +const[status]=awaitstatusPromise; |
| 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 | +constinterval=performLoad(); |
0 commit comments