diff --git a/bin/html-purify.js b/bin/html-purify.js
index 22babea..caa6468 100755
--- a/bin/html-purify.js
+++ b/bin/html-purify.js
@@ -8,65 +8,65 @@ 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,
+ benchmark = false,
+ argv = process.argv;
+
+ // 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;
+ }
- process.argv.forEach(function(val, index) {
- ++noofargs;
- if (index === 2) {
- file = val;
+ // read the given file path for processing
+ fs.readFile(file, 'utf8', function (err, data) {
+ if (err) {
+ throw err;
}
- 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);
- }
- }
+ // print the processed file
+ if (!benchmark) {
+ console.log((new Purifier()).purify(data));
+ return;
+ }
- } 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);
- }
+ // benchmarking
+ console.log('Benchmarking...');
+ 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);
+ // })
+ .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({
+ // '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",