Skip to content

Commit

Permalink
Execute each images comparison in a separate child processes (#561) (#…
Browse files Browse the repository at this point in the history
…571)

* Execute each images comparison in a separate child processes (#561)

* Do not use the logger in the comparison child processes (#561)
  • Loading branch information
vdekov authored and garris committed Oct 20, 2017
1 parent 3b9c17c commit 2a4a4b5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
29 changes: 26 additions & 3 deletions core/util/compare/compare.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
var compareHashes = require('./compare-hash');
var compareResemble = require('./compare-resemble');
var storeFailedDiff = require('./store-failed-diff.js');

module.exports = function (referencePath, testPath, misMatchThreshold, resembleOutputSettings, requireSameDimensions) {
return compareHashes(referencePath, testPath)
.catch(() => compareResemble(referencePath, testPath, misMatchThreshold, resembleOutputSettings, requireSameDimensions));
process.on('message', compare);

function compare (data) {
var {referencePath, testPath, resembleOutputSettings, pair} = data;
var promise = compareHashes(referencePath, testPath)
.catch(() => compareResemble(referencePath, testPath, pair.misMatchThreshold, resembleOutputSettings, pair.requireSameDimensions));
promise
.then(function (data) {
pair.diff = data;
pair.status = 'pass';
return sendMessage(pair);
})
.catch(function (data) {
pair.diff = data;
pair.status = 'fail';

return storeFailedDiff(testPath, data).then(function (compare) {
pair.diffImage = compare;
return sendMessage(pair);
});
});
};

function sendMessage (data) {
process.send(data);
}
44 changes: 23 additions & 21 deletions core/util/compare/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
var path = require('path');
var map = require('p-map');
var fs = require('fs');
var cp = require('child_process');

var compare = require('./compare');
var Reporter = require('./../Reporter');
var logger = require('./../logger')('compare');
var storeFailedDiff = require('./store-failed-diff.js');
var storeFailedDiffStub = require('./store-failed-diff-stub.js');

var ASYNC_COMPARE_LIMIT = 20;
Expand Down Expand Up @@ -48,27 +47,30 @@ function comparePair (pair, report, config) {
}

function compareImages (referencePath, testPath, pair, resembleOutputSettings, Test) {
return compare(referencePath, testPath, pair.misMatchThreshold, resembleOutputSettings, pair.requireSameDimensions)
.then(function (data) {
pair.diff = data;
Test.status = 'pass';
logger.success('OK: ' + pair.label + ' ' + pair.fileName);
data = null;
pair.diff.getDiffImage = null;
return pair;
})
.catch(function (data) {
pair.diff = data;
Test.status = 'fail';
logger.error('ERROR { requireSameDimensions: ' + (data.requireSameDimensions ? 'true' : 'false') + ' size: ' + (data.isSameDimensions ? 'ok' : 'isDifferent') + ', content: ' + data.misMatchPercentage + '%, threshold: ' + pair.misMatchThreshold + '% }: ' + pair.label + ' ' + pair.fileName);
return new Promise(function (resolve, reject) {
var worker = cp.fork(require.resolve('./compare'));
worker.send({
referencePath : referencePath,
testPath : testPath,
resembleOutputSettings : resembleOutputSettings,
pair : pair
});

worker.on('message', function (data) {
worker.kill();
Test.status = data.status;
pair.diff = data.diff;

if (data.status == 'fail') {
pair.diffImage = data.diffImage;
logger.error('ERROR { requireSameDimensions: ' + (data.requireSameDimensions ? 'true' : 'false') + ', size: ' + (data.isSameDimensions ? 'ok' : 'isDifferent') + ', content: ' + data.misMatchPercentage + '%, threshold: ' + pair.misMatchThreshold + '% }: ' + pair.label + ' ' + pair.fileName);
} else {
logger.success('OK: ' + pair.label + ' ' + pair.fileName);
}

return storeFailedDiff(testPath, data).then(function (compare) {
pair.diffImage = compare;
data = null;
pair.diff.getDiffImage = null;
return pair;
});
resolve(data);
});
});
}

module.exports = function (config) {
Expand Down

0 comments on commit 2a4a4b5

Please sign in to comment.