Skip to content

Instantly share code, notes, and snippets.

@thanpolas thanpolas/perf.js
Last active Dec 16, 2015

Embed
What would you like to do?
when.sequence execution penalty
//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]
};
};
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);
}
@thanpolas

This comment has been minimized.

Copy link
Owner Author

commented Apr 13, 2013

clone it, install when and run it:

https://gist.github.com/5376317.git

npm install when

node sequence.perf.js 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.