Permalink
Browse files

runtimer

  • Loading branch information...
1 parent 51efeaf commit b0f34213460304942fa3ee7a4295afd5ddc168fd @krzysztof-o committed Mar 31, 2013
Showing with 199 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +7 −0 Makefile
  3. +51 −0 index.js
  4. +30 −0 package.json
  5. +110 −0 test/tests.js
View
@@ -0,0 +1 @@
+node_modules/
View
@@ -0,0 +1,7 @@
+REPORTER = dot
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --reporter $(REPORTER) \
+
+.PHONY: test
View
@@ -0,0 +1,51 @@
+
+var startTime = now();
+var enabled = true;
+
+process.on('exit', function() {
+ var diffTime = now() - startTime;
+ if (enabled) {
+ console.log('\nScript execution time: %s', formatTime(diffTime));
+ }
+});
+
+function now() {
+ return new Date().getTime();
+}
+
+function formatTime(time) {
+ var str = '';
+
+ var hours = Math.floor(time / 1000 / 60 / 60);
+ var minutes = Math.floor(time / 1000 / 60) % 60;
+ var seconds = Math.floor(time / 1000) % 60;
+ var milliseconds = time % 1000;
+
+ if (hours > 0) {
+ str += addZero(hours) + 'h ';
+ }
+ if (minutes > 0 || hours > 0) {
+ str += addZero(minutes) + 'min ';
+ }
+ if (seconds > 0 || minutes > 0 || hours > 0) {
+ str += addZero(seconds) + 'sec ';
+ }
+ str += addZero(milliseconds)+ 'ms ';
+
+ //trim right
+ while (str.charAt(str.length - 1) == ' ') {
+ str = str.substring(0, str.length - 1);
+ }
+ return str;
+}
+
+function addZero(value) {
+ return value < 10 ? "0" + value : value;
+}
+
+module.exports.disable = function() {
+ enabled = false;
+}
+module.exports.enable = function() {
+ enabled = true;
+}
View
@@ -0,0 +1,30 @@
+{
+ "name": "node-runtimer",
+ "version": "1.0.0",
+ "description": "Simple utillity to measure execution time",
+ "main": "index.js",
+ "directories": {
+ "test": "test"
+ },
+ "dependencies": {
+ },
+ "devDependencies": {
+ "mocha": "~1.8.2"
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/krzysztof-o/node-runtimer.git"
+ },
+ "keywords": [
+ "time",
+ "profiler",
+ "execution"
+ ],
+ "author": "Krzysztof Opałka",
+ "license": "BSD",
+ "readmeFilename": "README.md",
+ "gitHead": "51efeaf9c6ec058a61733e2cb600832ee30e5b35"
+}
View
@@ -0,0 +1,110 @@
+var _getTime = Date.prototype.getTime;
+setCurrentTime(0);
+
+var runtimer = require('../');
+var assert = require('assert');
+var sinon = require('sinon');
+
+describe('Time formatter', function() {
+
+ after(function(){
+ Date.prototype.getTime = _getTime.bind(new Date());
+ });
+
+ var SEC = 1000;
+ var MIN = 60 * SEC;
+ var HOUR = 60 * MIN;
+
+ it('should parse milliseconds', function(done) {
+ assertTime(50, '50ms', done);
+ });
+
+ it('should parse seconds', function(done) {
+ assertTime(2 * SEC, '02sec 00ms', done);
+ });
+
+ it('should parse seconds and milliseconds', function(done) {
+ assertTime(5 * SEC + 123, '05sec 123ms', done);
+ });
+
+ it('should parse minutes', function(done) {
+ assertTime(3 * MIN, '03min 00sec 00ms', done);
+ });
+
+ it('should parse minutes and seconds and milliseconds', function(done) {
+ assertTime(3 * MIN + 2 * SEC + 123, '03min 02sec 123ms', done);
+ });
+
+ it('should parse hours', function(done) {
+ assertTime(4 * HOUR, '04h 00min 00sec 00ms', done);
+ });
+
+ it('should parse hours minutes and seconds and milliseconds', function(done) {
+ assertTime(4 * HOUR + 3 * MIN + 2 * SEC + 123, '04h 03min 02sec 123ms', done);
+ });
+
+ it('should not parse days but hours minutes and seconds and milliseconds', function(done) {
+ assertTime(30 * HOUR + 3 * MIN + 2 * SEC + 123, '30h 03min 02sec 123ms', done);
+ });
+
+});
+
+describe('Runtimer', function() {
+
+ after(function() {
+ runtimer.disable();
+ });
+
+ it('should be enabled by default', function(done) {
+ var temp = console.log;
+ console.log = function(message, date) {
+ console.log = temp;
+ done();
+ }
+ process.emit('exit');
+ });
+
+ it('should be enabled', function(done) {
+ runtimer.disable();
+ runtimer.enable();
+ var temp = console.log;
+ console.log = function(message, date) {
+ console.log = temp;
+ done();
+ }
+ process.emit('exit');
+ });
+
+ it('should be disabled', function() {
+ runtimer.disable();
+ var observed = false;
+ var temp = console.log;
+ console.log = function(message, date) {
+ console.log = temp;
+ observed = true;
+ }
+ setTimeout(function() {
+ if (!observed) {
+ done();
+ }
+ }, 1000);
+ });
+
+});
+
+function assertTime(ms, output, done) {
+ var temp = console.log;
+ console.log = function(message, date) {
+ console.log = temp;
+ assert.equal(date, output);
+ done();
+ }
+ setCurrentTime(ms);
+ process.emit('exit');
+}
+
+function setCurrentTime(currentTime) {
+ Date.prototype.getTime = function() {
+ return currentTime;
+ }
+}

0 comments on commit b0f3421

Please sign in to comment.