forked from cheeriojs/cheerio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite.js
91 lines (80 loc) · 2.24 KB
/
suite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var fs = require('fs');
var path = require('path');
var Benchmark = require('benchmark');
var jsdom = require('jsdom');
var cheerio = require('..');
var documentDir = path.join(__dirname, 'documents');
var jQuerySrc = path.join(__dirname, 'jquery-2.0.3.js');
var filterRe = /./;
var cheerioOnly = false;
var Suites = module.exports = function() {};
Suites.prototype.filter = function(str) {
filterRe = new RegExp(str, 'i');
};
Suites.prototype.cheerioOnly = function() {
cheerioOnly = true;
};
Suites.prototype.add = function(name, fileName, options) {
var markup, suite, testFn;
if (!filterRe.test(name)) {
return;
}
markup = fs.readFileSync(path.join(documentDir, fileName), 'utf8');
suite = new Benchmark.Suite(name);
testFn = options.test;
suite.on('start', function(event) {
console.log('Test: ' + name + ' (file: ' + fileName + ')');
});
suite.on('cycle', function(event) {
if (event.target.error) {
return;
}
console.log('\t' + String(event.target));
});
suite.on('error', function(event) {
console.log('*** Error in ' + event.target.name + ': ***');
console.log('\t' + event.target.error);
console.log('*** Test invalidated. ***');
});
suite.on('complete', function(event) {
if (event.target.error) {
console.log();
return;
}
console.log('\tFastest: ' + this.filter('fastest').pluck('name') + '\n');
});
this._benchCheerio(suite, markup, options);
if (!cheerioOnly) {
this._benchJsDom(suite, markup, options);
} else {
suite.run();
}
};
Suites.prototype._benchJsDom = function(suite, markup, options) {
var testFn = options.test;
jsdom.env({
html: markup,
scripts: jQuerySrc,
done: function(err, window) {
var setupData;
if (options.setup) {
setupData = options.setup.call(null, window.$);
}
suite.add('jsdom', function() {
testFn.call(null, window.$, setupData);
});
suite.run();
}
});
};
Suites.prototype._benchCheerio = function(suite, markup, options) {
var $ = cheerio.load(markup);
var testFn = options.test;
var setupData;
if (options.setup) {
setupData = options.setup.call(null, $);
}
suite.add('cheerio', function() {
testFn.call(null, $, setupData);
});
};