Skip to content

Commit

Permalink
Remote Browser's remoteDebuggingPort option
Browse files Browse the repository at this point in the history
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 puppeteer#21.
  • Loading branch information
aslushnikov committed Jul 11, 2017
1 parent d120e7e commit 279cd4c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
1 change: 0 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).

Expand Down
11 changes: 3 additions & 8 deletions examples/custom-chromium-revision.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
50 changes: 24 additions & 26 deletions lib/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
*/

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;

let DEFAULT_ARGS = [
'--disable-background-timer-throttling',
'--no-first-run',
'--remote-debugging-port=0',
];

class Browser {
Expand All @@ -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(...[
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
}

Expand Down
1 change: 0 additions & 1 deletion phantom_shim/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ if (!fs.existsSync(scriptPath)) {
}

let browser = new Browser({
remoteDebuggingPort: 9229,
headless: argv.headless,
args: ['--no-sandbox']
});
Expand Down

0 comments on commit 279cd4c

Please sign in to comment.