All files / lib/internal/perf performance.js

98.94% Statements 187/189
85.71% Branches 12/14
87.5% Functions 7/8
98.94% Lines 187/189

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 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 19028x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x     28x 28x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 28x 28x 1x 1x 1x 1x 1x 1x 1x 28x 24x 24x 5x 5x 23x 23x 24x 28x 15x 15x 2x 2x 15x 15x 28x 5x 5x 5x 28x 32x 32x 32x 32x 32x 32x 28x 21x 21x 21x 21x 21x 21x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 5467x 5467x 5467x 5467x 5467x 5467x 5467x 28x 28x 28x 28x 28x  
'use strict';
 
const {
  ObjectDefineProperty,
  ObjectDefineProperties,
  ObjectSetPrototypeOf,
} = primordials;
 
const {
  codes: {
    ERR_ILLEGAL_CONSTRUCTOR,
  }
} = require('internal/errors');
 
const {
  EventTarget,
} = require('internal/event_target');
 
const { now } = require('internal/perf/utils');
 
const {
  mark,
  measure,
  clearMarkTimings,
} = require('internal/perf/usertiming');
const {
  clearEntriesFromBuffer,
  filterBufferMapByNameAndType,
} = require('internal/perf/observe');
 
const eventLoopUtilization = require('internal/perf/event_loop_utilization');
const nodeTiming = require('internal/perf/nodetiming');
const timerify = require('internal/perf/timerify');
const { customInspectSymbol: kInspect } = require('internal/util');
const { inspect } = require('util');
 
const {
  getTimeOriginTimestamp
} = internalBinding('performance');
 
class Performance extends EventTarget {
  constructor() {
    throw new ERR_ILLEGAL_CONSTRUCTOR();
  }
 
  [kInspect](depth, options) {
    if (depth < 0) return this;
 
    const opts = {
      ...options,
      depth: options.depth == null ? null : options.depth - 1
    };
 
    return `Performance ${inspect({
      nodeTiming: this.nodeTiming,
      timeOrigin: this.timeOrigin,
    }, opts)}`;
  }
}
 
function toJSON() {
  return {
    nodeTiming: this.nodeTiming,
    timeOrigin: this.timeOrigin,
    eventLoopUtilization: this.eventLoopUtilization()
  };
}
 
function clearMarks(name) {
  if (name !== undefined) {
    name = `${name}`;
  }
  clearMarkTimings(name);
  clearEntriesFromBuffer('mark', name);
}
 
function clearMeasures(name) {
  if (name !== undefined) {
    name = `${name}`;
  }
  clearEntriesFromBuffer('measure', name);
}
 
function getEntries() {
  return filterBufferMapByNameAndType();
}
 
function getEntriesByName(name) {
  if (name !== undefined) {
    name = `${name}`;
  }
  return filterBufferMapByNameAndType(name, undefined);
}
 
function getEntriesByType(type) {
  if (type !== undefined) {
    type = `${type}`;
  }
  return filterBufferMapByNameAndType(undefined, type);
}
 
class InternalPerformance extends EventTarget {}
InternalPerformance.prototype.constructor = Performance.prototype.constructor;
ObjectSetPrototypeOf(InternalPerformance.prototype, Performance.prototype);
 
ObjectDefineProperties(Performance.prototype, {
  clearMarks: {
    configurable: true,
    enumerable: false,
    value: clearMarks,
  },
  clearMeasures: {
    configurable: true,
    enumerable: false,
    value: clearMeasures,
  },
  eventLoopUtilization: {
    configurable: true,
    enumerable: false,
    value: eventLoopUtilization,
  },
  getEntries: {
    configurable: true,
    enumerable: false,
    value: getEntries,
  },
  getEntriesByName: {
    configurable: true,
    enumerable: false,
    value: getEntriesByName,
  },
  getEntriesByType: {
    configurable: true,
    enumerable: false,
    value: getEntriesByType,
  },
  mark: {
    configurable: true,
    enumerable: false,
    value: mark,
  },
  measure: {
    configurable: true,
    enumerable: false,
    value: measure,
  },
  nodeTiming: {
    configurable: true,
    enumerable: false,
    value: nodeTiming,
  },
  now: {
    configurable: true,
    enumerable: false,
    value: now,
  },
  timerify: {
    configurable: true,
    enumerable: false,
    value: timerify,
  },
  // This would be updated during pre-execution in case
  // the process is launched from a snapshot.
  // TODO(joyeecheung): we may want to warn about access to
  // this during snapshot building.
  timeOrigin: {
    configurable: true,
    enumerable: true,
    value: getTimeOriginTimestamp(),
  },
  toJSON: {
    configurable: true,
    enumerable: true,
    value: toJSON,
  }
});
 
function refreshTimeOrigin() {
  ObjectDefineProperty(Performance.prototype, 'timeOrigin', {
    configurable: true,
    enumerable: true,
    value: getTimeOriginTimestamp(),
  });
}
 
module.exports = {
  InternalPerformance,
  refreshTimeOrigin
};