From 870eb7967f51383a97b0a040338ec068eea250f6 Mon Sep 17 00:00:00 2001 From: Evgeniy Gavryushin Date: Wed, 3 Sep 2014 18:11:58 +0400 Subject: [PATCH] Use COA; add 'output' option --- bin/compare-minimizers | 2 +- lib/cli.js | 27 +++++++ lib/compare.js | 175 +++++++++++++++++++++++++++++++++++++++++ lib/index.js | 153 ----------------------------------- package.json | 15 ++-- 5 files changed, 211 insertions(+), 161 deletions(-) create mode 100644 lib/cli.js create mode 100644 lib/compare.js delete mode 100644 lib/index.js diff --git a/bin/compare-minimizers b/bin/compare-minimizers index 56f4ff1..c0395aa 100755 --- a/bin/compare-minimizers +++ b/bin/compare-minimizers @@ -1,3 +1,3 @@ #!/usr/bin/env node -require('../lib/index.js') +require('../lib/cli.js') diff --git a/lib/cli.js b/lib/cli.js new file mode 100644 index 0000000..555c078 --- /dev/null +++ b/lib/cli.js @@ -0,0 +1,27 @@ +var path = require('path'), + bench = require('./compare'); + +module.exports = require('coa').Cmd() + .name(process.argv[1]) + .helpful() + .title('Сompares the work of CSS minimizers') + .opt() + .name('version') + .title('Shows the version number') + .short('v').long('version') + .flag() + .only() + .act(function () { + var p = require('../package.json'); + return p.name + ' ' + p.version; + }) + .end() + .opt() + .name('output') + .title('Path to the result output file') + .short('o').long('output') + .end() + .act(function (opts) { + bench.compare(opts); + }) + .run(process.argv.slice(2)); diff --git a/lib/compare.js b/lib/compare.js new file mode 100644 index 0000000..1bae75b --- /dev/null +++ b/lib/compare.js @@ -0,0 +1,175 @@ +var fs = require('fs'), + path = require('path'), + shell = require('shelljs'), + bench = require('../.cmb/config'), + utils = require('./utils'), + benchLogger = require('./logger'), + minimizers = bench.minimizers, + archivers = bench.archivers, + toRawCSS = bench.paths.toRawCSS, + toMinCSS = bench.paths.toMinCSS, + toArchCSS = bench.paths.toArchCSS; + +require('colors'); + +exports.compare = function (options) { + var outputToFile = options.output; + + shell.exec('ulimit -n 8192'); + + // constants + var MAX_TIME = Number.MAX_VALUE; + + var listOfMinimizers = Object.keys(minimizers) || [], + listOfArchivers = Object.keys(archivers) || [], + listOfMinLen = listOfMinimizers.length, + listOfArchLen = listOfArchivers.length; + + // makes dirs + listOfMinimizers.forEach(function (minimizer) { + utils.mkdir(path.join(toMinCSS, minimizer)); + listOfArchivers.forEach(function (archiver) { + utils.mkdir(path.join(toArchCSS, archiver, 'raw')); + utils.mkdir(path.join(toArchCSS, archiver, minimizer)); + }) + }); + + // Ok! Let's GO! + var output = '\n' + (outputToFile ? 'Result:' : 'Result:'.bold.green) + '\n', + cssFiles = fs.readdirSync(toRawCSS); + + cssFiles.forEach(function (cssFile) { + var pathToRawFile = path.join(toRawCSS, cssFile), + rawFileSize = fs.statSync(pathToRawFile).size; + + // minimizes and archives the file by tools which are specified in 'config.js' + var res = {}; + + if (listOfMinLen) { + res.min = {}; + } else { + benchLogger('e', '[e] Spicify minimizers in \'.cmb/config.js\'', '') + process.exit(1); + } + listOfArchLen && (res.arch = {}); + + listOfMinimizers.forEach(function (minimizer) { + var pathToMinCSS = path.join(toMinCSS, minimizer, cssFile), + minimizeMethod = minimizers[minimizer]; + + res.min[minimizer] = minimizeMethod(minimizer, pathToRawFile, pathToMinCSS); + + listOfArchivers.forEach(function (archiver) { + var pathToArchCSS = path.join(toArchCSS, archiver), + pathToRawArchCSS = path.join(pathToArchCSS, 'raw'), + pathToMinArchCSS = path.join(pathToArchCSS, minimizer), + archiveScript = archivers[archiver]; + + if (!res.arch.hasOwnProperty(archiver)) { + res.arch[archiver] = {}; + } + if (!res.arch.hasOwnProperty('raw')) { + res.arch[archiver].raw = archiveScript(cssFile, pathToRawFile, pathToRawArchCSS); + } + res.arch[archiver][minimizer] = archiveScript(cssFile, pathToMinCSS, pathToMinArchCSS); + }); + }); + + // gets the best and the fastest results + var minSize = rawFileSize, + maxSize = rawFileSize, + minTime = MAX_TIME; + + listOfMinimizers.forEach(function (minimizer) { + var currMinSize = res.min[minimizer].size, + currMinTime = res.min[minimizer].time; + + minSize = Math.min(minSize, currMinSize); + maxSize = Math.max(maxSize, currMinSize); + minTime = Math.min(minTime, currMinTime); + }); + + var minArchSize = {}, + maxArchSize = {}; + + listOfArchivers.forEach(function (archiver) { + minArchSize[archiver] = res.arch[archiver].raw; + maxArchSize[archiver] = res.arch[archiver].raw; + listOfMinimizers.forEach(function (minimizer) { + var currArchSize = res.arch[archiver][minimizer]; + + minArchSize[archiver] = Math.min(minArchSize[archiver], currArchSize); + maxArchSize[archiver] = Math.max(maxArchSize[archiver], currArchSize); + }); + }); + + // logs the test file + var _output = + (outputToFile ? '\n---> ' : '\n---> '.bold.green) + + cssFile + utils.addSymbols(40, cssFile.length, ' ') + ' --> ' + + (outputToFile ? rawFileSize : (rawFileSize + '').bold) + + ' b' + utils.addSymbols(listOfArchLen ? 22 - (rawFileSize + '').length : 2, 0, ' '), + + _outputLen = + ('\n---> ' + cssFile + utils.addSymbols(40, cssFile.length, ' ') + + ' --> ' + rawFileSize + ' b' + utils.addSymbols(listOfArchLen ? 22 - (rawFileSize + '').length : 2, 0, ' ')).length; + + listOfArchivers.forEach(function (archiver) { + var currArchSize = res.arch[archiver].raw; + + _output += + ' + ' + archiver + ' > ' + + (!outputToFile && minArchSize[archiver] === currArchSize ? (currArchSize + '').green : currArchSize) + ' b' + + utils.addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|'; + + _outputLen += + (' + ' + archiver + ' > ' + currArchSize + ' b' + + utils.addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|').length; + }); + + _output += '\n' + utils.addSymbols(_outputLen - 1, 0, '-'); + + output += _output; + + // logs the results of the test + listOfMinimizers.forEach(function (minimizer) { + var currMinSize = res.min[minimizer].size, + currMinTime = res.min[minimizer].time; + + output += + '\n > was minimized by ' + minimizer + utils.addSymbols(20, minimizer.length, ' ') + '--> ' + + (!outputToFile && minSize === currMinSize ? (currMinSize + '').bold.green : currMinSize) + ' b' + + utils.addSymbols((maxSize + '').length + 1, (currMinSize + '').length, ' ') + '| ' + + (!outputToFile && minTime === currMinTime ? (currMinTime + '').bold.green : currMinTime) + ' ms' + + utils.addSymbols(19 - (rawFileSize + '').length, (currMinTime + ' ms').length, ' '); + + listOfArchivers.forEach(function (archiver) { + var currArchSize = res.arch[archiver][minimizer]; + + output += + ' + ' + archiver + ' > ' + + (!outputToFile && minArchSize[archiver] === currArchSize ? (currArchSize + '').bold.green : currArchSize) + ' b' + + utils.addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|'; + }); + }); + + output += '\n'; + }); + + benchLogger('i', '[c] Everything was compared', ''); + + if (outputToFile) { + try { + benchLogger('i', '[r] Writing the result to the file --> ', outputToFile); + fs.writeFileSync(outputToFile, output); + } catch(err) { + benchLogger('e', '[e] Can not write to the file --> ', outputToFile) + throw err; + } + } else { + benchLogger('i', '[r] Printing the result', ''); + benchLogger('o', output); + } + + return output; +} diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index cf74b01..0000000 --- a/lib/index.js +++ /dev/null @@ -1,153 +0,0 @@ -var fs = require('fs'), - path = require('path'), - shell = require('shelljs'), - bench = require('../.cmb/config'), - utils = require('./utils'), - minimizers = bench.minimizers, - archivers = bench.archivers, - toRawCSS = bench.paths.toRawCSS, - toMinCSS = bench.paths.toMinCSS, - toArchCSS = bench.paths.toArchCSS; - -require('colors'); - -shell.exec('ulimit -n 8192'); - -// constants -var MAX_TIME = Number.MAX_VALUE; - -var listOfMinimizers = Object.keys(minimizers) || [], - listOfArchivers = Object.keys(archivers) || [], - listOfMinLen = listOfMinimizers.length, - listOfArchLen = listOfArchivers.length; - -// makes dirs -listOfMinimizers.forEach(function (minimizer) { - utils.mkdir(path.join(toMinCSS, minimizer)); - listOfArchivers.forEach(function (archiver) { - utils.mkdir(path.join(toArchCSS, archiver, 'raw')); - utils.mkdir(path.join(toArchCSS, archiver, minimizer)); - }) -}); - -// Ok! Let's GO! -var output = '\n' + 'Result:'.bold.green + '\n', - cssFiles = fs.readdirSync(toRawCSS); - -cssFiles.forEach(function (cssFile) { - var pathToRawFile = path.join(toRawCSS, cssFile), - rawFileSize = fs.statSync(pathToRawFile).size; - - // minimizes and archives the file by tools which are specified in 'config.js' - var res = {}; - - if (listOfMinLen) { - res.min = {}; - } else { - console.log(utils.getCurrTime() + ' - ' + 'error'.red + ': [e] Spicify minimizers in \'lib/config.js\''); - process.exit(1); - } - listOfArchLen && (res.arch = {}); - - listOfMinimizers.forEach(function (minimizer) { - var pathToMinCSS = path.join(toMinCSS, minimizer, cssFile), - minimizeMethod = minimizers[minimizer]; - - res.min[minimizer] = minimizeMethod(minimizer, pathToRawFile, pathToMinCSS); - - listOfArchivers.forEach(function (archiver) { - var pathToArchCSS = path.join(toArchCSS, archiver), - pathToRawArchCSS = path.join(pathToArchCSS, 'raw'), - pathToMinArchCSS = path.join(pathToArchCSS, minimizer), - archiveScript = archivers[archiver]; - - if (!res.arch.hasOwnProperty(archiver)) { - res.arch[archiver] = {}; - } - if (!res.arch.hasOwnProperty('raw')) { - res.arch[archiver].raw = archiveScript(cssFile, pathToRawFile, pathToRawArchCSS); - } - res.arch[archiver][minimizer] = archiveScript(cssFile, pathToMinCSS, pathToMinArchCSS); - }); - }); - - // gets the best and the fastest results - var minSize = rawFileSize, - maxSize = rawFileSize, - minTime = MAX_TIME; - - listOfMinimizers.forEach(function (minimizer) { - var currMinSize = res.min[minimizer].size, - currMinTime = res.min[minimizer].time; - - minSize = Math.min(minSize, currMinSize); - maxSize = Math.max(maxSize, currMinSize); - minTime = Math.min(minTime, currMinTime); - }); - - var minArchSize = {}, - maxArchSize = {}; - - listOfArchivers.forEach(function (archiver) { - minArchSize[archiver] = res.arch[archiver].raw; - maxArchSize[archiver] = res.arch[archiver].raw; - listOfMinimizers.forEach(function (minimizer) { - var currArchSize = res.arch[archiver][minimizer]; - - minArchSize[archiver] = Math.min(minArchSize[archiver], currArchSize); - maxArchSize[archiver] = Math.max(maxArchSize[archiver], currArchSize); - }); - }); - - // logs the test file - var _output = - '\n---> '.bold.green + cssFile + utils.addSymbols(40, cssFile.length, ' ') + - ' --> ' + (rawFileSize + '').bold + ' b' + utils.addSymbols(listOfArchLen ? 22 - (rawFileSize + '').length : 2, 0, ' '), - - _outputLen = - ('\n---> ' + cssFile + utils.addSymbols(40, cssFile.length, ' ') + - ' --> ' + rawFileSize + ' b' + utils.addSymbols(listOfArchLen ? 22 - (rawFileSize + '').length : 2, 0, ' ')).length; - - listOfArchivers.forEach(function (archiver) { - var currArchSize = res.arch[archiver].raw; - - _output += - ' + ' + archiver + ' > ' + - (minArchSize[archiver] === currArchSize ? (currArchSize + '').green : currArchSize) + ' b' + - utils.addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|'; - - _outputLen += - (' + ' + archiver + ' > ' + currArchSize + ' b' + - utils.addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|').length; - }); - - _output += '\n' + utils.addSymbols(_outputLen - 1, 0, '-'); - - output += _output; - - // logs the results of the test - listOfMinimizers.forEach(function (minimizer) { - var currMinSize = res.min[minimizer].size, - currMinTime = res.min[minimizer].time; - - output += - '\n > was minimized by ' + minimizer + utils.addSymbols(20, minimizer.length, ' ') + '--> ' + - (minSize === currMinSize ? (currMinSize + '').bold.green : currMinSize) + ' b' + - utils.addSymbols((maxSize + '').length + 1, (currMinSize + '').length, ' ') + '| ' + - (minTime === currMinTime ? (currMinTime + '').bold.green : currMinTime) + ' ms' + - utils.addSymbols(19 - (rawFileSize + '').length, (currMinTime + ' ms').length, ' '); - - listOfArchivers.forEach(function (archiver) { - var currArchSize = res.arch[archiver][minimizer]; - - output += - ' + ' + archiver + ' > ' + - (minArchSize[archiver] === currArchSize ? (currArchSize + '').bold.green : currArchSize) + ' b' + - utils.addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|'; - }); - }); - - output += '\n'; -}); - -console.log(output + '\nDone!'.bold.green); diff --git a/package.json b/package.json index e23fdce..234c4a5 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { - "name": "css-minimizers-bench", + "name": "css-minimizers-benchmark", "version": "0.0.0", "description": "Сompares the work of CSS minimizers", "keywords": [ "css", "minimizers", - "bench", - "css-minimizers-bench" + "benchmark", + "css-minimizers-benchmark" ], - "homepage": "https://github.com/eGavr/css-minimizers-bench", - "bugs": "https://github.com/eGavr/css-minimizers-bench/issues", + "homepage": "https://github.com/eGavr/css-minimizers-benchmark", + "bugs": "https://github.com/eGavr/css-minimizers-benchmark/issues", "author": { "name": "Evgeniy Gavryushin", "email": "job.egavr@yandex.ru", @@ -21,7 +21,7 @@ }, "repository": { "type": "git", - "url": "git://github.com/eGavr/css-minimizers-bench.git" + "url": "git://github.com/eGavr/css-minimizers-benchmark.git" }, "license": "MIT", "dependencies": { @@ -32,7 +32,8 @@ }, "devDependencies": { "colors": "0.6.2", - "shelljs": "0.3.0" + "shelljs": "0.3.0", + "coa": "0.4.1" }, "engines": { "node": ">=0.8.0",