Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sharp to convert screenshots #468

Merged
merged 7 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ COPY docker/webpagereplay/wpr_key.pem /webpagereplay/certs/
COPY docker/webpagereplay/deterministic.js /webpagereplay/scripts/deterministic.js
COPY docker/webpagereplay/LICENSE /webpagereplay/

# build-essential is needed for Sharp to compile
RUN sudo apt-get update && sudo apt-get install libnss3-tools \
build-essential \
iproute2 -y && \
mkdir -p $HOME/.pki/nssdb && \
certutil -d $HOME/.pki/nssdb -N
Expand Down
9 changes: 3 additions & 6 deletions lib/core/engine/collector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const forEach = require('lodash.foreach');
const chromeCPU = require('../../support/chromeCPU');
const ScreenshotManager = require('../../screenshot/');

/**
* Create a new Collector instance. The collector will collect metrics
Expand All @@ -12,6 +13,7 @@ class Collector {
this.options = options;
this.statistics = statistics;
this.storageManager = storageManager;
this.screenshotManager = new ScreenshotManager(storageManager, options);
this.collectChromeTimeline =
options.chrome &&
options.chrome.collectTracingEvents &&
Expand Down Expand Up @@ -57,12 +59,7 @@ class Collector {
// and array and the promise them all later on
const extraWork = [];
if (this.options.screenshot) {
extraWork.push(
this.storageManager.writeData(
`screenshot-${index}.png`,
data.screenshot
)
);
extraWork.push(this.screenshotManager.save(`${index}`, data.screenshot));
}

// Collect CPU data. There are some extra work going on here now
Expand Down
12 changes: 12 additions & 0 deletions lib/screenshot/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
type: 'jpg',
png: {
compressionLevel: 6
},
jpg: {
quality: 80
},
maxSize: 2000
};
46 changes: 46 additions & 0 deletions lib/screenshot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const sharp = require('sharp');
const merge = require('lodash.merge');
const defaultConfig = require('./defaults');

const SCREENSHOT_DIR = 'screenshot';

function savePng(name, data, storageManager, config) {
return sharp(data)
.png({ compressionLevel: config.png.compressionLevel })
.resize(config.maxSize, config.maxSize)
.max()
.toBuffer()
.then(newBuff =>
storageManager.writeData(`${name}.png`, newBuff, SCREENSHOT_DIR)
);
}

function saveJpg(name, data, storageManager, config) {
return sharp(data)
.jpeg({ quality: config.jpg.quality })
.resize(config.maxSize, config.maxSize)
.max()
.toBuffer()
.then(newBuff =>
storageManager.writeData(`${name}.jpg`, newBuff, SCREENSHOT_DIR)
);
}

class ScreenshotManager {
constructor(storageManager, options) {
this.storageManager = storageManager;
this.config = merge({}, defaultConfig, options.screenshotParams);
}

save(name, data) {
if (this.config.type === 'png') {
return savePng(name, data, this.storageManager, this.config);
} else {
return saveJpg(name, data, this.storageManager, this.config);
}
}
}

module.exports = ScreenshotManager;
27 changes: 26 additions & 1 deletion lib/support/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const urlValidator = require('valid-url');
const util = require('util');
const hasbin = require('hasbin');
const videoDefaults = require('../video/defaults');
const screenshotDefaults = require('../screenshot/defaults');

function validateInput(argv) {
let url = argv._[0];
Expand Down Expand Up @@ -290,9 +291,33 @@ module.exports.parseCommandLine = function parseCommandLine() {
choices: ['chrome', 'firefox'],
describe: 'Specify browser'
})
/** Screenshot */
.option('screenshot', {
type: 'boolean',
describe: 'Save one screen shot per iteration.'
default: false,
describe: 'Save one screen shot per iteration.',
group: 'Screenshot'
})
.option('screenshotParams.type', {
describe: 'Set the file type of the screenshot',
choices: ['png', 'jpg'],
default: screenshotDefaults.type,
group: 'Screenshot'
})
.option('screenshotParams.png.compressionLevel', {
describe: 'zlib compression level',
default: screenshotDefaults.png.compressionLevel,
group: 'Screenshot'
})
.option('screenshotParams.jpg.quality', {
describe: 'Quality of the JPEG screenshot. 1-100',
default: screenshotDefaults.jpg.quality,
group: 'Screenshot'
})
.option('screenshotParams.maxSize', {
describe: 'The max size of the screenshot (width and height).',
default: screenshotDefaults.maxSize,
group: 'Screenshot'
})
.option('pageCompleteCheck', {
describe:
Expand Down
4 changes: 2 additions & 2 deletions lib/support/storageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class StorageManager {
return mkdirp(dir).then(() => dir);
}

writeData(filename, data) {
writeData(filename, data, subdir) {
return Promise.join(
this.createDataDir(),
subdir ? this.createSubDataDir(subdir) : this.createDataDir(),
filename,
data,
(dirPath, filename, data) =>
Expand Down
Loading