From 45dd4c06b025f8bf1c13ae74596946ec9d2efd4c Mon Sep 17 00:00:00 2001 From: adon Date: Mon, 17 Aug 2015 17:55:53 +0800 Subject: [PATCH] improved benchmark suite --- bin/html-purifier.js | 118 +++++++++++++++++++++++++------------------ package.json | 1 + 2 files changed, 71 insertions(+), 48 deletions(-) diff --git a/bin/html-purifier.js b/bin/html-purifier.js index 4d9c07c..549ed9c 100755 --- a/bin/html-purifier.js +++ b/bin/html-purifier.js @@ -8,65 +8,87 @@ 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; - } - if (index === 3) { + } else if (index === 3) { if (val === "-l") { - lineByLine = 1; - } - } + lineByLine = true; + } else if (val === "--benchmark") { + benchmark = true; + } + } }); - 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-purifier "); - process.exit(1); + if (noofargs < 3) { + console.log("Usage: html-purifier "); + 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, since linebreak can exists within attr value state, while purify() assumes data state for each call + // 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 2694ab0..d9e8306 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",