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

Fix/update benchmark #1109

Draft
wants to merge 9 commits into
base: gh-pages
Choose a base branch
from
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ How does HTMLMinifier compare to other solutions — [HTML Minifier from Will Pe

| Site | Original size *(KB)* | HTMLMinifier | minimize | Will Peavy | htmlcompressor.com |
| ---------------------------------------------------------------------------- |:--------------------:| ------------:| --------:| ----------:| ------------------:|
| [Google](https://www.google.com/) | 46 | **42** | 46 | 48 | 46 |
| [HTMLMinifier](https://github.com/kangax/html-minifier) | 125 | **98** | 111 | 117 | 111 |
| [Twitter](https://twitter.com/) | 207 | **165** | 200 | 224 | 200 |
| [Stack Overflow](https://stackoverflow.com/) | 253 | **195** | 207 | 215 | 204 |
| [Bootstrap CSS](https://getbootstrap.com/docs/3.3/css/) | 271 | **260** | 269 | 228 | 269 |
| [BBC](https://www.bbc.co.uk/) | 298 | **239** | 290 | 291 | 280 |
| [Amazon](https://www.amazon.co.uk/) | 422 | **316** | 412 | 425 | n/a |
| [NBC](https://www.nbc.com/) | 553 | **530** | 552 | 553 | 534 |
| [Wikipedia](https://en.wikipedia.org/wiki/President_of_the_United_States) | 565 | **461** | 548 | 569 | 548 |
| [New York Times](https://www.nytimes.com/) | 678 | **606** | 675 | 670 | n/a |
| [Google](https://www.google.com/) | 51 | **45** | 51 | 54 | 50 |
| [Stack Overflow](https://stackoverflow.com/) | 203 | **165** | 178 | 179 | 173 |
| [HTMLMinifier](https://github.com/kangax/html-minifier) | 237 | **149** | 215 | 230 | 216 |
| [Bootstrap CSS](https://getbootstrap.com/docs/3.4/css/) | 303 | **262** | 272 | 241 | n/a |
| [BBC](https://www.bbc.co.uk/) | 329 | **306** | 327 | 329 | n/a |
| [Amazon](https://www.amazon.co.uk/) | 419 | **377** | 409 | 424 | n/a |
| [Twitter](https://twitter.com/) | 482 | **400** | 474 | 531 | n/a |
| [Wikipedia](https://en.wikipedia.org/wiki/President_of_the_United_States) | 696 | **562** | 675 | 701 | n/a |
| [Eloquent Javascript](https://eloquentjavascript.net/1st_edition/print.html) | 870 | **815** | 840 | 864 | n/a |
| [ES6 table](https://kangax.github.io/compat-table/es6/) | 5911 | **5051** | 5595 | n/a | n/a |
| [ES draft](https://tc39.github.io/ecma262/) | 6126 | **5495** | 5664 | n/a | n/a |
| [New York Times](https://www.nytimes.com/) | 1258 | **1128** | 1250 | 1225 | n/a |
| [NBC](https://www.nbc.com/) | 1982 | **1822** | 1968 | 1988 | n/a |
| [ES draft](https://tc39.github.io/ecma262/) | 6041 | **5310** | 5502 | n/a | n/a |
| [ES6 table](https://kangax.github.io/compat-table/es6/) | 8125 | **6924** | 7676 | n/a | n/a |

## Options Quick Reference

Expand Down
71 changes: 26 additions & 45 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ else if (installed.status) {
process.exit(installed.status);
}

var brotli = require('brotli'),
chalk = require('chalk'),
var chalk = require('chalk'),
fork = require('child_process').fork,
fs = require('fs'),
https = require('https'),
lzma = require('lzma'),
Minimize = require('minimize'),
path = require('path'),
Progress = require('progress'),
Expand Down Expand Up @@ -121,9 +119,17 @@ function readSize(filePath, callback) {
}

function gzip(inPath, outPath, callback) {
fs.createReadStream(inPath).pipe(zlib.createGzip({
level: zlib.Z_BEST_COMPRESSION
})).pipe(fs.createWriteStream(outPath)).on('finish', callback);
fs.createReadStream(inPath)
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream(outPath))
.on('finish', callback);
}

function brotli(inPath, outPath, callback) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as the gzip function except for the method name. If someone wants to deduplicate this more please go ahead.

fs.createReadStream(inPath)
.pipe(zlib.createBrotliCompress())
.pipe(fs.createWriteStream(outPath))
.on('finish', callback);
}

function run(tasks, done) {
Expand Down Expand Up @@ -201,20 +207,19 @@ function displayTable() {

run(fileNames.map(function(fileName) {
var filePath = path.join('benchmarks/', fileName + '.html');
fs.mkdirSync('benchmarks/generated/', { recursive: true });

function processFile(site, done) {
var original = {
filePath: filePath,
gzFilePath: path.join('benchmarks/generated/', fileName + '.html.gz'),
lzFilePath: path.join('benchmarks/generated/', fileName + '.html.lz'),
brFilePath: path.join('benchmarks/generated/', fileName + '.html.br')
};
var infos = {};
['minifier', 'minimize', 'willpeavy', 'compressor'].forEach(function(name) {
infos[name] = {
filePath: path.join('benchmarks/generated/', fileName + '.' + name + '.html'),
gzFilePath: path.join('benchmarks/generated/', fileName + '.' + name + '.html.gz'),
lzFilePath: path.join('benchmarks/generated/', fileName + '.' + name + '.html.lz'),
brFilePath: path.join('benchmarks/generated/', fileName + '.' + name + '.html.br')
};
});
Expand All @@ -233,35 +238,14 @@ run(fileNames.map(function(fileName) {
});
});
},
// Apply LZMA on minified output
function(done) {
readBuffer(info.filePath, function(data) {
lzma.compress(data, 1, function(result, error) {
if (error) {
throw error;
}
writeBuffer(info.lzFilePath, new Buffer(result), function() {
info.lzTime = Date.now();
// Open and read the size of the minified+lzma output
readSize(info.lzFilePath, function(size) {
info.lzSize = size;
done();
});
});
});
});
},
// Apply Brotli on minified output
function(done) {
readBuffer(info.filePath, function(data) {
var output = new Buffer(brotli.compress(data, true).buffer);
writeBuffer(info.brFilePath, output, function() {
info.brTime = Date.now();
// Open and read the size of the minified+brotli output
readSize(info.brFilePath, function(size) {
info.brSize = size;
done();
});
brotli(info.filePath, info.brFilePath, function() {
info.brTime = Date.now();
// Open and read the size of the minified+brotli output
readSize(info.brFilePath, function(size) {
info.brSize = size;
done();
});
});
},
Expand Down Expand Up @@ -297,7 +281,7 @@ run(fileNames.map(function(fileName) {

function testWillPeavy(done) {
readText(filePath, function(data) {
var options = url.parse('https://www.willpeavy.com/minifier/');
var options = url.parse('https://www.willpeavy.com/tools/minifier/');
options.method = 'POST';
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
Expand All @@ -322,7 +306,6 @@ run(fileNames.map(function(fileName) {
else {
info.size = 0;
info.gzSize = 0;
info.lzSize = 0;
info.brSize = 0;
done();
}
Expand All @@ -335,7 +318,7 @@ run(fileNames.map(function(fileName) {

function testHTMLCompressor(done) {
readText(filePath, function(data) {
var options = url.parse('https://htmlcompressor.com/compress_ajax_v2.php');
var options = url.parse('https://htmlcompressor.com/compress');
options.method = 'POST';
options.headers = {
'Accept-Encoding': 'gzip',
Expand All @@ -348,7 +331,6 @@ run(fileNames.map(function(fileName) {
if (info) {
info.size = 0;
info.gzSize = 0;
info.lzSize = 0;
info.brSize = 0;
info = null;
done();
Expand Down Expand Up @@ -382,6 +364,7 @@ run(fileNames.map(function(fileName) {
});
}).on('error', failed).end(querystring.stringify({
code_type: 'html',
output_format: 'json',
html_level: 3,
html_strip_quotes: 1,
minimize_style: 1,
Expand All @@ -407,30 +390,28 @@ run(fileNames.map(function(fileName) {
testHTMLCompressor
], function() {
var display = [
[fileName, '+ gzip', '+ lzma', '+ brotli'].join('\n'),
[redSize(original.size), redSize(original.gzSize), redSize(original.lzSize), redSize(original.brSize)].join('\n')
[fileName, '+ gzip', '+ brotli'].join('\n'),
[redSize(original.size), redSize(original.gzSize), redSize(original.brSize)].join('\n')
];
var report = [
'[' + fileName + '](' + urls[fileName] + ')',
toKb(original.size)
];
for (var name in infos) {
var info = infos[name];
display.push([greenSize(info.size), greenSize(info.gzSize), greenSize(info.lzSize), greenSize(info.brSize)].join('\n'));
display.push([greenSize(info.size), greenSize(info.gzSize), greenSize(info.brSize)].join('\n'));
report.push(info.size ? toKb(info.size) : 'n/a');
}
display.push(
[
blueSavings(original.size, infos.minifier.size),
blueSavings(original.gzSize, infos.minifier.gzSize),
blueSavings(original.lzSize, infos.minifier.lzSize),
blueSavings(original.brSize, infos.minifier.brSize)
].join('\n'),
[
blueTime(infos.minifier.endTime - infos.minifier.startTime),
blueTime(original.gzTime - original.endTime),
blueTime(original.lzTime - original.gzTime),
blueTime(original.brTime - original.lzTime)
blueTime(original.brTime - original.gzTime)
].join('\n')
);
rows[fileName] = {
Expand Down
Empty file removed benchmarks/generated/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion benchmarks/index.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Amazon": "https://www.amazon.co.uk/",
"BBC": "https://www.bbc.co.uk/",
"Bootstrap CSS": "https://getbootstrap.com/docs/3.3/css/",
"Bootstrap CSS": "https://getbootstrap.com/docs/3.4/css/",
"Eloquent Javascript": "https://eloquentjavascript.net/1st_edition/print.html",
"ES draft": "https://tc39.github.io/ecma262/",
"ES6 table": "https://kangax.github.io/compat-table/es6/",
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@
"qunit": "^2.17.2"
},
"benchmarkDependencies": {
"brotli": "^1.3.2",
"chalk": "^4.1.2",
"cli-table": "^0.3.6",
"lzma": "^2.3.2",
"minimize": "^2.2.0",
"progress": "^2.0.3"
},
Expand Down