Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
improved benchmark suite
Browse files Browse the repository at this point in the history
  • Loading branch information
adon committed Aug 30, 2015
1 parent fa24fa2 commit 45dd4c0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 48 deletions.
118 changes: 70 additions & 48 deletions bin/html-purifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <any html file>");
process.exit(1);
if (noofargs < 3) {
console.log("Usage: html-purifier <any html file>");
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);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 45dd4c0

Please sign in to comment.