2018-11-09 02:34:18 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const URL = require('url');
|
|
|
|
const debug = require('debug');
|
|
|
|
const pptr = require('..');
|
|
|
|
const browserFetcher = pptr.createBrowserFetcher();
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2022-06-22 13:25:44 +00:00
|
|
|
const {fork, spawn, execSync} = require('child_process');
|
2018-11-09 02:34:18 +00:00
|
|
|
|
|
|
|
const COLOR_RESET = '\x1b[0m';
|
|
|
|
const COLOR_RED = '\x1b[31m';
|
|
|
|
const COLOR_GREEN = '\x1b[32m';
|
|
|
|
const COLOR_YELLOW = '\x1b[33m';
|
|
|
|
|
|
|
|
const argv = require('minimist')(process.argv.slice(2), {});
|
|
|
|
|
|
|
|
const help = `
|
|
|
|
Usage:
|
|
|
|
node bisect.js --good <revision> --bad <revision> <script>
|
|
|
|
|
|
|
|
Parameters:
|
2022-08-11 18:45:52 +00:00
|
|
|
--good revision that is known to be GOOD, defaults to the chromium revision in src/revision.ts in the main branch
|
2021-09-14 15:21:06 +00:00
|
|
|
--bad revision that is known to be BAD, defaults to the chromium revision in src/revision.ts
|
|
|
|
--no-cache do not keep downloaded Chromium revisions
|
|
|
|
--unit-test pattern that identifies a unit tests that should be checked
|
|
|
|
--script path to a script that returns non-zero code for BAD revisions and 0 for good
|
2018-11-09 02:34:18 +00:00
|
|
|
|
|
|
|
Example:
|
2022-10-06 08:27:14 +00:00
|
|
|
node tools/bisect.js --unit-test test
|
|
|
|
node tools/bisect.js --good 577361 --bad 599821 --script simple.js
|
|
|
|
node tools/bisect.js --good 577361 --bad 599821 --unit-test test
|
2018-11-09 02:34:18 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
if (argv.h || argv.help) {
|
|
|
|
console.log(help);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof argv.good !== 'number') {
|
2021-09-14 15:21:06 +00:00
|
|
|
argv.good = getChromiumRevision('main');
|
|
|
|
if (typeof argv.good !== 'number') {
|
|
|
|
console.log(
|
|
|
|
COLOR_RED +
|
|
|
|
'ERROR: Could not parse current Chromium revision' +
|
|
|
|
COLOR_RESET
|
|
|
|
);
|
|
|
|
console.log(help);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-11-09 02:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof argv.bad !== 'number') {
|
2021-09-14 15:21:06 +00:00
|
|
|
argv.bad = getChromiumRevision();
|
|
|
|
if (typeof argv.bad !== 'number') {
|
|
|
|
console.log(
|
|
|
|
COLOR_RED +
|
|
|
|
'ERROR: Could not parse Chromium revision in the main branch' +
|
|
|
|
COLOR_RESET
|
|
|
|
);
|
|
|
|
console.log(help);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!argv.script && !argv['unit-test']) {
|
2020-05-07 10:54:55 +00:00
|
|
|
console.log(
|
2021-09-14 15:21:06 +00:00
|
|
|
COLOR_RED +
|
|
|
|
'ERROR: Expected to be given a script or a unit test to run' +
|
|
|
|
COLOR_RESET
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2018-11-09 02:34:18 +00:00
|
|
|
console.log(help);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:21:06 +00:00
|
|
|
const scriptPath = argv.script ? path.resolve(argv.script) : null;
|
|
|
|
|
|
|
|
if (argv.script && !fs.existsSync(scriptPath)) {
|
2020-05-07 10:54:55 +00:00
|
|
|
console.log(
|
|
|
|
COLOR_RED +
|
|
|
|
'ERROR: Expected to be given a path to a script to run' +
|
|
|
|
COLOR_RESET
|
|
|
|
);
|
2018-11-09 02:34:18 +00:00
|
|
|
console.log(help);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:21:06 +00:00
|
|
|
(async (scriptPath, good, bad, pattern, noCache) => {
|
2018-11-09 02:34:18 +00:00
|
|
|
const span = Math.abs(good - bad);
|
2020-05-07 10:54:55 +00:00
|
|
|
console.log(
|
|
|
|
`Bisecting ${COLOR_YELLOW}${span}${COLOR_RESET} revisions in ${COLOR_YELLOW}~${
|
|
|
|
span.toString(2).length
|
|
|
|
}${COLOR_RESET} iterations`
|
|
|
|
);
|
2018-11-09 02:34:18 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const middle = Math.round((good + bad) / 2);
|
|
|
|
const revision = await findDownloadableRevision(middle, good, bad);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!revision || revision === good || revision === bad) {
|
|
|
|
break;
|
|
|
|
}
|
2018-11-09 02:34:18 +00:00
|
|
|
let info = browserFetcher.revisionInfo(revision);
|
2021-09-14 15:21:06 +00:00
|
|
|
const shouldRemove = noCache && !info.local;
|
2018-11-09 02:34:18 +00:00
|
|
|
info = await downloadRevision(revision);
|
2021-09-14 15:21:06 +00:00
|
|
|
const exitCode = await (pattern
|
|
|
|
? runUnitTest(pattern, info)
|
|
|
|
: runScript(scriptPath, info));
|
2022-06-14 11:55:35 +00:00
|
|
|
if (shouldRemove) {
|
|
|
|
await browserFetcher.remove(revision);
|
|
|
|
}
|
2018-11-09 02:34:18 +00:00
|
|
|
let outcome;
|
|
|
|
if (exitCode) {
|
|
|
|
bad = revision;
|
|
|
|
outcome = COLOR_RED + 'BAD' + COLOR_RESET;
|
|
|
|
} else {
|
|
|
|
good = revision;
|
|
|
|
outcome = COLOR_GREEN + 'GOOD' + COLOR_RESET;
|
|
|
|
}
|
|
|
|
const span = Math.abs(good - bad);
|
2018-11-09 03:17:56 +00:00
|
|
|
let fromText = '';
|
|
|
|
let toText = '';
|
|
|
|
if (good < bad) {
|
|
|
|
fromText = COLOR_GREEN + good + COLOR_RESET;
|
|
|
|
toText = COLOR_RED + bad + COLOR_RESET;
|
|
|
|
} else {
|
|
|
|
fromText = COLOR_RED + bad + COLOR_RESET;
|
|
|
|
toText = COLOR_GREEN + good + COLOR_RESET;
|
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
console.log(
|
|
|
|
`- ${COLOR_YELLOW}r${revision}${COLOR_RESET} was ${outcome}. Bisecting [${fromText}, ${toText}] - ${COLOR_YELLOW}${span}${COLOR_RESET} revisions and ${COLOR_YELLOW}~${
|
|
|
|
span.toString(2).length
|
|
|
|
}${COLOR_RESET} iterations`
|
|
|
|
);
|
2018-11-09 02:34:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 03:17:56 +00:00
|
|
|
const [fromSha, toSha] = await Promise.all([
|
|
|
|
revisionToSha(Math.min(good, bad)),
|
|
|
|
revisionToSha(Math.max(good, bad)),
|
2018-11-09 02:34:18 +00:00
|
|
|
]);
|
2020-05-07 10:54:55 +00:00
|
|
|
console.log(
|
|
|
|
`RANGE: https://chromium.googlesource.com/chromium/src/+log/${fromSha}..${toSha}`
|
|
|
|
);
|
2021-09-14 15:21:06 +00:00
|
|
|
})(scriptPath, argv.good, argv.bad, argv['unit-test'], argv['no-cache']);
|
2018-11-09 02:34:18 +00:00
|
|
|
|
|
|
|
function runScript(scriptPath, revisionInfo) {
|
|
|
|
const log = debug('bisect:runscript');
|
|
|
|
log('Running script');
|
|
|
|
const child = fork(scriptPath, [], {
|
|
|
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
|
|
|
env: {
|
2019-08-13 23:28:09 +00:00
|
|
|
...process.env,
|
2018-11-09 02:34:18 +00:00
|
|
|
PUPPETEER_EXECUTABLE_PATH: revisionInfo.executablePath,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-06-22 13:25:44 +00:00
|
|
|
child.on('error', err => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return reject(err);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
child.on('exit', code => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return resolve(code);
|
|
|
|
});
|
2018-11-09 02:34:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:21:06 +00:00
|
|
|
function runUnitTest(pattern, revisionInfo) {
|
|
|
|
const log = debug('bisect:rununittest');
|
|
|
|
log('Running unit test');
|
2022-08-11 18:45:52 +00:00
|
|
|
const child = spawn('npm run test:chrome:headless', ['--', '-g', pattern], {
|
2021-09-14 15:21:06 +00:00
|
|
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
|
|
|
shell: true,
|
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
PUPPETEER_EXECUTABLE_PATH: revisionInfo.executablePath,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-06-22 13:25:44 +00:00
|
|
|
child.on('error', err => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return reject(err);
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
child.on('exit', code => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return resolve(code);
|
|
|
|
});
|
2021-09-14 15:21:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-09 02:34:18 +00:00
|
|
|
async function downloadRevision(revision) {
|
|
|
|
const log = debug('bisect:download');
|
|
|
|
log(`Downloading ${revision}`);
|
|
|
|
let progressBar = null;
|
|
|
|
let lastDownloadedBytes = 0;
|
2020-05-07 10:54:55 +00:00
|
|
|
return await browserFetcher.download(
|
|
|
|
revision,
|
|
|
|
(downloadedBytes, totalBytes) => {
|
|
|
|
if (!progressBar) {
|
|
|
|
const ProgressBar = require('progress');
|
|
|
|
progressBar = new ProgressBar(
|
|
|
|
`- downloading Chromium r${revision} - ${toMegabytes(
|
|
|
|
totalBytes
|
|
|
|
)} [:bar] :percent :etas `,
|
|
|
|
{
|
|
|
|
complete: '=',
|
|
|
|
incomplete: ' ',
|
|
|
|
width: 20,
|
|
|
|
total: totalBytes,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const delta = downloadedBytes - lastDownloadedBytes;
|
|
|
|
lastDownloadedBytes = downloadedBytes;
|
|
|
|
progressBar.tick(delta);
|
2018-11-09 02:34:18 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2018-11-09 02:34:18 +00:00
|
|
|
function toMegabytes(bytes) {
|
|
|
|
const mb = bytes / 1024 / 1024;
|
|
|
|
return `${Math.round(mb * 10) / 10} Mb`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findDownloadableRevision(rev, from, to) {
|
|
|
|
const log = debug('bisect:findrev');
|
|
|
|
const min = Math.min(from, to);
|
|
|
|
const max = Math.max(from, to);
|
|
|
|
log(`Looking around ${rev} from [${min}, ${max}]`);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (await browserFetcher.canDownload(rev)) {
|
|
|
|
return rev;
|
|
|
|
}
|
2018-11-09 02:34:18 +00:00
|
|
|
let down = rev;
|
|
|
|
let up = rev;
|
|
|
|
while (min <= down || up <= max) {
|
|
|
|
const [downOk, upOk] = await Promise.all([
|
|
|
|
down > min ? probe(--down) : Promise.resolve(false),
|
|
|
|
up < max ? probe(++up) : Promise.resolve(false),
|
|
|
|
]);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (downOk) {
|
|
|
|
return down;
|
|
|
|
}
|
|
|
|
if (upOk) {
|
|
|
|
return up;
|
|
|
|
}
|
2018-11-09 02:34:18 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
async function probe(rev) {
|
|
|
|
const result = await browserFetcher.canDownload(rev);
|
|
|
|
log(` ${rev} - ${result ? 'OK' : 'missing'}`);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function revisionToSha(revision) {
|
2020-05-07 10:54:55 +00:00
|
|
|
const json = await fetchJSON(
|
|
|
|
'https://cr-rev.appspot.com/_ah/api/crrev/v1/redirect/' + revision
|
|
|
|
);
|
2018-11-09 02:34:18 +00:00
|
|
|
return json.git_sha;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchJSON(url) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-05-07 10:54:55 +00:00
|
|
|
const agent = url.startsWith('https://')
|
|
|
|
? require('https')
|
|
|
|
: require('http');
|
2018-11-09 02:34:18 +00:00
|
|
|
const options = URL.parse(url);
|
|
|
|
options.method = 'GET';
|
|
|
|
options.headers = {
|
2020-05-07 10:54:55 +00:00
|
|
|
'Content-Type': 'application/json',
|
2018-11-09 02:34:18 +00:00
|
|
|
};
|
2020-05-07 10:54:55 +00:00
|
|
|
const req = agent.request(options, function (res) {
|
2018-11-09 02:34:18 +00:00
|
|
|
let result = '';
|
|
|
|
res.setEncoding('utf8');
|
2022-06-22 13:25:44 +00:00
|
|
|
res.on('data', chunk => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return (result += chunk);
|
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
return resolve(JSON.parse(result));
|
|
|
|
});
|
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
req.on('error', err => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return reject(err);
|
2018-11-09 02:34:18 +00:00
|
|
|
});
|
|
|
|
req.end();
|
|
|
|
});
|
|
|
|
}
|
2021-09-14 15:21:06 +00:00
|
|
|
|
|
|
|
function getChromiumRevision(gitRevision = null) {
|
|
|
|
const fileName = 'src/revisions.ts';
|
|
|
|
const command = gitRevision
|
|
|
|
? `git show ${gitRevision}:${fileName}`
|
|
|
|
: `cat ${fileName}`;
|
|
|
|
const result = execSync(command, {
|
|
|
|
encoding: 'utf8',
|
|
|
|
shell: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const m = result.match(/chromium: '(\d+)'/);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!m) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-14 15:21:06 +00:00
|
|
|
return parseInt(m[1], 10);
|
|
|
|
}
|