Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions benchmark/misc/compile-cache-timing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

// Startup benchmark for the compile cache (including the zstd dictionary).
// Compares no-cache / cold-cache / warm-cache for two workloads:
// big - one large module (the typescript.js fixture)
// many - many small modules (generated here, side-effect-free)
// The modules are generated into a temp dir so the benchmark is self-contained
// and reproducible, and never executes unrelated code.

const common = require('../common.js');
const { spawnSync } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');

const bench = common.createBenchmark(main, {
workload: ['big', 'many'],
cache: ['none', 'cold', 'warm'],
n: [30],
});

const BIG = path.resolve(__dirname, '../../test/fixtures/snapshot/typescript.js');

// Generate `count` small, side-effect-free modules and return the require()
// code that loads them all in one child.
function makeManyModules(dir, count) {
fs.mkdirSync(dir, { recursive: true });
const reqs = [];
for (let i = 0; i < count; i++) {
const file = path.join(dir, `mod-${i}.js`);
fs.writeFileSync(
file,
`'use strict';\n` +
`module.exports = function value${i}(a, b) {\n` +
` const sum = a + b + ${i};\n` +
` return { id: ${i}, sum, label: 'module-${i}' };\n` +
`};\n`);
reqs.push(`require(${JSON.stringify(file)});`);
}
return reqs.join('');
}

function run(cmd, args, cacheDir) {
const env = { ...process.env };
if (cacheDir) env.NODE_COMPILE_CACHE = cacheDir;
else delete env.NODE_COMPILE_CACHE;
const child = spawnSync(cmd, args, { env, stdio: 'ignore' });
if (child.error) throw child.error;
}

function main({ n, workload, cache }) {
const cmd = process.execPath || process.argv[0];
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'cc-bench-'));
const args = workload === 'big' ?
[BIG] :
['-e', makeManyModules(path.join(tmp, 'mods'), 120)];
const cacheDir = cache === 'none' ? null : path.join(tmp, 'cache');

try {
if (cache === 'warm') run(cmd, args, cacheDir); // populate once
bench.start();
for (let i = 0; i < n; i++) {
if (cache === 'cold' && cacheDir) {
fs.rmSync(cacheDir, { recursive: true, force: true });
}
run(cmd, args, cacheDir);
}
bench.end(n);
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}
}
17 changes: 17 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,22 @@
'<@(linked_module_files)',
],
},
{
'action_name': 'generate_compile_cache_zstd_dict',
'inputs': [
'src/compile_cache_zstd.dict',
'tools/generate_compile_cache_dict.py',
],
'outputs': [
'<(SHARED_INTERMEDIATE_DIR)/compile_cache_zstd_dict.h',
],
'action': [
'<(python)',
'tools/generate_compile_cache_dict.py',
'src/compile_cache_zstd.dict',
'<@(_outputs)',
],
},
],
}, # node_base
{
Expand All @@ -1123,6 +1139,7 @@
'src',
'deps/v8/include',
'deps/uv/include',
'<(SHARED_INTERMEDIATE_DIR)', # for compile_cache_zstd_dict.h etc.
],

'dependencies': [
Expand Down
Loading
Loading