node/test/fixtures/compile-cache-flush.js
Joyee Cheung 62383cd113 module: implement flushCompileCache()
This implements an API for users to intentionally flush the
accumulated compile cache instead of waiting until process
shutdown. It may be useful for application that loads dependencies
first and then either reload itself in other instances, or spawning
other instances that load an overlapping set of its dependencies -
in this case its useful to flush the cache early instead of waiting
until the shutdown of itself.

Currently flushing is triggered by either process
shutdown or user requests. In the future we should simply start the
writes right after module loading on a separate thread, and this method
only blocks until all the pending writes (if any) on the other thread
are finished. In that case, the off-thread writes should finish long
before any attempt of flushing is made so the method would then only
incur a negligible overhead from thread synchronization.

PR-URL: https://github.com/nodejs/node/pull/54971
Fixes: https://github.com/nodejs/node/issues/54770
Fixes: https://github.com/nodejs/node/issues/54465
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2024-09-20 19:05:07 +00:00

22 lines
799 B
JavaScript

'use strict';
const { flushCompileCache, getCompileCacheDir } = require('module');
const { spawnSync } = require('child_process');
const assert = require('assert');
if (process.argv[2] !== 'child') {
// The test should be run with the compile cache already enabled and NODE_DEBUG_NATIVE=COMPILE_CACHE.
assert(getCompileCacheDir());
assert(process.env.NODE_DEBUG_NATIVE.includes('COMPILE_CACHE'));
flushCompileCache();
const child1 = spawnSync(process.execPath, [__filename, 'child']);
console.log(child1.stderr.toString().trim().split('\n').map(line => `[child1]${line}`).join('\n'));
flushCompileCache();
const child2 = spawnSync(process.execPath, [__filename, 'child']);
console.log(child2.stderr.toString().trim().split('\n').map(line => `[child2]${line}`).join('\n'));
}