Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created December 18, 2008 19:59
Show Gist options
  • Select an option

  • Save subtleGradient/37623 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/37623 to your computer and use it in GitHub Desktop.
var SubtleBenchmarker = new new Class({
benchmarks:{},
start : function(key){
this.benchmarks[key] = this.benchmarks[key] || [];
this.benchmarks[key].push({ start:new Date() });
},
end : function(key){
this.benchmarks[key][this.benchmarks[key].length-1].end = new Date();
},
report: function(key){
var report = {};
Hash.each(this.benchmarks, function(benchmarks, key){
report[key] = {them:[]};
report[key].total = 0;
benchmarks.each(function(benchmark){
benchmark.total = benchmark.end - benchmark.start;
report[key].them.push(benchmark.total);
report[key].total += benchmark.total;
});
report[key].count = report[key].them.length;
report[key].average = report[key].total / report[key].count;
delete report[key].them;
});
report_text = '';
for (var property in report) {
report_text += property;
report_text += ': ';
report_text += pp(report[property]);
report_text += '\n';
}
try{
console.log(report_text);
}catch(e){
alert(report_text);
};
}
});
SubtleBenchmarker.start('Benchmark Name');
// Do something that takes a long time or something
SubtleBenchmarker.end('Benchmark Name');
SubtleBenchmarker.start('Benchmark Name');
// Do the same thing again
SubtleBenchmarker.end('Benchmark Name');
SubtleBenchmarker.start('a different Benchmark Name');
// Do something else
SubtleBenchmarker.end('a different Benchmark Name');
SubtleBenchmarker.report(); // View a report of all your activities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment