-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: add utilities to help debugging reproducibility of snapshots
- Print offsets in blob serializer - Add a special node:generate_default_snapshot ID to generate the built-in snapshot. - Improve logging - Add a test to check the reproducibilty of the snapshot PR-URL: #50983 Refs: nodejs/build#3043 Reviewed-By: Daniel Lemire <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
004b9ea
commit f077afa
Showing
4 changed files
with
110 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const { spawnSyncAndAssert } = require('../common/child_process'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
|
||
// When the test fails this helper can be modified to write outputs | ||
// differently and aid debugging. | ||
function log(line) { | ||
console.log(line); | ||
} | ||
|
||
function generateSnapshot() { | ||
tmpdir.refresh(); | ||
|
||
spawnSyncAndAssert( | ||
process.execPath, | ||
[ | ||
'--random_seed=42', | ||
'--predictable', | ||
'--build-snapshot', | ||
'node:generate_default_snapshot', | ||
], | ||
{ | ||
env: { ...process.env, NODE_DEBUG_NATIVE: 'SNAPSHOT_SERDES' }, | ||
cwd: tmpdir.path | ||
}, | ||
{ | ||
stderr(output) { | ||
const lines = output.split('\n'); | ||
for (const line of lines) { | ||
if (line.startsWith('0x')) { | ||
log(line); | ||
} | ||
} | ||
}, | ||
} | ||
); | ||
const blobPath = tmpdir.resolve('snapshot.blob'); | ||
return fs.readFileSync(blobPath); | ||
} | ||
|
||
const buf1 = generateSnapshot(); | ||
const buf2 = generateSnapshot(); | ||
|
||
const diff = []; | ||
let offset = 0; | ||
const step = 16; | ||
do { | ||
const length = Math.min(buf1.length - offset, step); | ||
const slice1 = buf1.slice(offset, offset + length).toString('hex'); | ||
const slice2 = buf2.slice(offset, offset + length).toString('hex'); | ||
if (slice1 !== slice2) { | ||
diff.push({ offset: '0x' + (offset).toString(16), slice1, slice2 }); | ||
} | ||
offset += length; | ||
} while (offset < buf1.length); | ||
|
||
assert.strictEqual(offset, buf1.length); | ||
if (offset < buf2.length) { | ||
const length = Math.min(buf2.length - offset, step); | ||
const slice2 = buf2.slice(offset, offset + length).toString('hex'); | ||
diff.push({ offset, slice1: '', slice2 }); | ||
offset += length; | ||
} while (offset < buf2.length); | ||
|
||
assert.deepStrictEqual(diff, []); | ||
assert.strictEqual(buf1.length, buf2.length); |