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