| 1 |
/* eslint max-lines: "off" */ |
| 2 |
|
| 3 |
"use strict"; |
| 4 |
|
| 5 |
// Benchmark comparing performance of promise setups (concurrent) |
| 6 |
// To run it, do following in package path: |
| 7 |
// |
| 8 |
// $ npm install Q when kew bluebird |
| 9 |
// $ node benchmark/concurrent.js |
| 10 |
|
| 11 |
var generate = require("es5-ext/array/generate") |
| 12 |
, forEach = require("es5-ext/object/for-each") |
| 13 |
, pad = require("es5-ext/string/#/pad") |
| 14 |
, lstat = require("fs").lstat |
| 15 |
, QLib = require("Q") |
| 16 |
, Bluebird = require("bluebird") |
| 17 |
, kew = require("kew") |
| 18 |
, when = require("when") |
| 19 |
, deferred = require("../"); |
| 20 |
|
| 21 |
var now = Date.now |
| 22 |
, Deferred = deferred.Deferred |
| 23 |
, promisify = deferred.promisify |
| 24 |
, nextTick = process.nextTick |
| 25 |
, self |
| 26 |
, time |
| 27 |
, count = 10000 |
| 28 |
, data = {} |
| 29 |
, next |
| 30 |
, tests |
| 31 |
, def = deferred() |
| 32 |
, files = generate(count, __filename); |
| 33 |
|
| 34 |
console.log("Promise overhead (concurrent calls)", "x" + count + ":\n"); |
| 35 |
|
| 36 |
// Plain |
| 37 |
tests = [ |
| 38 |
function () { |
| 39 |
var i = count, j = count; |
| 40 |
self = function () { |
| 41 |
lstat(__filename, function (err) { |
| 42 |
if (err) throw err; |
| 43 |
if (!--i) next(); // Ignore first |
| 44 |
}); |
| 45 |
}; |
| 46 |
time = now(); |
| 47 |
while (j--) self(); |
| 48 |
}, |
| 49 |
function () { |
| 50 |
var i = count, j = count; |
| 51 |
self = function () { |
| 52 |
lstat(__filename, function (err) { |
| 53 |
if (err) throw err; |
| 54 |
if (!--i) { |
| 55 |
data["Base (plain Node.js lstat call)"] = now() - time; |
| 56 |
next(); |
| 57 |
} |
| 58 |
}); |
| 59 |
}; |
| 60 |
time = now(); |
| 61 |
while (j--) self(); |
| 62 |
|
| 63 |
// Bluebird |
| 64 |
}, |
| 65 |
function () { |
| 66 |
var i = count, j = count, dlstat = Bluebird.promisify(lstat); |
| 67 |
|
| 68 |
self = function () { |
| 69 |
dlstat(__filename).done(function () { |
| 70 |
if (!--i) next(); // Ignore first |
| 71 |
}); |
| 72 |
}; |
| 73 |
time = now(); |
| 74 |
while (j--) self(); |
| 75 |
}, |
| 76 |
function () { |
| 77 |
var i = count, j = count, dlstat = Bluebird.promisify(lstat); |
| 78 |
|
| 79 |
self = function () { |
| 80 |
dlstat(__filename).done(function () { |
| 81 |
if (!--i) { |
| 82 |
data["Bluebird: Promisify (generic wrapper)"] = now() - time; |
| 83 |
next(); |
| 84 |
} |
| 85 |
}); |
| 86 |
}; |
| 87 |
time = now(); |
| 88 |
while (j--) self(); |
| 89 |
}, |
| 90 |
function () { |
| 91 |
var i = count, j = count, dlstat; |
| 92 |
|
| 93 |
dlstat = function (path) { |
| 94 |
return new Bluebird(function (resolve, reject) { |
| 95 |
lstat(path, function (err, stats) { |
| 96 |
if (err) reject(err); |
| 97 |
else resolve(stats); |
| 98 |
}); |
| 99 |
}); |
| 100 |
}; |
| 101 |
|
| 102 |
self = function () { |
| 103 |
dlstat(__filename).done(function () { |
| 104 |
if (!--i) next(); // Ignore first |
| 105 |
}); |
| 106 |
}; |
| 107 |
time = now(); |
| 108 |
while (j--) self(); |
| 109 |
}, |
| 110 |
function () { |
| 111 |
var i = count, j = count, dlstat; |
| 112 |
|
| 113 |
dlstat = function (path) { |
| 114 |
return new Bluebird(function (resolve, reject) { |
| 115 |
lstat(path, function (err, stats) { |
| 116 |
if (err) reject(err); |
| 117 |
else resolve(stats); |
| 118 |
}); |
| 119 |
}); |
| 120 |
}; |
| 121 |
|
| 122 |
self = function () { |
| 123 |
dlstat(__filename).done(function () { |
| 124 |
if (!--i) { |
| 125 |
data["Bluebird: Dedicated wrapper"] = now() - time; |
| 126 |
next(); |
| 127 |
} |
| 128 |
}); |
| 129 |
}; |
| 130 |
time = now(); |
| 131 |
while (j--) self(); |
| 132 |
|
| 133 |
// Kew |
| 134 |
}, |
| 135 |
function () { |
| 136 |
var i = count, j = count, dlstat; |
| 137 |
|
| 138 |
dlstat = function (path) { |
| 139 |
var localDef = kew.defer(); |
| 140 |
lstat(path, function (err, stats) { |
| 141 |
if (err) localDef.reject(err); |
| 142 |
else localDef.resolve(stats); |
| 143 |
}); |
| 144 |
return localDef; |
| 145 |
}; |
| 146 |
|
| 147 |
self = function () { |
| 148 |
dlstat(__filename).then( |
| 149 |
function () { |
| 150 |
if (!--i) nextTick(next); // Ignore first |
| 151 |
}, |
| 152 |
function (err) { |
| 153 |
nextTick(function () { throw err; }); |
| 154 |
} |
| 155 |
); |
| 156 |
}; |
| 157 |
time = now(); |
| 158 |
while (j--) self(); |
| 159 |
}, |
| 160 |
function () { |
| 161 |
var i = count, j = count, dlstat; |
| 162 |
|
| 163 |
dlstat = function (path) { |
| 164 |
var localDef = kew.defer(); |
| 165 |
lstat(path, function (err, stats) { |
| 166 |
if (err) localDef.reject(err); |
| 167 |
else localDef.resolve(stats); |
| 168 |
}); |
| 169 |
return localDef; |
| 170 |
}; |
| 171 |
|
| 172 |
self = function () { |
| 173 |
dlstat(__filename).then( |
| 174 |
function () { |
| 175 |
if (!--i) { |
| 176 |
data["Kew: Dedicated wrapper"] = now() - time; |
| 177 |
// Get out of try/catch clause |
| 178 |
nextTick(next); |
| 179 |
} |
| 180 |
}, |
| 181 |
function (err) { |
| 182 |
nextTick(function () { throw err; }); |
| 183 |
} |
| 184 |
); |
| 185 |
}; |
| 186 |
time = now(); |
| 187 |
while (j--) self(); |
| 188 |
|
| 189 |
// Deferred |
| 190 |
}, |
| 191 |
function () { |
| 192 |
var i = count, j = count, dlstat; |
| 193 |
dlstat = function (path) { |
| 194 |
var localDef = new Deferred(); |
| 195 |
lstat(path, function (err, stats) { localDef.resolve(err || stats); }); |
| 196 |
return localDef.promise; |
| 197 |
}; |
| 198 |
|
| 199 |
self = function () { |
| 200 |
dlstat(__filename).done(function () { |
| 201 |
if (!--i) next(); // Ignore first |
| 202 |
}); |
| 203 |
}; |
| 204 |
time = now(); |
| 205 |
while (j--) self(); |
| 206 |
}, |
| 207 |
function () { |
| 208 |
var i = count, j = count, dlstat; |
| 209 |
dlstat = function (path) { |
| 210 |
var localDef = new Deferred(); |
| 211 |
lstat(path, function (err, stats) { localDef.resolve(err || stats); }); |
| 212 |
return localDef.promise; |
| 213 |
}; |
| 214 |
|
| 215 |
self = function () { |
| 216 |
dlstat(__filename).done(function () { |
| 217 |
if (!--i) { |
| 218 |
data["Deferred: Dedicated wrapper"] = now() - time; |
| 219 |
next(); |
| 220 |
} |
| 221 |
}); |
| 222 |
}; |
| 223 |
time = now(); |
| 224 |
while (j--) self(); |
| 225 |
}, |
| 226 |
function () { |
| 227 |
var i = count, j = count, dlstat = promisify(lstat); |
| 228 |
|
| 229 |
self = function () { |
| 230 |
dlstat(__filename).done(function () { |
| 231 |
if (!--i) next(); // Ignore first |
| 232 |
}); |
| 233 |
}; |
| 234 |
time = now(); |
| 235 |
while (j--) self(); |
| 236 |
}, |
| 237 |
function () { |
| 238 |
var i = count, j = count, dlstat = promisify(lstat); |
| 239 |
|
| 240 |
self = function () { |
| 241 |
dlstat(__filename).done(function () { |
| 242 |
if (!--i) { |
| 243 |
data["Deferred: Promisify (generic wrapper)"] = now() - time; |
| 244 |
next(); |
| 245 |
} |
| 246 |
}); |
| 247 |
}; |
| 248 |
time = now(); |
| 249 |
while (j--) self(); |
| 250 |
}, |
| 251 |
function () { |
| 252 |
var dlstat = promisify(lstat); |
| 253 |
|
| 254 |
time = now(); |
| 255 |
deferred.map(files, function (name) { return dlstat(name); }).done(function () { next(); }); |
| 256 |
}, |
| 257 |
function () { |
| 258 |
var dlstat = promisify(lstat); |
| 259 |
|
| 260 |
time = now(); |
| 261 |
deferred.map(files, function (name) { return dlstat(name); }).done(function () { next(); }); |
| 262 |
}, |
| 263 |
function () { |
| 264 |
var dlstat = promisify(lstat); |
| 265 |
|
| 266 |
time = now(); |
| 267 |
deferred |
| 268 |
.map(files, function (name) { return dlstat(name); }) |
| 269 |
.done(function () { |
| 270 |
data["Deferred: Map + Promisify"] = now() - time; |
| 271 |
next(); |
| 272 |
}); |
| 273 |
|
| 274 |
// Q |
| 275 |
}, |
| 276 |
function () { |
| 277 |
var i = count, j = count, dlstat; |
| 278 |
|
| 279 |
dlstat = function (path) { |
| 280 |
var localDef = QLib.defer(); |
| 281 |
lstat(path, function (err, stats) { |
| 282 |
if (err) localDef.reject(err); |
| 283 |
else localDef.resolve(stats); |
| 284 |
}); |
| 285 |
return localDef.promise; |
| 286 |
}; |
| 287 |
|
| 288 |
self = function () { |
| 289 |
dlstat(__filename).done(function () { if (!--i) next(); }); |
| 290 |
}; |
| 291 |
time = now(); |
| 292 |
while (j--) self(); |
| 293 |
}, |
| 294 |
function () { |
| 295 |
var i = count, j = count, dlstat; |
| 296 |
|
| 297 |
dlstat = function (path) { |
| 298 |
var localDef = QLib.defer(); |
| 299 |
lstat(path, function (err, stats) { |
| 300 |
if (err) localDef.reject(err); |
| 301 |
else localDef.resolve(stats); |
| 302 |
}); |
| 303 |
return localDef.promise; |
| 304 |
}; |
| 305 |
|
| 306 |
self = function () { |
| 307 |
dlstat(__filename).done(function () { |
| 308 |
if (!--i) { |
| 309 |
data["Q: Dedicated wrapper"] = now() - time; |
| 310 |
next(); |
| 311 |
} |
| 312 |
}); |
| 313 |
}; |
| 314 |
time = now(); |
| 315 |
while (j--) self(); |
| 316 |
}, |
| 317 |
function () { |
| 318 |
var i = count, j = count, dlstat = QLib.nbind(lstat, null); |
| 319 |
|
| 320 |
self = function () { |
| 321 |
dlstat(__filename).done(function () { if (!--i) next(); }); |
| 322 |
}; |
| 323 |
time = now(); |
| 324 |
while (j--) self(); |
| 325 |
}, |
| 326 |
function () { |
| 327 |
var i = count, j = count, dlstat = QLib.nbind(lstat, null); |
| 328 |
|
| 329 |
self = function () { |
| 330 |
dlstat(__filename).done(function () { |
| 331 |
if (!--i) { |
| 332 |
data["Q: nbind (generic wrapper)"] = now() - time; |
| 333 |
// Get out of try/catch clause |
| 334 |
next(); |
| 335 |
} |
| 336 |
}); |
| 337 |
}; |
| 338 |
time = now(); |
| 339 |
while (j--) self(); |
| 340 |
}, |
| 341 |
function () { |
| 342 |
var i = count, j = count, dlstat; |
| 343 |
|
| 344 |
dlstat = function (path) { |
| 345 |
var localDef = when.defer(); |
| 346 |
lstat(path, function (err, stats) { |
| 347 |
if (err) localDef.reject(err); |
| 348 |
else localDef.resolve(stats); |
| 349 |
}); |
| 350 |
return localDef.promise; |
| 351 |
}; |
| 352 |
|
| 353 |
self = function () { |
| 354 |
dlstat(__filename).then( |
| 355 |
function () { if (!--i) nextTick(next); }, |
| 356 |
function (e) { |
| 357 |
nextTick(function () { throw e; }); |
| 358 |
} |
| 359 |
); |
| 360 |
}; |
| 361 |
|
| 362 |
time = now(); |
| 363 |
while (j--) self(); |
| 364 |
}, |
| 365 |
function () { |
| 366 |
var i = count, j = count, dlstat; |
| 367 |
|
| 368 |
dlstat = function (path) { |
| 369 |
var localDef = when.defer(); |
| 370 |
lstat(path, function (err, stats) { |
| 371 |
if (err) localDef.reject(err); |
| 372 |
else localDef.resolve(stats); |
| 373 |
}); |
| 374 |
return localDef.promise; |
| 375 |
}; |
| 376 |
|
| 377 |
self = function () { |
| 378 |
dlstat(__filename).then( |
| 379 |
function () { |
| 380 |
if (!--i) { |
| 381 |
data["When: Dedicated wrapper"] = now() - time; |
| 382 |
nextTick(next); |
| 383 |
} |
| 384 |
}, |
| 385 |
function (e) { |
| 386 |
nextTick(function () { throw e; }); |
| 387 |
} |
| 388 |
); |
| 389 |
}; |
| 390 |
|
| 391 |
time = now(); |
| 392 |
while (j--) self(); |
| 393 |
} |
| 394 |
]; |
| 395 |
|
| 396 |
next = function () { |
| 397 |
setTimeout(function () { |
| 398 |
if (tests.length) { |
| 399 |
tests.shift()(); |
| 400 |
} else { |
| 401 |
def.resolve(); |
| 402 |
forEach( |
| 403 |
data, |
| 404 |
function (value, name, obj, index) { |
| 405 |
console.log( |
| 406 |
pad.call(index + 1 + ":", " ", 3), pad.call(value, " ", 5) + "ms ", name |
| 407 |
); |
| 408 |
}, |
| 409 |
null, |
| 410 |
function (data1, data2) { return this[data1] - this[data2]; } |
| 411 |
); |
| 412 |
} |
| 413 |
}, 100); |
| 414 |
}; |
| 415 |
|
| 416 |
next(); |