forked from albanm/node-libxslt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.js
151 lines (130 loc) · 4.44 KB
/
benchmark.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var fs = require('fs');
var async = require('async');
var libxslt = require('./index');
var libxmljs = libxslt.libxmljs;
var stylesheetStr = fs.readFileSync('./test/resources/cd.xsl', 'utf8');
var stylesheetObj = libxmljs.parseXml(stylesheetStr);
var stylesheet = libxslt.parse(stylesheetObj);
var docStr = fs.readFileSync('./test/resources/cd.xml', 'utf8');
var docObj = libxmljs.parseXml(docStr);
var bench = function(name, iterations, f) {
return function(callback) {
var before = Date.now();
f(iterations, function() {
var duration = (Date.now() - before);
console.log('%d %s in %dms = %d/s', iterations, name, duration, Math.round(iterations / (duration / 1000)));
if (callback) callback();
});
};
};
var stylesheetSyncParsingStr = function(iterations, callback) {
for (var i = 0; i < iterations; i++) {
libxslt.parse(stylesheetStr);
}
callback();
};
var stylesheetSyncParsingObj = function(iterations, callback) {
for (var i = 0; i < iterations; i++) {
libxslt.parse(stylesheetObj);
}
callback();
};
var stylesheetAsyncSeriesParsingStr = function(iterations, callback) {
var i = 0;
async.eachSeries(new Array(iterations), function(u, callbackEach) {
libxslt.parse(stylesheetStr, function(err, result) {
i++;
callbackEach(err);
});
}, callback);
};
var stylesheetAsyncSeriesParsingObj = function(iterations, callback) {
var i = 0;
async.eachSeries(new Array(iterations), function(u, callbackEach) {
libxslt.parse(stylesheetObj, function(err, result) {
i++;
callbackEach(err);
});
}, callback);
};
var stylesheetAsyncParallelParsingStr = function(iterations, callback) {
var i = 0;
async.eachLimit(new Array(iterations), 10, function(u, callbackEach) {
libxslt.parse(stylesheetStr, function(err, result) {
i++;
callbackEach(err);
});
}, callback);
};
var stylesheetAsyncParallelParsingObj = function(iterations, callback) {
var i = 0;
async.eachLimit(new Array(iterations), 10, function(u, callbackEach) {
libxslt.parse(stylesheetObj, function(err, result) {
i++;
callbackEach(err);
});
}, callback);
};
var applySyncStr = function(iterations, callback) {
for (var i = 0; i < iterations; i++) {
stylesheet.apply(docStr);
}
callback();
};
var applySyncObj = function(iterations, callback) {
for (var i = 0; i < iterations; i++) {
stylesheet.apply(docObj);
}
callback();
};
var applyAsyncSeriesStr = function(iterations, callback) {
var i = 0;
async.eachSeries(new Array(iterations), function(u, callbackEach) {
stylesheet.apply(docStr, function(err, result) {
i++;
callbackEach(err);
});
}, callback);
};
var applyAsyncSeriesObj = function(iterations, callback) {
var i = 0;
async.eachSeries(new Array(iterations), function(u, callbackEach) {
stylesheet.apply(docObj, function(err, result) {
i++;
callbackEach(err);
});
}, callback);
};
var applyAsyncParallelStr = function(iterations, callback) {
var i = 0;
async.eachLimit(new Array(iterations), 10, function(u, callbackEach) {
stylesheet.apply(docStr, function(err, result) {
i++;
callbackEach(err);
});
}, callback);
};
var applyAsyncParallelObj = function(iterations, callback) {
var i = 0;
async.eachLimit(new Array(iterations), 10, function(u, callbackEach) {
stylesheet.apply(docObj, function(err, result) {
i++;
callbackEach(err);
});
}, callback);
};
var iterations = 10000;
async.series([
//bench('synchronous parse from string\t\t\t', iterations, stylesheetSyncParsingStr),
bench('synchronous parse from parsed doc\t\t\t', iterations, stylesheetSyncParsingObj),
//bench('asynchronous parse in series from string\t\t\t', iterations, stylesheetAsyncSeriesParsingStr),
bench('asynchronous parse in series from parsed doc\t', iterations, stylesheetAsyncSeriesParsingObj),
//bench('asynchronous parse in parallel from string\t\t\t', iterations, stylesheetAsyncParallelParsingStr),
bench('asynchronous parse in parallel from parsed doc\t', iterations, stylesheetAsyncParallelParsingObj),
//bench('synchronous apply from string\t\t\t', iterations, applySyncStr),
bench('synchronous apply from parsed doc\t\t\t', iterations, applySyncObj),
//bench('asynchronous apply in series from string\t\t', iterations, applyAsyncSeriesStr),
bench('asynchronous apply in series from parsed doc\t', iterations, applyAsyncSeriesObj),
//bench('asynchronous apply in parallel from string\t', iterations, applyAsyncParallelStr),
bench('asynchronous apply in parallel from parsed doc\t', iterations, applyAsyncParallelObj)
]);