All files index.js

88.18% Statements 97/110
63.41% Branches 52/82
95% Functions 19/20
87.96% Lines 95/108
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222  1x   1x 1x 1x 1x   1x                           7x 7x 1x     5x       7x   7x     7x     7x     7x       7x 7x 7x   7x       7x         7x 7x 7x 7x   7x         7x 7x                                             22x 22x   22x   22x 17x 17x   17x   17x 17x 17x 17x     22x   22x           22x   22x     17x         22x       22x 22x 22x   22x   22x 17x 17x   17x 17x 17x 17x   17x 4x 4x       22x 22x       4x 4x         6x   6x 5x 8x 10x 6x 6x   6x 7x 7x 7x   7x 7x 7x 7x 7x 7x 7x     6x 6x   6x   4x   4x 4x   4x           6x       15x 15x       7x 7x       7x 21x 7x    
 
const debug = require('debug')('feathers-profiler');
 
let app;
let options;
let pending = 0;
let cache = {};
 
const cacheEntry = {
  // sum all calls
  calledCount: 0,       // #times service called
  pendingTotal: 0,      // total #pending service calls at start of these calls
  pendingAvg: 0,        // average #pending service calls at start of a call
  // sum successful calls
  resolvedCount: 0,     // #times service call completed successfully
  nanoTotal: 0,         // total nano-secs (sec/1e9) spent on successful calls
  avgMs: 0,             // average milli-sec per successful call
  nanoMin: Infinity,    // shortest successful call
  nanoMax: -1,          // longest successful call
  resultItemsCount: 0   // #items read in find and get calls
};
 
export function profiler (options1 = {}) {
  options = Object.assign({}, {
    logger: { log: msg => console.log(msg) },
    logMsg: defaultLogMsg,
    stats: 'total',
    statsDetail: hook => `id:${typeof hook.id}, params:${JSON.stringify(hook.params)}`
  },
    options1);
 
  options.stats = options.stats || 'none';
 
  Iif (!['detail', 'total', 'none'].includes(options.stats)) {
    throw new Error('stats option invalid. (profiler)');
  }
  Iif (options.logger && typeof options.logger.log !== 'function') {
    throw new Error('Logger.log is not a function. (feathers-profiler)');
  }
  Iif (typeof options.logMsg !== 'function') {
    throw new Error('logMsg is not a function. (feathers-profiler)');
  }
  Iif (typeof options.statsDetail !== 'function') {
    throw new Error('statsDetail is not a function. (feathers-profiler)');
  }
 
  return function () {
    debug('Initializing logger-stats plugin');
    app = this;
 
    Iif (typeof app.hooks !== 'function') {
      throw new Error('feathers-hooks >= 1.6.0 is needed. (logger-stats)');
    }
 
    instrumentServices();
  };
}
 
export function defaultLogMsg (hook) {
  hook._log = hook._log || {};
  const elapsed = Math.round(hook._log.elapsed / 1e5) / 10;
  const header = `${timestamp()} ${hook._log.route}::${hook.method}`;
  const trailer = `(${hook.params.provider || 'server'}) ${elapsed} ms - ${pending} pending`;
 
  return `${header} ${trailer}` +
    (hook.error ? ` - FAILED ${(hook.original || {}).type} ${hook.error.message || ''}` : '');
}
 
function instrumentServices () {
  debug('Creating app.hooks');
  app.hooks({
    before: { all: timeStart },
    after: { all: timeEnd },
    error: {
      all: (hook) => {
        hook._log = hook._log || { route: '', key: '', hrtime: [0, 0], elapsed: 0 };
 
        if (hook._log.hrtime !== 0) {
          const diff = process.hrtime(hook._log.hrtime || [0, 0]);
          hook._log.elapsed = (diff[0] * 1e9) + diff[1];
        }
 
        pending += -1;
 
        if (options.logger) {
          options.logger.log(options.logMsg(hook));
        }
      }
    }
  });
}
 
export function timeStart (hook) {
  const route = hook.path; // feathers-hooks v1.7.0 required
  let key = '';
 
  debug(`timeStart ${route} ${hook.method} ${hook.params.provider}`);
 
  if (options.stats !== 'none') {
    cache[route] = cache[route] || {};
    cache[route][hook.method] = cache[route][hook.method] || {};
 
    key = getKey(options);
 
    const routeMethod = cache[route][hook.method];
    routeMethod[key] = routeMethod[key] || Object.assign({}, cacheEntry);
    routeMethod[key].calledCount += 1;
    routeMethod[key].pendingTotal += pending;
  }
 
  pending += 1;
 
  hook._log = {
    route,
    key,
    hrtime: [0, 0],
    elapsed: 0
  };
  hook._log.hrtime = process.hrtime(); // V8 bug: breaks if inside the above object literal
 
  return hook;
 
  function getKey (options) { // to reduce complexity for code climate
    return options.stats === 'detail' ? (options.statsDetail(hook) || '_misc') : '_total';
  }
}
 
export function timeEnd (hook) {
  Iif (!hook._log || !hook._log.hrtime) {
    return; // ignore RethinkDB change-feed
  }
 
  const diff = process.hrtime(hook._log.hrtime);
  pending += -1;
  debug(`timeEnd ${hook._log.route} ${hook.method} ${hook.params.provider}`);
 
  hook._log.elapsed = (diff[0] * 1e9) + diff[1];
 
  if (options.stats !== 'none') {
    const entry = cache[hook._log.route][hook.method][hook._log.key];
    const nano = hook._log.elapsed;
 
    entry.resolvedCount += 1;
    entry.nanoTotal += nano;
    entry.nanoMin = Math.min(entry.nanoMin, nano);
    entry.nanoMax = Math.max(entry.nanoMax, nano);
 
    if (hook.method === 'find' || hook.method === 'get') {
      const items = getItems(hook);
      entry.resultItemsCount += Array.isArray(items) ? items.length : 1;
    }
  }
 
  Eif (options.logger) {
    options.logger.log(options.logMsg(hook));
  }
 
  function getItems (hook) { // to reduce complexity for code climate
    const result = hook.result;
    return result ? result.data || result : result;
  }
}
 
export function getProfile () {
  debug('Get timings');
 
  if (options.stats !== 'none') {
    Object.keys(cache).forEach(route => {
      Object.keys(cache[route]).forEach(method => {
        if (options.stats === 'detail') {
          const total = Object.assign({}, cacheEntry);
          const rM = cache[route][method];
 
          Object.keys(rM).forEach(key => {
            const rMK = rM[key];
            rMK.avgMs = !rMK.resolvedCount ? 0 : rMK.nanoTotal / rMK.resolvedCount / 1e6;
            rMK.pendingAvg = !rMK.resolvedCount ? 0 : rMK.pendingTotal / rMK.resolvedCount;
 
            total.calledCount += rMK.calledCount;
            total.resolvedCount += rMK.resolvedCount;
            total.nanoTotal += rMK.nanoTotal;
            total.nanoMin = Math.min(total.nanoMin, rMK.nanoMin);
            total.nanoMax = Math.max(total.nanoMax, rMK.nanoMax);
            total.resultItemsCount += rMK.resultItemsCount;
            total.pendingTotal += rMK.pendingTotal;
          });
 
          total.avgMs = !total.resolvedCount ? 0 : total.nanoTotal / total.resolvedCount / 1e6;
          total.pendingAvg = !total.calledCount ? 0 : total.pendingTotal / total.calledCount;
 
          cache[route][method]._total = total;
        } else {
          const total2 = cache[route][method]._total;
 
          total2.avgMs = !total2.resolvedCount ? 0 : total2.nanoTotal / total2.resolvedCount / 1e6;
          total2.pendingAvg = !total2.calledCount ? 0 : total2.pendingTotal / total2.calledCount;
 
          cache[route][method]._total = total2;
        }
      });
    });
  }
 
  return cache;
}
 
export function getPending () {
  debug('getPending', pending);
  return pending;
}
 
export function clearProfile () {
  debug('Clearing cache');
  cache = {};
}
 
export function timestamp () {
  const date = new Date();
  const last2 = (numb) => `0${numb}`.slice(-2);
  return `${last2(date.getHours())}:${last2(date.getMinutes())}:${last2(date.getSeconds())}`;
}