Skip to content

Commit

Permalink
perf: run HTML checks concurrently
Browse files Browse the repository at this point in the history
Runs all Content Document checks concurrently, earch within a new
page of a shared Chrome browser instance.

The results are aggregated in document order.

The concurrency level is currently hard-coded to 4, but it should
be made configurable when implementing issue #77.

Based on very naïve –and probably not very significant– benchmarking,
the execution time is reduced by a factor of at least 2 on large
publications.
  • Loading branch information
rdeltour committed Oct 27, 2017
1 parent e28a0a6 commit b86d485
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"marko": "^4.4.26",
"meow": "^3.7.0",
"multer": "^1.3.0",
"p-map": "^1.2.0",
"path": "^0.12.7",
"puppeteer": "^0.12.0",
"shortid": "^2.2.8",
Expand Down
19 changes: 7 additions & 12 deletions src/checker/checker-chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const fs = require('fs');
const path = require('path');
const pMap = require('p-map');
const puppeteer = require('puppeteer');
const axe2ace = require('../report/axe2ace.js');
const winston = require('winston');
Expand Down Expand Up @@ -43,7 +44,7 @@ if (!fs.existsSync(PATH_TO_ACE_EXTRACTION)) {
}

async function checkSingle(spineItem, epub, browser) {
winston.info(`- ${spineItem.relpath}`);
winston.verbose(`- Processing ${spineItem.relpath}`);
try {
const page = await browser.newPage();
await page.goto(spineItem.url);
Expand Down Expand Up @@ -92,10 +93,10 @@ async function checkSingle(spineItem, epub, browser) {
// Post-process results
results.assertions = (results.axe != null) ? axe2ace.axe2ace(spineItem, results.axe) : [];
delete results.axe;
winston.info(`- ${
(results.assertions == null)
? 'No'
: results.assertions.assertions.length} issues found`);
winston.info(`- ${spineItem.relpath}: ${
(results.assertions && results.assertions.lentgh > 0)
? results.assertions.assertions.length
: 'No'} issues found`);
// Resolve path and locators for extracted data
if (results.data != null) {
Object.getOwnPropertyNames(results.data).forEach((key) => {
Expand Down Expand Up @@ -133,13 +134,7 @@ async function checkSingle(spineItem, epub, browser) {
module.exports.check = async (epub) => {
const browser = await puppeteer.launch();
winston.info('Checking documents...');
return epub.contentDocs.reduce((sequence, spineItem) =>
sequence.then(results =>
checkSingle(spineItem, epub, browser)
.then((result) => {
results.push(result);
return results;
})), Promise.resolve([]))
return pMap(epub.contentDocs, doc => checkSingle(doc, epub, browser), { concurrency: 4 })
.then(async (results) => {
await browser.close();
return results;
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3846,6 +3846,10 @@ p-locate@^2.0.0:
dependencies:
p-limit "^1.1.0"

p-map@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/p-map/-/p-map-1.2.0.tgz#e4e94f311eabbc8633a1e79908165fca26241b6b"

package-json@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/package-json/-/package-json-1.2.0.tgz#c8ecac094227cdf76a316874ed05e27cc939a0e0"
Expand Down

0 comments on commit b86d485

Please sign in to comment.