11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciif (common.isIBMi) 41cb0ef41Sopenharmony_ci common.skip('On IBMi, the rss memory always returns zero'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst util = require('util'); 81cb0ef41Sopenharmony_ciconst { Worker } = require('worker_threads'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_cilet numWorkers = +process.env.JOBS || require('os').availableParallelism(); 111cb0ef41Sopenharmony_ciif (numWorkers > 20) { 121cb0ef41Sopenharmony_ci // Cap the number of workers at 20 (as an even divisor of 60 used as 131cb0ef41Sopenharmony_ci // the total number of workers started) otherwise the test fails on 141cb0ef41Sopenharmony_ci // machines with high core counts. 151cb0ef41Sopenharmony_ci numWorkers = 20; 161cb0ef41Sopenharmony_ci} 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci// Verify that a Worker's memory isn't kept in memory after the thread finishes. 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cifunction run(n, done) { 211cb0ef41Sopenharmony_ci console.log(`run() called with n=${n} (numWorkers=${numWorkers})`); 221cb0ef41Sopenharmony_ci if (n <= 0) 231cb0ef41Sopenharmony_ci return done(); 241cb0ef41Sopenharmony_ci const worker = new Worker( 251cb0ef41Sopenharmony_ci 'require(\'worker_threads\').parentPort.postMessage(2 + 2)', 261cb0ef41Sopenharmony_ci { eval: true }); 271cb0ef41Sopenharmony_ci worker.on('message', common.mustCall((value) => { 281cb0ef41Sopenharmony_ci assert.strictEqual(value, 4); 291cb0ef41Sopenharmony_ci })); 301cb0ef41Sopenharmony_ci worker.on('exit', common.mustCall(() => { 311cb0ef41Sopenharmony_ci run(n - 1, done); 321cb0ef41Sopenharmony_ci })); 331cb0ef41Sopenharmony_ci} 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciconst startStats = process.memoryUsage(); 361cb0ef41Sopenharmony_cilet finished = 0; 371cb0ef41Sopenharmony_cifor (let i = 0; i < numWorkers; ++i) { 381cb0ef41Sopenharmony_ci run(60 / numWorkers, () => { 391cb0ef41Sopenharmony_ci console.log(`done() called (finished=${finished})`); 401cb0ef41Sopenharmony_ci if (++finished === numWorkers) { 411cb0ef41Sopenharmony_ci const finishStats = process.memoryUsage(); 421cb0ef41Sopenharmony_ci // A typical value for this ratio would be ~1.15. 431cb0ef41Sopenharmony_ci // 5 as a upper limit is generous, but the main point is that we 441cb0ef41Sopenharmony_ci // don't have the memory of 50 Isolates/Node.js environments just lying 451cb0ef41Sopenharmony_ci // around somewhere. 461cb0ef41Sopenharmony_ci assert.ok(finishStats.rss / startStats.rss < 5, 471cb0ef41Sopenharmony_ci 'Unexpected memory overhead: ' + 481cb0ef41Sopenharmony_ci util.inspect([startStats, finishStats])); 491cb0ef41Sopenharmony_ci } 501cb0ef41Sopenharmony_ci }); 511cb0ef41Sopenharmony_ci} 52