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

improved benchmark suite #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 53 additions & 53 deletions bin/html-purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <html_filepath> [--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 <any html file>");
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));
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to dump event.target to console log?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shows the intermediate results of every test. now we have only one test, but we'll have more...
if that onCycle dump is commented. nothing will show up until everything is completed. that'd look like hanged...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in other words, if that is omitted, we will miss something like below

default x 0.64 ops/sec ±5.90% (6 runs sampled)

but will only see:

Fastest is [ 'default' ]
Speed/Time is 0.6423016849725829MB/s 1.5567777936666667s

.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);
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