Skip to content

Instantly share code, notes, and snippets.

@acdha
Last active May 14, 2019 10:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save acdha/5673011 to your computer and use it in GitHub Desktop.
Save acdha/5673011 to your computer and use it in GitHub Desktop.
Simple PhantomJS script to warm multiple pages as fast as possible
/*
* Warm a WDL web server by requesting our top pages using a full web browser
*/
var system = require('system');
var page = new WebPage(),
hostnames = [],
items = [],
customHeaders = {};
system.args.slice(1).forEach(function (arg, i) {
arg = arg.trim();
if (arg.indexOf("--header=") === 0) {
var header_arg = arg.slice(9),
parts = header_arg.split(":").map(function (i) { return i.trim(); });
customHeaders[parts[0]] = parts[1];
} else if (!arg.match(/^[0-9]+$/)) {
hostnames.push(arg);
} else {
items.push(arg);
}
});
if (hostnames.length < 1 || items.length < 1) {
console.log('Usage:', system.args[0], '[--header=key:value] hostname [hostname2…] id_1 [id_2 …]');
phantom.exit();
}
if (customHeaders) {
console.log("Setting custom headers:", JSON.stringify(customHeaders, null, 4));
page.customHeaders = customHeaders;
}
console.log("Loading item pages from", hostnames.join(" "), "for items:", items.join(" "));
var urls = [],
languages = ['ar', 'en', 'es', 'fr', 'pt', 'ru', 'zh'],
pages = ['/', '/zoom/'];
items.forEach(function (item) {
languages.forEach(function (lang) {
hostnames.forEach(function (hostname) {
pages.forEach(function (page) {
urls.push("http://" + hostname + '/' + lang + "/item/" + item + page);
});
});
});
});
function nextPage() {
if (urls.length < 1) {
phantom.exit();
}
var loadStart = new Date(),
url = urls.shift();
page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to load ' + url + ': ' + status);
} else {
console.log('Loaded', url, 'in', ((new Date() - loadStart) / 1000).toFixed(1), 'seconds');
}
nextPage();
});
}
nextPage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment