All files / lib/internal/perf performance.js

94.26% Statements 230/244
94.28% Branches 33/35
100% Functions 16/16
94.26% Lines 230/244

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 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 24525x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 1x 25x 25x                         25x 25x 26x 26x 5x 5x 23x 23x 26x 25x 25x 17x 17x 2x 2x 15x 17x 25x 25x 10x 10x     8x 10x 25x 25x 14x 14x 14x 25x 25x 39x 39x 2x 2x 35x 35x 39x 25x 25x 43x 43x 2x 2x 39x 39x 43x 25x 25x 216x 216x 1x 1x 213x 216x 25x 25x 97x 97x 1x 1x 94x 97x 25x 25x 62935x 62935x 62935x 25x 25x 11x 11x 1x 1x 8x 11x 25x 25x 10x 10x 10x 25x 25x 4x 4x 4x 4x 4x 4x 4x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 740x 740x 740x 740x 740x 740x 25x 25x 25x 7x 7x 7x 7x 7x 7x 25x 25x 25x 25x 25x 25x  
'use strict';
 
const {
  ObjectDefineProperties,
  ReflectConstruct,
  Symbol,
  SymbolToStringTag,
} = primordials;
 
const {
  codes: {
    ERR_ILLEGAL_CONSTRUCTOR,
    ERR_MISSING_ARGS
  }
} = require('internal/errors');
 
const {
  EventTarget,
  Event,
  kTrustEvent,
  initEventTarget,
  defineEventHandler,
} = require('internal/event_target');
 
const { now } = require('internal/perf/utils');
 
const { markResourceTiming } = require('internal/perf/resource_timing');
 
const {
  mark,
  measure,
  clearMarkTimings,
} = require('internal/perf/usertiming');
const {
  clearEntriesFromBuffer,
  filterBufferMapByNameAndType,
  setResourceTimingBufferSize,
  setDispatchBufferFull,
} = 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, kEnumerableProperty, kEmptyObject } = require('internal/util');
const { inspect } = require('util');
const { validateInternalField } = require('internal/validators');
 
const {
  getTimeOriginTimestamp
} = internalBinding('performance');
 
const kPerformanceBrand = Symbol('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)}`;
  }
 
  clearMarks(name = undefined) {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    if (name !== undefined) {
      name = `${name}`;
    }
    clearMarkTimings(name);
    clearEntriesFromBuffer('mark', name);
  }
 
  clearMeasures(name = undefined) {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    if (name !== undefined) {
      name = `${name}`;
    }
    clearEntriesFromBuffer('measure', name);
  }
 
  clearResourceTimings(name = undefined) {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    if (name !== undefined) {
      name = `${name}`;
    }
    clearEntriesFromBuffer('resource', name);
  }
 
  getEntries() {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    return filterBufferMapByNameAndType();
  }
 
  getEntriesByName(name) {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    if (arguments.length === 0) {
      throw new ERR_MISSING_ARGS('name');
    }
    name = `${name}`;
    return filterBufferMapByNameAndType(name, undefined);
  }
 
  getEntriesByType(type) {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    if (arguments.length === 0) {
      throw new ERR_MISSING_ARGS('type');
    }
    type = `${type}`;
    return filterBufferMapByNameAndType(undefined, type);
  }
 
  mark(name, options = kEmptyObject) {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    if (arguments.length === 0) {
      throw new ERR_MISSING_ARGS('name');
    }
    return mark(name, options);
  }
 
  measure(name, startOrMeasureOptions = kEmptyObject, endMark = undefined) {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    if (arguments.length === 0) {
      throw new ERR_MISSING_ARGS('name');
    }
    return measure(name, startOrMeasureOptions, endMark);
  }
 
  now() {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    return now();
  }
 
  setResourceTimingBufferSize(maxSize) {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    if (arguments.length === 0) {
      throw new ERR_MISSING_ARGS('maxSize');
    }
    return setResourceTimingBufferSize(maxSize);
  }
 
  get timeOrigin() {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    return getTimeOriginTimestamp();
  }
 
  toJSON() {
    validateInternalField(this, kPerformanceBrand, 'Performance');
    return {
      nodeTiming: this.nodeTiming,
      timeOrigin: this.timeOrigin,
      eventLoopUtilization: this.eventLoopUtilization(),
    };
  }
}
 
ObjectDefineProperties(Performance.prototype, {
  clearMarks: kEnumerableProperty,
  clearMeasures: kEnumerableProperty,
  clearResourceTimings: kEnumerableProperty,
  getEntries: kEnumerableProperty,
  getEntriesByName: kEnumerableProperty,
  getEntriesByType: kEnumerableProperty,
  mark: kEnumerableProperty,
  measure: kEnumerableProperty,
  now: kEnumerableProperty,
  timeOrigin: kEnumerableProperty,
  toJSON: kEnumerableProperty,
  setResourceTimingBufferSize: kEnumerableProperty,
  [SymbolToStringTag]: {
    __proto__: null,
    writable: false,
    enumerable: false,
    configurable: true,
    value: 'Performance',
  },
 
  // Node.js specific extensions.
  eventLoopUtilization: {
    __proto__: null,
    configurable: true,
    // Node.js specific extensions.
    enumerable: false,
    writable: true,
    value: eventLoopUtilization,
  },
  nodeTiming: {
    __proto__: null,
    configurable: true,
    // Node.js specific extensions.
    enumerable: false,
    writable: true,
    value: nodeTiming,
  },
  // In the browser, this function is not public.  However, it must be used inside fetch
  // which is a Node.js dependency, not a internal module
  markResourceTiming: {
    __proto__: null,
    configurable: true,
    // Node.js specific extensions.
    enumerable: false,
    writable: true,
    value: markResourceTiming,
  },
  timerify: {
    __proto__: null,
    configurable: true,
    // Node.js specific extensions.
    enumerable: false,
    writable: true,
    value: timerify,
  },
});
defineEventHandler(Performance.prototype, 'resourcetimingbufferfull');
 
function createPerformance() {
  return ReflectConstruct(function Performance() {
    initEventTarget(this);
    this[kPerformanceBrand] = true;
  }, [], Performance);
}
 
const performance = createPerformance();
 
function dispatchBufferFull(type) {
  const event = new Event(type, {
    [kTrustEvent]: true
  });
  performance.dispatchEvent(event);
}
setDispatchBufferFull(dispatchBufferFull);
 
module.exports = {
  Performance,
  performance,
};