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

@@ -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();
}
}