Source: perf/PerformanceMonitor.js

/**
 * @version  1.0.0
 * @author Boris GBAHOUE
 * @file Common PerformanceMonitor
 * @module amiwo/perf
*/


// =======================================================================
// BASE SETUP
// =======================================================================
// call the packages we need
var u                       = require('../util');
var uuid                    = require('node-uuid');

// public static variables
var __perfHash               = {};

// =======================================================================
// CONSTRUCTOR
// =======================================================================
/**
 * @class
 */
function PerformanceMonitor() {
    // Empty
}

// =======================================================================
// PUBLIC METHODS
// =======================================================================
/**
 * Adds a new timestamp for monitor ID 'id'
 * 
 * @param {Number} [id] timer id, creates a unique ID if null
 * @return {Number} id
 * @public
 */
PerformanceMonitor.addTimer = function(id) {
    if (u.isEmpty(id)) {
        id = uuid.v4();
    }
    
    __perfHash[id] = Date.now();
    return id;
}

/**
 * Gets a timestamp for monitor ID 'id'
 * 
 * @param {Number} id timer id
 * @param {Boolean} [del] set to true to delete the timer
 * 
 * @returns {Number} timestamp at which the timer was set
 * 
 * @public
 */
PerformanceMonitor.getTimer = function(id, del) {
    if (u.isEmpty(id)) return;
    del = del || false;
    
    var timer = __perfHash[id];
    if (del) delete __perfHash[id];
    
    return timer;
}

/**
 * Deletes a monitor
 * 
 * @public
 */
PerformanceMonitor.deleteTimer = function(id) {
    if (u.isEmpty(id)) return;
    
    delete __perfHash[id];
}


module.exports = PerformanceMonitor;