Last active
December 16, 2015 04:19
-
-
Save thanpolas/5376317 to your computer and use it in GitHub Desktop.
when.sequence execution penalty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//var PerfTime = require('perf-time'); | |
//var _ = require('underscore'); | |
var Perf = module.exports = function() { | |
this.startTime = null; | |
// this.t = new PerfTime(); | |
this.t = { | |
get: function() { | |
return Date.now(); | |
} | |
}; | |
this.logs = []; | |
this.tags = []; | |
}; | |
var _singleton; | |
Perf.getSingleton = function() { | |
if (_singleton) { | |
return _singleton; | |
} | |
return (_singleton = new Perf()); | |
}; | |
Perf.prototype.start = function() { | |
this.startTime = this.t.get(); | |
}; | |
Perf.prototype.log = function(optsTag) { | |
this.logs.push(this.t.get()); | |
this.tags.push(optsTag || ''); | |
}; | |
Perf.prototype._getPercent = function(whole, fragment) { | |
return this._round(fragment / whole); | |
}; | |
Perf.prototype._round = function(num) { | |
return num;//Math.round(num * 100) / 100; | |
}; | |
Perf.prototype.result = function() { | |
var max = 0; | |
var min = 0; | |
var diffs = []; | |
var cur = 0; | |
this.logs.forEach(function(stamp, index) { | |
// console.log('stamp:', stamp, index, !_.isNumber(this.logs[index - 1]), stamp - this.logs[index - 1], this.tags[index]); | |
if (!this.logs[index - 1]) { | |
return; | |
} | |
var diff = stamp - this.logs[index - 1]; | |
diffs.push(diff); | |
max = (diff > max ? diff : max); | |
min = (diff < min ? diff : min); | |
}, this); | |
var totalLogs = this.logs.length; | |
var mean = 0; | |
if ( 1 < diffs.length) { | |
mean = diffs.reduce(function(a, b){return a+b;}) / totalLogs; | |
mean = this._round(mean); | |
} | |
var total = this.logs[totalLogs - 1] - this.startTime; | |
return { | |
stats: { | |
max: max, | |
min: min, | |
mean: mean, | |
total: total | |
}, | |
logs: this.logs, | |
tags: this.tags, | |
diffs: diffs, | |
firstLog: this.startTime, | |
lastLog: this.logs[totalLogs - 1] | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var when = require('when'); | |
var sequence = require('when/sequence'); | |
var Perf = require('./perf'); | |
var perf = new Perf(); | |
var noop = function(){}; | |
var main = function() { | |
var def = when.defer(); | |
var def2 = when.defer(); | |
var def3 = when.defer(); | |
var getDef2 = function() { return def2.promise;}; | |
var getDef3 = function() { return def3.promise;}; | |
sequence([getDef2, getDef3]).then(def.resolve, def.reject); | |
setTimeout(def2.resolve); | |
setTimeout(def3.resolve); | |
return def.promise; | |
}; | |
console.log('starting...'); | |
perf.start(); | |
var promises = []; | |
var loops = 500; | |
var promise; | |
function asyncNew(i) { | |
setTimeout(function(){ | |
perf.log('Adding main:' + i); | |
promise = main(); | |
promises.push(promise); | |
if (loops === i + 1){ | |
when.all(promises).then(restore); | |
} | |
}); | |
} | |
perf.log('before for'); | |
for (var i = 0; i < loops; i++) { | |
perf.log(i); | |
asyncNew(i); | |
} | |
perf.log('after for'); | |
function restore(proms) { | |
perf.log('in restore'); | |
var nowTime = Date.now(); | |
var res = perf.result(); | |
console.log('Diff startTime:', nowTime - res.firstLog); | |
console.log('proms:', proms.length); | |
console.log('stats:', res.stats); | |
console.log('diffs:', res.diffs.join(' ')); | |
console.log('diffs len:', res.diffs.length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
clone it, install when and run it: