All files / lib/internal/perf timerify.js

100% Statements 126/126
100% Branches 17/17
100% Functions 3/3
100% Lines 126/126

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 12722x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 22x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 10x 10x 10x 10x 10x 10x 26x 8x 8x 8x 8x 25x 25x 25x 25x 25x 11x 11x 11x 11x 11x 11x 11x 11x 11x 13x 13x 25x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 26x 22x 22x  
'use strict';
 
const {
  FunctionPrototypeBind,
  ObjectDefineProperties,
  MathCeil,
  ReflectApply,
  ReflectConstruct,
  Symbol,
} = primordials;
 
const { InternalPerformanceEntry } = require('internal/perf/performance_entry');
const { now } = require('internal/perf/utils');
 
const {
  validateFunction,
  validateObject,
} = require('internal/validators');
 
const {
  isHistogram
} = require('internal/histogram');
 
const {
  isConstructor,
} = internalBinding('util');
 
const {
  codes: {
    ERR_INVALID_ARG_TYPE,
  },
} = require('internal/errors');
 
const {
  enqueue,
} = require('internal/perf/observe');
 
const kTimerified = Symbol('kTimerified');
 
function processComplete(name, start, args, histogram) {
  const duration = now() - start;
  if (histogram !== undefined)
    histogram.record(MathCeil(duration * 1e6));
  const entry =
    new InternalPerformanceEntry(
      name,
      'function',
      start,
      duration,
      args);
 
  for (let n = 0; n < args.length; n++)
    entry[n] = args[n];
 
  enqueue(entry);
}
 
function timerify(fn, options = {}) {
  validateFunction(fn, 'fn');
 
  validateObject(options, 'options');
  const {
    histogram
  } = options;
 
  if (histogram !== undefined &&
      (!isHistogram(histogram) || typeof histogram.record !== 'function')) {
    throw new ERR_INVALID_ARG_TYPE(
      'options.histogram',
      'RecordableHistogram',
      histogram);
  }
 
  if (fn[kTimerified]) return fn[kTimerified];
 
  const constructor = isConstructor(fn);
 
  function timerified(...args) {
    const start = now();
    const result = constructor ?
      ReflectConstruct(fn, args, fn) :
      ReflectApply(fn, this, args);
    if (!constructor && typeof result?.finally === 'function') {
      return result.finally(
        FunctionPrototypeBind(
          processComplete,
          result,
          fn.name,
          start,
          args,
          histogram));
    }
    processComplete(fn.name, start, args, histogram);
    return result;
  }
 
  ObjectDefineProperties(timerified, {
    [kTimerified]: {
      configurable: false,
      enumerable: false,
      value: timerified,
    },
    length: {
      configurable: false,
      enumerable: true,
      value: fn.length,
    },
    name: {
      configurable: false,
      enumerable: true,
      value: `timerified ${fn.name}`
    }
  });
 
  ObjectDefineProperties(fn, {
    [kTimerified]: {
      configurable: false,
      enumerable: false,
      value: timerified,
    }
  });
 
  return timerified;
}
 
module.exports = timerify;