Remote Browser's remoteDebuggingPort option

This patch remove remoteDebuggingPort option. Instead, browser
is launched with '--remote-debugging-port=0' flag, letting browser
to pick any port. The puppeteer reads the port number from the
browser's stderr stream.

This change cuts average browser start time from 300ms to 250ms
on my machine. This happens since puppeteer doesn't have to probe
network once every 100ms, waiting for the remote debugging server to
instantiate.

Fixes #21.
This commit is contained in:
Andrey Lushnikov 2017-07-11 08:00:21 -07:00
parent d120e7e426
commit 279cd4c9fb
4 changed files with 27 additions and 36 deletions

View File

@ -88,7 +88,6 @@ not necessarily result in launching browser; the instance will be launched when
- `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields:
- `headless` <[boolean]> Wether to run chromium in headless mode. Defaults to `true`.
- `remoteDebuggingPort` <[number]> Specify a remote debugging port to open on chromium instance. Defaults to `9229`.
- `executablePath` <[string]> Path to a chromium executable to run instead of bundled chromium.
- `args` <[Array]<[string]>> Additional arguments to pass to the chromium instance. List of chromium flags could be found [here](http://peter.sh/experiments/chromium-command-line-switches/).

View File

@ -17,18 +17,13 @@
var Browser = require('../lib/Browser');
var Downloader = require('../utils/ChromiumDownloader');
var revision = "464642";
var revision = '483012';
console.log('Downloading custom chromium revision - ' + revision);
Downloader.downloadRevision(Downloader.currentPlatform(), revision).then(async () => {
console.log('Done.');
var executablePath = Downloader.revisionInfo(Downloader.currentPlatform(), revision).executablePath;
var browser1 = new Browser({
remoteDebuggingPort: 9228,
executablePath,
});
var browser2 = new Browser({
remoteDebuggingPort: 9229,
});
var browser1 = new Browser({ executablePath });
var browser2 = new Browser();
var [version1, version2] = await Promise.all([
browser1.version(),
browser2.version()

View File

@ -15,13 +15,13 @@
*/
let {Duplex} = require('stream');
let http = require('http');
let path = require('path');
let removeRecursive = require('rimraf').sync;
let Page = require('./Page');
let childProcess = require('child_process');
let Downloader = require('../utils/ChromiumDownloader');
let Connection = require('./Connection');
let readline = require('readline');
let CHROME_PROFILE_PATH = path.resolve(__dirname, '..', '.dev_profile');
let browserId = 0;
@ -29,6 +29,7 @@ let browserId = 0;
let DEFAULT_ARGS = [
'--disable-background-timer-throttling',
'--no-first-run',
'--remote-debugging-port=0',
];
class Browser {
@ -39,12 +40,9 @@ class Browser {
options = options || {};
++browserId;
this._userDataDir = CHROME_PROFILE_PATH + browserId;
this._remoteDebuggingPort = 9227;
if (typeof options.remoteDebuggingPort === 'number')
this._remoteDebuggingPort = options.remoteDebuggingPort;
this._remoteDebuggingPort = 0;
this._chromeArguments = DEFAULT_ARGS.concat([
`--user-data-dir=${this._userDataDir}`,
`--remote-debugging-port=${this._remoteDebuggingPort}`,
]);
if (typeof options.headless !== 'boolean' || options.headless) {
this._chromeArguments.push(...[
@ -122,7 +120,13 @@ class Browser {
this._chromeProcess.stderr.pipe(this.stderr);
this._chromeProcess.stdout.pipe(this.stdout);
await waitForChromeResponsive(this._remoteDebuggingPort, () => !this._terminated);
this._remoteDebuggingPort = await waitForRemoteDebuggingPort(this._chromeProcess);
// Failed to connect to browser.
if (this._remoteDebuggingPort === -1) {
this._chromeProcess.kill();
throw new Error('Failed to connect to chrome!');
}
if (this._terminated)
throw new Error('Failed to launch chrome! ' + stderr);
}
@ -136,30 +140,24 @@ class Browser {
module.exports = Browser;
function waitForChromeResponsive(remoteDebuggingPort, shouldWaitCallback) {
function waitForRemoteDebuggingPort(chromeProcess) {
const rl = readline.createInterface({ input: chromeProcess.stderr });
let fulfill;
let promise = new Promise(x => fulfill = x);
let options = {
method: 'GET',
host: 'localhost',
port: remoteDebuggingPort,
path: '/json/list'
};
let probeTimeout = 100;
sendRequest();
rl.on('line', onLine);
rl.once('close', () => fulfill(-1));
return promise;
function sendRequest() {
let req = http.request(options, res => {
fulfill();
});
req.on('error', e => {
if (shouldWaitCallback())
setTimeout(sendRequest, probeTimeout);
else
fulfill();
});
req.end();
/**
* @param {string} line
*/
function onLine(line) {
const match = line.match(/^DevTools listening on .*:([\d]+)$/);
if (!match)
return;
fulfill(Number.parseInt(match[1], 10));
rl.removeListener('line', onLine);
rl.close();
}
}

View File

@ -57,7 +57,6 @@ if (!fs.existsSync(scriptPath)) {
}
let browser = new Browser({
remoteDebuggingPort: 9229,
headless: argv.headless,
args: ['--no-sandbox']
});