forked from kangax/html-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
110 lines (89 loc) · 3.21 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* jshint strict:false */
var fs = require('fs'),
exec = require('child_process').exec,
Table = require('cli-table');
var fileNames = [
// 'es6-draft',
// 'eloquentjavascript',
'wikipedia',
'stackoverflow',
'amazon',
'es6-table',
'msn',
'google',
'newyorktimes',
'abc',
'html-minifier'
];
var table = new Table({
head: ['File', 'Before', 'After', 'Savings', 'Time'],
colWidths: [20, 25, 25, 20, 20]
});
console.log('');
function redSize(size) {
return '\033[91m' + size + '\033[0m (' + toKb(size) + 'KB)';
}
function greenSize(size) {
return '\033[92m' + size + '\033[0m (' + toKb(size) + 'KB)';
}
function toKb(size) {
return (size / 1024).toFixed(2);
}
function blueSavings(oldSize, newSize) {
var savingsPercent = (1 - newSize / oldSize) * 100;
var savings = (oldSize - newSize) / 1024;
return '\033[96m' + savingsPercent.toFixed(2) + '\033[0m% (' + savings.toFixed(2) + 'KB)';
}
function blueTime(time) {
return '\033[96m' + time + '\033[0mms';
}
function test(fileName, done) {
if (!fileName) {
console.log(table.toString());
return;
}
console.log('Processing...', fileName);
var filePath = 'benchmarks/' + fileName + '.html';
var minifiedFilePath = 'benchmarks/generated/' + fileName + '.min.html';
var gzFilePath = 'benchmarks/generated/' + fileName + '.html.gz';
var gzMinifiedFilePath = 'benchmarks/generated/' + fileName + '.min.html.gz';
var command = './cli.js ' + filePath + ' -c benchmark.conf' + ' -o ' + minifiedFilePath;
// Open and read the size of the original input
fs.stat(filePath, function (err, stats) {
var originalSize = stats.size;
exec('gzip --keep --force --best --stdout ' + filePath + ' > ' + gzFilePath, function () {
// Open and read the size of the gzipped original
fs.stat(gzFilePath, function (err, stats) {
var gzOriginalSize = stats.size;
// Begin timing after gzipped fixtures have been created
var startTime = new Date();
exec(command, function () {
// Open and read the size of the minified output
fs.stat(minifiedFilePath, function (err, stats) {
var minifiedSize = stats.size;
var minifiedTime = new Date() - startTime;
// Gzip the minified output
exec('gzip --keep --force --best --stdout ' + minifiedFilePath + ' > ' + gzMinifiedFilePath, function () {
// Open and read the size of the minified+gzipped output
fs.stat(gzMinifiedFilePath, function (err, stats) {
var gzMinifiedSize = stats.size;
var gzMinifiedTime = new Date() - startTime;
table.push([
[fileName, '+ gzipped'].join('\n'),
[redSize(originalSize), redSize(gzOriginalSize)].join('\n'),
[greenSize(minifiedSize), greenSize(gzMinifiedSize)].join('\n'),
[blueSavings(originalSize, minifiedSize), blueSavings(gzOriginalSize, gzMinifiedSize)].join('\n'),
[blueTime(minifiedTime), blueTime(gzMinifiedTime)].join('\n')
]);
done();
});
});
});
});
});
});
});
}
(function run() {
test(fileNames.pop(), run);
})();