From 9f0034d968499d769e7f3ca2779919821dca60ae Mon Sep 17 00:00:00 2001 From: adon Date: Tue, 18 Aug 2015 11:32:47 +0800 Subject: [PATCH 1/2] improved benchmark suite --- bin/html-purify.js | 120 +++++++++++++++++++++++++++------------------ package.json | 1 + 2 files changed, 73 insertions(+), 48 deletions(-) diff --git a/bin/html-purify.js b/bin/html-purify.js index 22babea..a16d91a 100755 --- a/bin/html-purify.js +++ b/bin/html-purify.js @@ -8,65 +8,89 @@ See the accompanying LICENSE file for terms. /* This utility prints out sanitized html content */ -var Debug = require("debug"), - progname = 'HTML-Purifier'; -var Purifier = require('../src/html-purify'); - -Debug.enable(progname); +var Purifier = require('../src/html-purify'), + Benchmark = require('benchmark'); (function() { var fs = require('fs'), - file, - lineByLine = 0, - noofargs = 0; + file, + // lineByLine = false, + benchmark = false, + noofargs = process.argv.length; process.argv.forEach(function(val, index) { - ++noofargs; if (index === 2) { file = val; + } else if (index === 3) { + if (val === "--benchmark") { + benchmark = true; + } + // else if (val === "-l") { + // lineByLine = true; + // } } - if (index === 3) { - if (val === "-l") { - lineByLine = 1; - } - } }); - if (noofargs >= 3) { - if (fs.existsSync(file)) { - var data = fs.readFileSync(file, 'utf-8'); - var i; - var output = ''; - if (lineByLine) { - // reading and processing line by line - var data2 = data.split(/\n/); - for (i = 0; i < data2.length; i++) { - if (data2[i].length !== 0) { - output = (new Purifier()).purify(data2[i]); - console.log("*****"); - console.log("input ==> " + data2[i]); - console.log("output ==> " + output); - } - } - } else { - var start = +new Date(); - output = (new Purifier()).purify(data); - /*for (i = 0; i < 100; i++) { - output = (new Purifier()).purify(data); - }*/ - var end = +new Date(); - console.log(output); - //console.log("html-purify runs at a speed of " + 10/((end - start)/1000) + " MB per seconds [" + (end-start)/10/1000 + " second per MB]."); - } - process.exit(0); - } else { - console.log("[ERROR] "+file+" not exist"); - process.exit(1); - } - } else { - console.log("Usage: html-purify "); - process.exit(1); + if (noofargs < 3) { + console.log("Usage: html-purifier [--benchmark]"); + process.exit(1); + } + + if (!fs.existsSync(file)) { + console.log("[ERROR] "+file+" not exist"); + process.exit(1); + } + + var data = fs.readFileSync(file, 'utf-8'), i, output = ''; + + // The following is disabled as it might generate insecure html, + // as it could violate the assumption that purify() must start with the data state + // if (lineByLine) { + // // reading and processing line by line + // var data2 = data.split(/\n/); + // for (i = 0; i < data2.length; i++) { + // if (data2[i].length !== 0) { + // output = (new Purifier()).purify(data2[i]); + // console.log("*****"); + // console.log("input ==> " + data2[i]); + // console.log("output ==> " + output); + // } + // } + // process.exit(0); + // } + + if (!benchmark) { + console.log((new Purifier()).purify(data)); + process.exit(0); + } + else if (benchmark) { + + var suite = new Benchmark.Suite; + var purifier1a = new Purifier(); + var purifier1b = new Purifier({enableTagBalancing:false}); + + suite.add('default', function() { + purifier1a.purify(data); + }) + .add('disabled tag balancing', function() { + purifier1b.purify(data); + }) + // add listeners + .on('cycle', function(event) { + console.log(String(event.target)); + }) + .on('complete', function() { + console.log('Fastest is ', this.filter('fastest').pluck('name')); + + var t = this.filter('fastest')[0].stats.mean; + console.log('Speed/Time is ', data.length/1000000/t + 'MB/s', t + 's'); + }) + // run async + .run({ + // 'minSamples': 10, + 'async': true + }); } }).call(this); diff --git a/package.json b/package.json index 3cc9b65..ad56027 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "css-js": "1.0.3" }, "devDependencies": { + "benchmark": "^1.0.0", "expect.js": "^0.3.1", "grunt": "^0.4.5", "grunt-browserify": "^3.8.0", From 372dac9a70f82bc3a54d3269c766e76879373bde Mon Sep 17 00:00:00 2001 From: adon Date: Mon, 5 Oct 2015 14:37:05 +0800 Subject: [PATCH 2/2] turned readFile to run asynchronously --- bin/html-purify.js | 82 ++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 53 deletions(-) diff --git a/bin/html-purify.js b/bin/html-purify.js index a16d91a..caa6468 100755 --- a/bin/html-purify.js +++ b/bin/html-purify.js @@ -14,69 +14,46 @@ var Purifier = require('../src/html-purify'), (function() { var fs = require('fs'), file, - // lineByLine = false, benchmark = false, - noofargs = process.argv.length; + argv = process.argv; - process.argv.forEach(function(val, index) { - if (index === 2) { - file = val; - } else if (index === 3) { - if (val === "--benchmark") { - benchmark = true; - } - // else if (val === "-l") { - // lineByLine = true; - // } - } - }); - - - if (noofargs < 3) { - console.log("Usage: html-purifier [--benchmark]"); - process.exit(1); - } - - if (!fs.existsSync(file)) { - console.log("[ERROR] "+file+" not exist"); - process.exit(1); + // pick up the parameters + switch (argv.length) { + case 4: + benchmark = argv[3] === '--benchmark'; + case 3: + file = argv[2]; + break; + default: + console.log("Usage: html-purifier [--benchmark]"); + process.exit(1); + return; } - var data = fs.readFileSync(file, 'utf-8'), i, output = ''; - - // The following is disabled as it might generate insecure html, - // as it could violate the assumption that purify() must start with the data state - // if (lineByLine) { - // // reading and processing line by line - // var data2 = data.split(/\n/); - // for (i = 0; i < data2.length; i++) { - // if (data2[i].length !== 0) { - // output = (new Purifier()).purify(data2[i]); - // console.log("*****"); - // console.log("input ==> " + data2[i]); - // console.log("output ==> " + output); - // } - // } - // process.exit(0); - // } + // read the given file path for processing + fs.readFile(file, 'utf8', function (err, data) { + if (err) { + throw err; + } - if (!benchmark) { - console.log((new Purifier()).purify(data)); - process.exit(0); - } - else if (benchmark) { + // print the processed file + if (!benchmark) { + console.log((new Purifier()).purify(data)); + return; + } + // benchmarking + console.log('Benchmarking...'); var suite = new Benchmark.Suite; var purifier1a = new Purifier(); - var purifier1b = new Purifier({enableTagBalancing:false}); + // var purifier1b = new Purifier({enableTagBalancing:false}); suite.add('default', function() { purifier1a.purify(data); }) - .add('disabled tag balancing', function() { - purifier1b.purify(data); - }) - // add listeners + // .add('disabled tag balancing', function() { + // purifier1b.purify(data); + // }) .on('cycle', function(event) { console.log(String(event.target)); }) @@ -86,11 +63,10 @@ var Purifier = require('../src/html-purify'), var t = this.filter('fastest')[0].stats.mean; console.log('Speed/Time is ', data.length/1000000/t + 'MB/s', t + 's'); }) - // run async .run({ // 'minSamples': 10, 'async': true }); - } + }); }).call(this);