5 var child_process = require('child_process'),
8 path = require('path'),
9 Progress = require('progress');
11 var urls = require('./benchmarks');
12 var fileNames = Object.keys(urls);
15 var args = [].concat.apply([], [].slice.call(arguments, 0, -1));
16 var callback = arguments[arguments.length - 1];
17 var task = child_process.spawn('git', args, { stdio: ['ignore', 'pipe', 'ignore'] });
19 task.stdout.setEncoding('utf8');
20 task.stdout.on('data', function(data) {
23 task.on('exit', function(code) {
24 callback(code, output);
28 function readText(filePath, callback) {
29 fs.readFile(filePath, { encoding: 'utf8' }, callback);
32 function writeText(filePath, data) {
33 fs.writeFile(filePath, data, { encoding: 'utf8' }, function(err) {
40 function loadModule() {
41 require('./src/htmlparser');
42 return require('./src/htmlminifier').minify || global.minify;
45 function getOptions(fileName, options) {
51 for (var key in options) {
52 result[key] = options[key];
57 function minify(hash, options) {
58 var minify = loadModule();
59 process.send('ready');
60 var count = fileNames.length;
61 fileNames.forEach(function(fileName) {
62 readText(path.join('benchmarks/', fileName + '.html'), function(err, data) {
68 var minified = minify(data, getOptions(fileName, options));
70 process.send({ name: fileName, size: minified.length });
73 throw new Error('unexpected result: ' + minified);
77 console.error('[' + fileName + ']', e.stack || e);
89 function print(table) {
92 var row = fileNames.slice(0);
93 row.unshift('hash', 'date');
94 output.push(row.join(','));
95 for (var hash in table) {
96 var data = table[hash];
97 row = [hash, '"' + data.date + '"'];
98 fileNames.forEach(function(fileName) {
99 row.push(data[fileName]);
101 output.push(row.join(','));
103 errors.push(hash + ' - ' + data.error);
106 writeText('backtest.csv', output.join('\n'));
107 writeText('backtest.log', errors.join('\n'));
110 if (process.argv.length > 2) {
111 var count = +process.argv[2];
113 git('log', '--date=iso', '--pretty=format:%h %cd', '-' + count, function(code, data) {
115 var commits = data.split(/\s*?\n/).map(function(line) {
116 var index = line.indexOf(' ');
117 var hash = line.substr(0, index);
119 date: line.substr(index + 1).replace('+', '').replace(/ 0000$/, '')
123 var nThreads = os.cpus().length;
125 var progress = new Progress('[:bar] :etas', {
127 total: commits.length * 2
131 if (commits.length && running < nThreads) {
132 var hash = commits.shift();
133 var task = child_process.fork('./backtest', { silent: true });
135 var id = setTimeout(function() {
136 if (task.connected) {
137 error += 'task timed out\n';
141 task.on('message', function(data) {
142 if (data === 'ready') {
147 table[hash][data.name] = data.size;
149 }).on('exit', function() {
153 table[hash].error = error;
155 if (!--running && !commits.length) {
162 task.stderr.setEncoding('utf8');
163 task.stderr.on('data', function(data) {
166 task.stdout.resume();
176 console.error('Invalid input:', process.argv[2]);
180 process.on('message', function(hash) {
181 var paths = ['src', 'benchmark.conf', 'sample-cli-config-file.conf'];
182 git('reset', 'HEAD', '--', paths, function() {
183 var conf = 'sample-cli-config-file.conf';
185 function checkout() {
186 var path = paths.shift();
187 git('checkout', hash, '--', path, function(code) {
188 if (code === 0 && path === 'benchmark.conf') {
195 readText(conf, function(err, data) {
200 minify(hash, JSON.parse(data));