Skip to content

성능 측정 API

[안정됨: 2 - 안정됨]

안정됨: 2 안정성: 2 - 안정됨

소스 코드: lib/perf_hooks.js

이 모듈은 W3C 웹 성능 API의 하위 집합과 Node.js 특정 성능 측정을 위한 추가 API를 구현합니다.

Node.js는 다음 웹 성능 API를 지원합니다.

js
import { performance, PerformanceObserver } from 'node:perf_hooks';

const obs = new PerformanceObserver((items) => {
  console.log(items.getEntries()[0].duration);
  performance.clearMarks();
});
obs.observe({ type: 'measure' });
performance.measure('Start to Now');

performance.mark('A');
doSomeLongRunningProcess(() => {
  performance.measure('A to Now', 'A');

  performance.mark('B');
  performance.measure('A to B', 'A', 'B');
});
js
const { PerformanceObserver, performance } = require('node:perf_hooks');

const obs = new PerformanceObserver((items) => {
  console.log(items.getEntries()[0].duration);
});
obs.observe({ type: 'measure' });
performance.measure('Start to Now');

performance.mark('A');
(async function doSomeLongRunningProcess() {
  await new Promise((r) => setTimeout(r, 5000));
  performance.measure('A to Now', 'A');

  performance.mark('B');
  performance.measure('A to B', 'A', 'B');
})();

perf_hooks.performance

추가됨: v8.5.0

현재 Node.js 인스턴스에서 성능 메트릭을 수집하는 데 사용할 수 있는 객체입니다. 브라우저의 window.performance와 유사합니다.

performance.clearMarks([name])

[기록]

버전변경 사항
v19.0.0이 메서드는 performance 객체를 수신자로 하여 호출해야 합니다.
v8.5.0추가됨: v8.5.0

name이 제공되지 않으면 Performance Timeline에서 모든 PerformanceMark 객체를 제거합니다. name이 제공되면 명명된 마크만 제거합니다.

performance.clearMeasures([name])

[기록]

버전변경 사항
v19.0.0이 메서드는 performance 객체를 수신자로 하여 호출해야 합니다.
v16.7.0추가됨: v16.7.0

name이 제공되지 않으면 Performance Timeline에서 모든 PerformanceMeasure 객체를 제거합니다. name이 제공되면 명명된 측정값만 제거합니다.

performance.clearResourceTimings([name])

[기록]

버전변경 사항
v19.0.0이 메서드는 performance 객체를 수신자로 하여 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

name이 제공되지 않으면 Resource Timeline에서 모든 PerformanceResourceTiming 객체를 제거합니다. name이 제공되면 명명된 리소스만 제거합니다.

performance.eventLoopUtilization([utilization1[, utilization2]])

추가됨: v14.10.0, v12.19.0

  • utilization1 <Object> eventLoopUtilization()에 대한 이전 호출의 결과입니다.
  • utilization2 <Object> utilization1 이전의 eventLoopUtilization()에 대한 이전 호출의 결과입니다.
  • 반환값: <Object>

eventLoopUtilization() 메서드는 이벤트 루프가 고해상도 밀리초 타이머로 유휴 및 활성 상태였던 누적 시간을 포함하는 객체를 반환합니다. utilization 값은 계산된 이벤트 루프 사용률(ELU)입니다.

메인 스레드에서 부트스트래핑이 아직 완료되지 않은 경우 속성 값은 0입니다. Worker 스레드에서는 부트스트랩이 이벤트 루프 내에서 발생하므로 ELU를 즉시 사용할 수 있습니다.

utilization1utilization2는 모두 선택적 매개변수입니다.

utilization1이 전달되면 현재 호출의 activeidle 시간 간의 델타와 해당 utilization 값이 계산되어 반환됩니다(process.hrtime()과 유사).

utilization1utilization2가 모두 전달되면 두 인수 간의 델타가 계산됩니다. 이는 process.hrtime()과 달리 ELU 계산이 단일 빼기보다 더 복잡하기 때문에 편리한 옵션입니다.

ELU는 CPU 사용률과 유사하지만 CPU 사용량이 아닌 이벤트 루프 통계만 측정합니다. 이는 이벤트 루프가 이벤트 루프의 이벤트 제공자(예: epoll_wait) 외부에서 보낸 시간의 백분율을 나타냅니다. 다른 CPU 유휴 시간은 고려되지 않습니다. 다음은 대부분 유휴 상태인 프로세스가 높은 ELU를 갖는 방법의 예입니다.

js
import { eventLoopUtilization } from 'node:perf_hooks';
import { spawnSync } from 'node:child_process';

setImmediate(() => {
  const elu = eventLoopUtilization();
  spawnSync('sleep', ['5']);
  console.log(eventLoopUtilization(elu).utilization);
});
js
'use strict';
const { eventLoopUtilization } = require('node:perf_hooks').performance;
const { spawnSync } = require('node:child_process');

setImmediate(() => {
  const elu = eventLoopUtilization();
  spawnSync('sleep', ['5']);
  console.log(eventLoopUtilization(elu).utilization);
});

이 스크립트를 실행하는 동안 CPU가 대부분 유휴 상태이지만 utilization 값은 1입니다. 이는 child_process.spawnSync()에 대한 호출이 이벤트 루프가 진행되지 않도록 차단하기 때문입니다.

eventLoopUtilization()에 대한 이전 호출의 결과 대신 사용자 정의 객체를 전달하면 정의되지 않은 동작이 발생합니다. 반환 값은 이벤트 루프의 올바른 상태를 반영하도록 보장되지 않습니다.

performance.getEntries()

[기록]

버전변경 사항
v19.0.0이 메서드는 performance 객체를 수신자로 호출해야 합니다.
v16.7.0추가됨: v16.7.0

performanceEntry.startTime을 기준으로 시간순으로 정렬된 PerformanceEntry 객체 목록을 반환합니다. 특정 유형의 성능 항목 또는 특정 이름을 가진 성능 항목에만 관심이 있는 경우 performance.getEntriesByType()performance.getEntriesByName()을 참조하십시오.

performance.getEntriesByName(name[, type])

[기록]

버전변경 사항
v19.0.0이 메서드는 performance 객체를 수신자로 호출해야 합니다.
v16.7.0추가됨: v16.7.0

performanceEntry.namename과 같고 선택적으로 performanceEntry.entryTypetype과 같은 PerformanceEntry 객체 목록을 performanceEntry.startTime을 기준으로 시간순으로 정렬하여 반환합니다.

performance.getEntriesByType(type)

[기록]

버전변경 사항
v19.0.0이 메서드는 performance 객체를 수신자로 호출해야 합니다.
v16.7.0추가됨: v16.7.0

performanceEntry.entryTypetype과 같은 PerformanceEntry 객체 목록을 performanceEntry.startTime을 기준으로 시간순으로 정렬하여 반환합니다.

performance.mark(name[, options])

[기록]

버전변경 사항
v19.0.0이 메서드는 performance 객체를 수신자로 호출해야 합니다. name 인수는 더 이상 선택 사항이 아닙니다.
v16.0.0User Timing Level 3 사양을 준수하도록 업데이트되었습니다.
v8.5.0추가됨: v8.5.0
  • name <string>
  • options <Object>
    • detail <any> 마크에 포함할 추가 선택적 세부 정보입니다.
    • startTime <number> 마크 시간으로 사용할 선택적 타임스탬프입니다. 기본값: performance.now().

Performance Timeline에 새로운 PerformanceMark 항목을 만듭니다. PerformanceMarkperformanceEntry.entryType이 항상 'mark'이고 performanceEntry.duration이 항상 0PerformanceEntry의 서브클래스입니다. 성능 마크는 Performance Timeline에서 특정 중요한 순간을 표시하는 데 사용됩니다.

생성된 PerformanceMark 항목은 글로벌 Performance Timeline에 배치되며 performance.getEntries, performance.getEntriesByNameperformance.getEntriesByType로 쿼리할 수 있습니다. 관찰이 수행되면 performance.clearMarks를 사용하여 글로벌 Performance Timeline에서 항목을 수동으로 지워야 합니다.

performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, global, cacheMode, bodyInfo, responseStatus[, deliveryType])

[기록]

버전변경 사항
v22.2.0bodyInfo, responseStatus, 및 deliveryType 인수가 추가되었습니다.
v18.2.0, v16.17.0다음 버전에서 추가됨: v18.2.0, v16.17.0

이 속성은 Node.js에서 확장한 것입니다. 웹 브라우저에서는 사용할 수 없습니다.

리소스 타임라인에 새로운 PerformanceResourceTiming 항목을 만듭니다. PerformanceResourceTimingperformanceEntry.entryType이 항상 'resource'PerformanceEntry의 하위 클래스입니다. 성능 리소스는 리소스 타임라인에서 순간을 표시하는 데 사용됩니다.

생성된 PerformanceMark 항목은 전역 리소스 타임라인에 배치되며 performance.getEntries, performance.getEntriesByNameperformance.getEntriesByType를 사용하여 쿼리할 수 있습니다. 관찰이 수행되면 performance.clearResourceTimings를 사용하여 전역 성능 타임라인에서 항목을 수동으로 지워야 합니다.

performance.measure(name[, startMarkOrOptions[, endMark]])

[기록]

버전변경 사항
v19.0.0이 메서드는 performance 객체를 수신자로 호출해야 합니다.
v16.0.0User Timing Level 3 사양을 준수하도록 업데이트되었습니다.
v13.13.0, v12.16.3startMarkendMark 매개변수를 선택 사항으로 만듭니다.
v8.5.0다음 버전에서 추가됨: v8.5.0
  • name <string>

  • startMarkOrOptions <string> | <Object> 선택 사항.

    • detail <any> 측정에 포함할 추가 선택적 세부 정보입니다.
    • duration <number> 시작 시간과 종료 시간 사이의 기간입니다.
    • end <number> | <string> 종료 시간으로 사용할 타임스탬프 또는 이전에 기록된 마크를 식별하는 문자열입니다.
    • start <number> | <string> 시작 시간으로 사용할 타임스탬프 또는 이전에 기록된 마크를 식별하는 문자열입니다.
  • endMark <string> 선택 사항. startMarkOrOptions<Object>인 경우 생략해야 합니다.

Performance Timeline에 새 PerformanceMeasure 항목을 만듭니다. PerformanceMeasureperformanceEntry.entryType이 항상 'measure'이고 performanceEntry.durationstartMarkendMark 이후 경과된 밀리초 수를 측정하는 PerformanceEntry의 하위 클래스입니다.

startMark 인수는 Performance Timeline에 있는 기존 PerformanceMark를 식별하거나 PerformanceNodeTiming 클래스에서 제공하는 타임스탬프 속성을 식별할 수 있습니다. 지정된 startMark가 존재하지 않으면 오류가 발생합니다.

선택적 endMark 인수는 Performance Timeline에 있는 기존 PerformanceMark 또는 PerformanceNodeTiming 클래스에서 제공하는 타임스탬프 속성을 식별해야 합니다. 매개변수가 전달되지 않으면 endMarkperformance.now()가 되고, 그렇지 않으면 지정된 endMark가 존재하지 않으면 오류가 발생합니다.

생성된 PerformanceMeasure 항목은 전역 Performance Timeline에 배치되며 performance.getEntries, performance.getEntriesByNameperformance.getEntriesByType로 쿼리할 수 있습니다. 관찰이 수행되면 performance.clearMeasures를 사용하여 전역 Performance Timeline에서 항목을 수동으로 지워야 합니다.

performance.nodeTiming

Added in: v8.5.0

이 속성은 Node.js에 의해 확장되었습니다. 웹 브라우저에서는 사용할 수 없습니다.

특정 Node.js 운영 단계에 대한 성능 메트릭을 제공하는 PerformanceNodeTiming 클래스의 인스턴스입니다.

performance.now()

[History]

VersionChanges
v19.0.0이 메서드는 performance 객체를 수신자로 사용하여 호출해야 합니다.
v8.5.0Added in: v8.5.0

현재 고해상도 밀리초 타임스탬프를 반환합니다. 여기서 0은 현재 node 프로세스의 시작을 나타냅니다.

performance.setResourceTimingBufferSize(maxSize)

[History]

VersionChanges
v19.0.0이 메서드는 performance 객체를 수신자로 사용하여 호출해야 합니다.
v18.8.0Added in: v18.8.0

전역 성능 리소스 타이밍 버퍼 크기를 지정된 "리소스" 유형 성능 항목 객체 수로 설정합니다.

기본적으로 최대 버퍼 크기는 250으로 설정됩니다.

performance.timeOrigin

Added in: v8.5.0

timeOrigin은 현재 node 프로세스가 시작된 고해상도 밀리초 타임스탬프를 Unix 시간으로 측정한 값을 지정합니다.

performance.timerify(fn[, options])

[History]

VersionChanges
v16.0.0히스토그램 옵션을 추가했습니다.
v16.0.0순수 JavaScript를 사용하여 비동기 함수를 시간 측정할 수 있도록 다시 구현했습니다.
v8.5.0Added in: v8.5.0
  • fn <Function>
  • options <Object>
    • histogram <RecordableHistogram> 나노초 단위로 런타임 기간을 기록할 perf_hooks.createHistogram()을 사용하여 생성된 히스토그램 객체입니다.

이 속성은 Node.js에 의해 확장되었습니다. 웹 브라우저에서는 사용할 수 없습니다.

래핑된 함수의 실행 시간을 측정하는 새로운 함수 내에 함수를 래핑합니다. 타이밍 세부 정보에 액세스하려면 PerformanceObserver'function' 이벤트 유형을 구독해야 합니다.

js
import { performance, PerformanceObserver } from 'node:perf_hooks';

function someFunction() {
  console.log('hello world');
}

const wrapped = performance.timerify(someFunction);

const obs = new PerformanceObserver((list) => {
  console.log(list.getEntries()[0].duration);

  performance.clearMarks();
  performance.clearMeasures();
  obs.disconnect();
});
obs.observe({ entryTypes: ['function'] });

// A performance timeline entry will be created
wrapped();
js
const {
  performance,
  PerformanceObserver,
} = require('node:perf_hooks');

function someFunction() {
  console.log('hello world');
}

const wrapped = performance.timerify(someFunction);

const obs = new PerformanceObserver((list) => {
  console.log(list.getEntries()[0].duration);

  performance.clearMarks();
  performance.clearMeasures();
  obs.disconnect();
});
obs.observe({ entryTypes: ['function'] });

// A performance timeline entry will be created
wrapped();

래핑된 함수가 프로미스를 반환하는 경우, finally 핸들러가 프로미스에 첨부되고 finally 핸들러가 호출되면 기간이 보고됩니다.

performance.toJSON()

[기록]

버전변경 사항
v19.0.0이 메서드는 performance 객체를 수신자로 사용하여 호출해야 합니다.
v16.1.0추가됨: v16.1.0

performance 객체의 JSON 표현인 객체입니다. 브라우저의 window.performance.toJSON과 유사합니다.

이벤트: 'resourcetimingbufferfull'

추가됨: v18.8.0

글로벌 성능 리소스 타이밍 버퍼가 가득 차면 'resourcetimingbufferfull' 이벤트가 발생합니다. 성능 타임라인 버퍼에 더 많은 항목을 추가할 수 있도록 이벤트 리스너에서 performance.setResourceTimingBufferSize()로 리소스 타이밍 버퍼 크기를 조정하거나 performance.clearResourceTimings()로 버퍼를 지웁니다.

클래스: PerformanceEntry

추가됨: v8.5.0

이 클래스의 생성자는 사용자에게 직접 노출되지 않습니다.

performanceEntry.duration

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceEntry 객체를 수신자로 사용하여 호출해야 합니다.
v8.5.0추가됨: v8.5.0

이 항목에 경과된 총 밀리초 수입니다. 이 값은 모든 성능 항목 유형에 대해 의미가 없을 수 있습니다.

performanceEntry.entryType

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceEntry 객체를 수신자로 사용하여 호출해야 합니다.
v8.5.0추가됨: v8.5.0

성능 항목의 유형입니다. 다음 중 하나일 수 있습니다.

  • 'dns' (Node.js 전용)
  • 'function' (Node.js 전용)
  • 'gc' (Node.js 전용)
  • 'http2' (Node.js 전용)
  • 'http' (Node.js 전용)
  • 'mark' (웹에서 사용 가능)
  • 'measure' (웹에서 사용 가능)
  • 'net' (Node.js 전용)
  • 'node' (Node.js 전용)
  • 'resource' (웹에서 사용 가능)

performanceEntry.name

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceEntry 객체를 수신자로 하여 호출해야 합니다.
v8.5.0추가됨: v8.5.0

성능 항목의 이름입니다.

performanceEntry.startTime

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceEntry 객체를 수신자로 하여 호출해야 합니다.
v8.5.0추가됨: v8.5.0

성능 항목의 시작 시간을 표시하는 고해상도 밀리초 타임스탬프입니다.

클래스: PerformanceMark

추가됨: v18.2.0, v16.17.0

Performance.mark() 메서드를 통해 생성된 마크를 노출합니다.

performanceMark.detail

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceMark 객체를 수신자로 하여 호출해야 합니다.
v16.0.0추가됨: v16.0.0

Performance.mark() 메서드로 생성 시 지정된 추가 정보입니다.

클래스: PerformanceMeasure

추가됨: v18.2.0, v16.17.0

Performance.measure() 메서드를 통해 생성된 측정을 노출합니다.

이 클래스의 생성자는 사용자에게 직접 노출되지 않습니다.

performanceMeasure.detail

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceMeasure 객체를 수신자로 하여 호출해야 합니다.
v16.0.0추가됨: v16.0.0

Performance.measure() 메서드로 생성 시 지정된 추가 정보입니다.

클래스: PerformanceNodeEntry

추가된 버전: v19.0.0

이 클래스는 Node.js에 의해 확장되었습니다. 웹 브라우저에서는 사용할 수 없습니다.

자세한 Node.js 타이밍 데이터를 제공합니다.

이 클래스의 생성자는 사용자에게 직접 노출되지 않습니다.

performanceNodeEntry.detail

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceNodeEntry 객체를 리시버로 사용하여 호출해야 합니다.
v16.0.0추가된 버전: v16.0.0

entryType에 특정한 추가 세부 정보.

performanceNodeEntry.flags

[기록]

버전변경 사항
v16.0.0런타임에서 더 이상 사용되지 않습니다. 이제 entryType이 'gc'인 경우 detail 속성으로 이동되었습니다.
v13.9.0, v12.17.0추가된 버전: v13.9.0, v12.17.0

[안정성: 0 - 더 이상 사용되지 않음]

안정성: 0 안정성: 0 - 더 이상 사용되지 않음: 대신 performanceNodeEntry.detail을 사용하세요.

performanceEntry.entryType'gc'와 같을 때, performance.flags 속성은 가비지 컬렉션 작업에 대한 추가 정보를 포함합니다. 값은 다음 중 하나일 수 있습니다.

  • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_NO
  • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED
  • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_FORCED
  • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING
  • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE
  • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY
  • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE

performanceNodeEntry.kind

[기록]

버전변경 사항
v16.0.0런타임에서 더 이상 사용되지 않습니다. 이제 entryType이 'gc'인 경우 detail 속성으로 이동되었습니다.
v8.5.0추가된 버전: v8.5.0

[안정성: 0 - 더 이상 사용되지 않음]

안정성: 0 안정성: 0 - 더 이상 사용되지 않음: 대신 performanceNodeEntry.detail을 사용하세요.

performanceEntry.entryType'gc'와 같을 때, performance.kind 속성은 발생한 가비지 컬렉션 작업의 유형을 식별합니다. 값은 다음 중 하나일 수 있습니다.

  • perf_hooks.constants.NODE_PERFORMANCE_GC_MAJOR
  • perf_hooks.constants.NODE_PERFORMANCE_GC_MINOR
  • perf_hooks.constants.NODE_PERFORMANCE_GC_INCREMENTAL
  • perf_hooks.constants.NODE_PERFORMANCE_GC_WEAKCB

가비지 컬렉션('gc') 세부 정보

performanceEntry.type'gc'와 같으면 performanceNodeEntry.detail 속성은 두 개의 속성을 가진 <Object>가 됩니다.

  • kind <number> 다음 중 하나:

    • perf_hooks.constants.NODE_PERFORMANCE_GC_MAJOR
    • perf_hooks.constants.NODE_PERFORMANCE_GC_MINOR
    • perf_hooks.constants.NODE_PERFORMANCE_GC_INCREMENTAL
    • perf_hooks.constants.NODE_PERFORMANCE_GC_WEAKCB
  • flags <number> 다음 중 하나:

    • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_NO
    • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED
    • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_FORCED
    • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING
    • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE
    • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY
    • perf_hooks.constants.NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE

HTTP ('http') 세부 정보

performanceEntry.type'http'와 같으면 performanceNodeEntry.detail 속성은 추가 정보가 포함된 <Object>가 됩니다.

performanceEntry.nameHttpClient와 같으면 detailreq, res 속성을 포함합니다. 그리고 req 속성은 method, url, headers를 포함하는 <Object>이고, res 속성은 statusCode, statusMessage, headers를 포함하는 <Object>입니다.

performanceEntry.nameHttpRequest와 같으면 detailreq, res 속성을 포함합니다. 그리고 req 속성은 method, url, headers를 포함하는 <Object>이고, res 속성은 statusCode, statusMessage, headers를 포함하는 <Object>입니다.

이는 추가 메모리 오버헤드를 초래할 수 있으며 기본적으로 프로덕션 환경에서 켜둔 채로 두지 말고 진단 목적으로만 사용해야 합니다.

HTTP/2 ('http2') 세부 정보

performanceEntry.type'http2'와 같으면, performanceNodeEntry.detail 속성은 추가적인 성능 정보를 담고 있는 <Object>가 됩니다.

performanceEntry.nameHttp2Stream과 같으면, detail은 다음 속성을 포함합니다:

  • bytesRead <number>Http2Stream에 대해 수신된 DATA 프레임 바이트 수.
  • bytesWritten <number>Http2Stream에 대해 전송된 DATA 프레임 바이트 수.
  • id <number> 연결된 Http2Stream의 식별자
  • timeToFirstByte <number> PerformanceEntry startTime과 첫 번째 DATA 프레임 수신 사이의 경과 시간(밀리초).
  • timeToFirstByteSent <number> PerformanceEntry startTime과 첫 번째 DATA 프레임 전송 사이의 경과 시간(밀리초).
  • timeToFirstHeader <number> PerformanceEntry startTime과 첫 번째 헤더 수신 사이의 경과 시간(밀리초).

performanceEntry.nameHttp2Session과 같으면, detail은 다음 속성을 포함합니다:

  • bytesRead <number>Http2Session에 대해 수신된 바이트 수.
  • bytesWritten <number>Http2Session에 대해 전송된 바이트 수.
  • framesReceived <number> Http2Session에 의해 수신된 HTTP/2 프레임 수.
  • framesSent <number> Http2Session에 의해 전송된 HTTP/2 프레임 수.
  • maxConcurrentStreams <number> Http2Session의 수명 동안 동시에 열려 있는 최대 스트림 수.
  • pingRTT <number> PING 프레임 전송 이후 해당 승인 수신까지의 경과 시간(밀리초). Http2Session에서 PING 프레임이 전송된 경우에만 존재합니다.
  • streamAverageDuration <number> 모든 Http2Stream 인스턴스의 평균 지속 시간 (밀리초).
  • streamCount <number> Http2Session에 의해 처리된 Http2Stream 인스턴스 수.
  • type <string> Http2Session의 유형을 식별하기 위해 'server' 또는 'client' 중 하나.

Timerify ('function') 세부 정보

performanceEntry.type'function'과 같으면 performanceNodeEntry.detail 속성은 시간이 지정된 함수에 대한 입력 인수를 나열하는 <Array>가 됩니다.

Net ('net') 세부 정보

performanceEntry.type'net'과 같으면 performanceNodeEntry.detail 속성은 추가 정보를 포함하는 <Object>가 됩니다.

performanceEntry.nameconnect와 같으면 detail에는 host, port 속성이 포함됩니다.

DNS ('dns') 세부 정보

performanceEntry.type'dns'와 같으면 performanceNodeEntry.detail 속성은 추가 정보를 포함하는 <Object>가 됩니다.

performanceEntry.namelookup과 같으면 detail에는 hostname, family, hints, verbatim, addresses 속성이 포함됩니다.

performanceEntry.namelookupService와 같으면 detail에는 host, port, hostname, service 속성이 포함됩니다.

performanceEntry.namequeryxxx 또는 getHostByAddr과 같으면 detail에는 host, ttl, result 속성이 포함됩니다. result의 값은 queryxxx 또는 getHostByAddr의 결과와 같습니다.

클래스: PerformanceNodeTiming

추가된 버전: v8.5.0

이 속성은 Node.js에 의한 확장입니다. 웹 브라우저에서는 사용할 수 없습니다.

Node.js 자체에 대한 타이밍 세부 정보를 제공합니다. 이 클래스의 생성자는 사용자에게 노출되지 않습니다.

performanceNodeTiming.bootstrapComplete

추가된 버전: v8.5.0

Node.js 프로세스가 부트스트랩을 완료한 시점의 고해상도 밀리초 타임스탬프입니다. 부트스트랩이 아직 완료되지 않은 경우 속성 값은 -1입니다.

performanceNodeTiming.environment

추가된 버전: v8.5.0

Node.js 환경이 초기화된 고해상도 밀리초 타임스탬프입니다.

performanceNodeTiming.idleTime

추가된 버전: v14.10.0, v12.19.0

이벤트 루프의 이벤트 공급자(예: epoll_wait) 내에서 이벤트 루프가 유휴 상태였던 시간의 고해상도 밀리초 타임스탬프입니다. 이는 CPU 사용량을 고려하지 않습니다. 이벤트 루프가 아직 시작되지 않은 경우(예: 메인 스크립트의 첫 번째 틱에서) 속성 값은 0입니다.

performanceNodeTiming.loopExit

추가된 버전: v8.5.0

Node.js 이벤트 루프가 종료된 고해상도 밀리초 타임스탬프입니다. 이벤트 루프가 아직 종료되지 않은 경우 속성 값은 -1입니다. 'exit' 이벤트의 핸들러에서만 -1이 아닌 값을 가질 수 있습니다.

performanceNodeTiming.loopStart

추가된 버전: v8.5.0

Node.js 이벤트 루프가 시작된 고해상도 밀리초 타임스탬프입니다. 이벤트 루프가 아직 시작되지 않은 경우(예: 메인 스크립트의 첫 번째 틱에서) 속성 값은 -1입니다.

performanceNodeTiming.nodeStart

추가된 버전: v8.5.0

Node.js 프로세스가 초기화된 고해상도 밀리초 타임스탬프입니다.

performanceNodeTiming.uvMetricsInfo

추가된 버전: v22.8.0, v20.18.0

  • 반환: <Object>
    • loopCount <number> 이벤트 루프 반복 횟수입니다.
    • events <number> 이벤트 핸들러에 의해 처리된 이벤트 수입니다.
    • eventsWaiting <number> 이벤트 공급자가 호출되었을 때 처리되기를 기다리는 이벤트 수입니다.

이것은 uv_metrics_info 함수에 대한 래퍼입니다. 현재 이벤트 루프 메트릭 세트를 반환합니다.

현재 루프 반복 중에 예약된 모든 작업이 완료되기 전에 메트릭을 수집하지 않도록 setImmediate를 사용하여 예약된 함수 내에서 이 속성을 사용하는 것이 좋습니다.

js
const { performance } = require('node:perf_hooks');

setImmediate(() => {
  console.log(performance.nodeTiming.uvMetricsInfo);
});
js
import { performance } from 'node:perf_hooks';

setImmediate(() => {
  console.log(performance.nodeTiming.uvMetricsInfo);
});

performanceNodeTiming.v8Start

Added in: v8.5.0

V8 플랫폼이 초기화된 고해상도 밀리초 타임스탬프입니다.

Class: PerformanceResourceTiming

Added in: v18.2.0, v16.17.0

애플리케이션 리소스 로드에 대한 자세한 네트워크 타이밍 데이터를 제공합니다.

이 클래스의 생성자는 사용자에게 직접 노출되지 않습니다.

performanceResourceTiming.workerStart

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 사용하여 호출해야 합니다.
v18.2.0, v16.17.0Added in: v18.2.0, v16.17.0

fetch 요청을 디스패치하기 바로 직전의 고해상도 밀리초 타임스탬프입니다. 리소스가 워커에 의해 가로채이지 않으면 속성은 항상 0을 반환합니다.

performanceResourceTiming.redirectStart

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 사용하여 호출해야 합니다.
v18.2.0, v16.17.0Added in: v18.2.0, v16.17.0

리디렉션을 시작하는 fetch의 시작 시간을 나타내는 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.redirectEnd

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 사용하여 호출해야 합니다.
v18.2.0, v16.17.0Added in: v18.2.0, v16.17.0

마지막 리디렉션의 응답의 마지막 바이트를 수신한 직후에 생성되는 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.fetchStart

[History]

VersionChanges
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 하여 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

Node.js가 리소스 가져오기를 시작하기 직전의 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.domainLookupStart

[History]

VersionChanges
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 하여 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

Node.js가 리소스에 대한 도메인 이름 조회를 시작하기 직전의 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.domainLookupEnd

[History]

VersionChanges
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 하여 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

Node.js가 리소스에 대한 도메인 이름 조회를 완료한 직후의 시간을 나타내는 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.connectStart

[History]

VersionChanges
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 하여 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

Node.js가 리소스를 검색하기 위해 서버에 대한 연결을 설정하기 직전의 시간을 나타내는 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.connectEnd

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 하여 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

Node.js가 리소스를 검색하기 위해 서버에 대한 연결 설정을 완료한 직후의 시간을 나타내는 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.secureConnectionStart

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 하여 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

Node.js가 현재 연결을 보호하기 위한 핸드셰이크 프로세스를 시작하기 직전의 시간을 나타내는 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.requestStart

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 하여 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

Node.js가 서버에서 응답의 첫 번째 바이트를 받기 직전의 시간을 나타내는 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.responseEnd

[기록]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 하여 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

Node.js가 리소스의 마지막 바이트를 받은 직후 또는 전송 연결이 닫히기 직전 중 더 빠른 시간을 나타내는 고해상도 밀리초 타임스탬프입니다.

performanceResourceTiming.transferSize

[내역]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

가져온 리소스의 크기(옥텟 단위)를 나타내는 숫자입니다. 크기에는 응답 헤더 필드와 응답 페이로드 본문이 포함됩니다.

performanceResourceTiming.encodedBodySize

[내역]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

적용된 콘텐츠 코딩을 제거하기 전 페치(HTTP 또는 캐시)에서 수신한 페이로드 본문의 크기(옥텟 단위)를 나타내는 숫자입니다.

performanceResourceTiming.decodedBodySize

[내역]

버전변경 사항
v19.0.0이 속성 getter는 PerformanceResourceTiming 객체를 수신자로 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

적용된 콘텐츠 코딩을 제거한 후 페치(HTTP 또는 캐시)에서 수신한 메시지 본문의 크기(옥텟 단위)를 나타내는 숫자입니다.

performanceResourceTiming.toJSON()

[내역]

버전변경 사항
v19.0.0이 메서드는 PerformanceResourceTiming 객체를 수신자로 호출해야 합니다.
v18.2.0, v16.17.0추가됨: v18.2.0, v16.17.0

PerformanceResourceTiming 객체의 JSON 표현인 object를 반환합니다.

클래스: PerformanceObserver

추가됨: v8.5.0

PerformanceObserver.supportedEntryTypes

추가됨: v16.0.0

지원되는 형식을 가져옵니다.

new PerformanceObserver(callback)

[기록]

버전변경 사항
v18.0.0callback 인수에 유효하지 않은 콜백을 전달하면 이제 ERR_INVALID_CALLBACK 대신 ERR_INVALID_ARG_TYPE 오류가 발생합니다.
v8.5.0추가됨: v8.5.0

PerformanceObserver 객체는 새 PerformanceEntry 인스턴스가 성능 타임라인에 추가될 때 알림을 제공합니다.

js
import { performance, PerformanceObserver } from 'node:perf_hooks';

const obs = new PerformanceObserver((list, observer) => {
  console.log(list.getEntries());

  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ entryTypes: ['mark'], buffered: true });

performance.mark('test');
js
const {
  performance,
  PerformanceObserver,
} = require('node:perf_hooks');

const obs = new PerformanceObserver((list, observer) => {
  console.log(list.getEntries());

  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ entryTypes: ['mark'], buffered: true });

performance.mark('test');

PerformanceObserver 인스턴스는 자체적인 추가 성능 오버헤드를 발생시키므로 인스턴스를 알림에 무기한 구독 상태로 두어서는 안 됩니다. 사용자는 더 이상 필요하지 않으면 옵저버 연결을 끊어야 합니다.

callback은 새 PerformanceEntry 인스턴스에 대해 PerformanceObserver가 알림을 받을 때 호출됩니다. 콜백은 PerformanceObserverEntryList 인스턴스와 PerformanceObserver에 대한 참조를 받습니다.

performanceObserver.disconnect()

추가됨: v8.5.0

PerformanceObserver 인스턴스를 모든 알림에서 연결 해제합니다.

performanceObserver.observe(options)

[기록]

버전변경 사항
v16.7.0Performance Timeline Level 2를 준수하도록 업데이트되었습니다. buffered 옵션이 다시 추가되었습니다.
v16.0.0User Timing Level 3을 준수하도록 업데이트되었습니다. buffered 옵션이 제거되었습니다.
v8.5.0추가됨: v8.5.0
  • options <Object>
    • type <string> 단일 <PerformanceEntry> 유형. entryTypes가 이미 지정된 경우 제공하지 않아야 합니다.
    • entryTypes <string[]> 옵저버가 관심 있는 <PerformanceEntry> 인스턴스의 유형을 식별하는 문자열 배열입니다. 제공되지 않으면 오류가 발생합니다.
    • buffered <boolean> true인 경우 옵저버 콜백은 전역 PerformanceEntry 버퍼링된 항목 목록과 함께 호출됩니다. false인 경우 해당 시점 이후에 생성된 PerformanceEntry만 옵저버 콜백으로 전송됩니다. 기본값: false.

options.entryTypes 또는 options.type으로 식별된 새로운 <PerformanceEntry> 인스턴스에 대한 알림을 <PerformanceObserver> 인스턴스가 구독합니다.

js
import { performance, PerformanceObserver } from 'node:perf_hooks';

const obs = new PerformanceObserver((list, observer) => {
  // 비동기적으로 한 번 호출됩니다. `list`에는 세 개의 항목이 있습니다.
});
obs.observe({ type: 'mark' });

for (let n = 0; n < 3; n++)
  performance.mark(`test${n}`);
js
const {
  performance,
  PerformanceObserver,
} = require('node:perf_hooks');

const obs = new PerformanceObserver((list, observer) => {
  // 비동기적으로 한 번 호출됩니다. `list`에는 세 개의 항목이 있습니다.
});
obs.observe({ type: 'mark' });

for (let n = 0; n < 3; n++)
  performance.mark(`test${n}`);

performanceObserver.takeRecords()

추가됨: v16.0.0

클래스: PerformanceObserverEntryList

추가됨: v8.5.0

PerformanceObserverEntryList 클래스는 PerformanceObserver에 전달된 PerformanceEntry 인스턴스에 대한 액세스를 제공하는 데 사용됩니다. 이 클래스의 생성자는 사용자에게 노출되지 않습니다.

performanceObserverEntryList.getEntries()

추가됨: v8.5.0

performanceEntry.startTime을 기준으로 시간 순서대로 PerformanceEntry 객체의 목록을 반환합니다.

js
import { performance, PerformanceObserver } from 'node:perf_hooks';

const obs = new PerformanceObserver((perfObserverList, observer) => {
  console.log(perfObserverList.getEntries());
  /**
   * [
   *   PerformanceEntry {
   *     name: 'test',
   *     entryType: 'mark',
   *     startTime: 81.465639,
   *     duration: 0,
   *     detail: null
   *   },
   *   PerformanceEntry {
   *     name: 'meow',
   *     entryType: 'mark',
   *     startTime: 81.860064,
   *     duration: 0,
   *     detail: null
   *   }
   * ]
   */

  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ type: 'mark' });

performance.mark('test');
performance.mark('meow');
js
const {
  performance,
  PerformanceObserver,
} = require('node:perf_hooks');

const obs = new PerformanceObserver((perfObserverList, observer) => {
  console.log(perfObserverList.getEntries());
  /**
   * [
   *   PerformanceEntry {
   *     name: 'test',
   *     entryType: 'mark',
   *     startTime: 81.465639,
   *     duration: 0,
   *     detail: null
   *   },
   *   PerformanceEntry {
   *     name: 'meow',
   *     entryType: 'mark',
   *     startTime: 81.860064,
   *     duration: 0,
   *     detail: null
   *   }
   * ]
   */

  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ type: 'mark' });

performance.mark('test');
performance.mark('meow');

performanceObserverEntryList.getEntriesByName(name[, type])

Added in: v8.5.0

performanceEntry.namename과 같고 선택적으로 performanceEntry.entryTypetype과 같은 PerformanceEntry 객체의 목록을 performanceEntry.startTime을 기준으로 시간 순서대로 반환합니다.

js
import { performance, PerformanceObserver } from 'node:perf_hooks';

const obs = new PerformanceObserver((perfObserverList, observer) => {
  console.log(perfObserverList.getEntriesByName('meow'));
  /**
   * [
   *   PerformanceEntry {
   *     name: 'meow',
   *     entryType: 'mark',
   *     startTime: 98.545991,
   *     duration: 0,
   *     detail: null
   *   }
   * ]
   */
  console.log(perfObserverList.getEntriesByName('nope')); // []

  console.log(perfObserverList.getEntriesByName('test', 'mark'));
  /**
   * [
   *   PerformanceEntry {
   *     name: 'test',
   *     entryType: 'mark',
   *     startTime: 63.518931,
   *     duration: 0,
   *     detail: null
   *   }
   * ]
   */
  console.log(perfObserverList.getEntriesByName('test', 'measure')); // []

  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ entryTypes: ['mark', 'measure'] });

performance.mark('test');
performance.mark('meow');
js
const {
  performance,
  PerformanceObserver,
} = require('node:perf_hooks');

const obs = new PerformanceObserver((perfObserverList, observer) => {
  console.log(perfObserverList.getEntriesByName('meow'));
  /**
   * [
   *   PerformanceEntry {
   *     name: 'meow',
   *     entryType: 'mark',
   *     startTime: 98.545991,
   *     duration: 0,
   *     detail: null
   *   }
   * ]
   */
  console.log(perfObserverList.getEntriesByName('nope')); // []

  console.log(perfObserverList.getEntriesByName('test', 'mark'));
  /**
   * [
   *   PerformanceEntry {
   *     name: 'test',
   *     entryType: 'mark',
   *     startTime: 63.518931,
   *     duration: 0,
   *     detail: null
   *   }
   * ]
   */
  console.log(perfObserverList.getEntriesByName('test', 'measure')); // []

  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ entryTypes: ['mark', 'measure'] });

performance.mark('test');
performance.mark('meow');

performanceObserverEntryList.getEntriesByType(type)

추가된 버전: v8.5.0

performanceEntry.entryTypetype과 같은 PerformanceEntry 객체 목록을 performanceEntry.startTime을 기준으로 시간순으로 반환합니다.

js
import { performance, PerformanceObserver } from 'node:perf_hooks';

const obs = new PerformanceObserver((perfObserverList, observer) => {
  console.log(perfObserverList.getEntriesByType('mark'));
  /**
   * [
   *   PerformanceEntry {
   *     name: 'test',
   *     entryType: 'mark',
   *     startTime: 55.897834,
   *     duration: 0,
   *     detail: null
   *   },
   *   PerformanceEntry {
   *     name: 'meow',
   *     entryType: 'mark',
   *     startTime: 56.350146,
   *     duration: 0,
   *     detail: null
   *   }
   * ]
   */
  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ type: 'mark' });

performance.mark('test');
performance.mark('meow');
js
const {
  performance,
  PerformanceObserver,
} = require('node:perf_hooks');

const obs = new PerformanceObserver((perfObserverList, observer) => {
  console.log(perfObserverList.getEntriesByType('mark'));
  /**
   * [
   *   PerformanceEntry {
   *     name: 'test',
   *     entryType: 'mark',
   *     startTime: 55.897834,
   *     duration: 0,
   *     detail: null
   *   },
   *   PerformanceEntry {
   *     name: 'meow',
   *     entryType: 'mark',
   *     startTime: 56.350146,
   *     duration: 0,
   *     detail: null
   *   }
   * ]
   */
  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ type: 'mark' });

performance.mark('test');
performance.mark('meow');

perf_hooks.createHistogram([options])

추가된 버전: v15.9.0, v14.18.0

  • options <Object>

    • lowest <number> | <bigint> 가장 낮은 식별 가능한 값. 0보다 큰 정수 값이어야 합니다. 기본값: 1.
    • highest <number> | <bigint> 기록 가능한 가장 높은 값. lowest의 두 배 이상인 정수 값이어야 합니다. 기본값: Number.MAX_SAFE_INTEGER.
    • figures <number> 정확도 자릿수. 15 사이의 숫자여야 합니다. 기본값: 3.
  • 반환: <RecordableHistogram>

<RecordableHistogram>을 반환합니다.

perf_hooks.monitorEventLoopDelay([options])

Added in: v11.10.0

이 속성은 Node.js의 확장입니다. 웹 브라우저에서는 사용할 수 없습니다.

시간 경과에 따른 이벤트 루프 지연을 샘플링하고 보고하는 IntervalHistogram 객체를 생성합니다. 지연 시간은 나노초 단위로 보고됩니다.

타이머를 사용하여 대략적인 이벤트 루프 지연을 감지하는 이유는 타이머 실행이 libuv 이벤트 루프의 라이프사이클에 구체적으로 연결되어 있기 때문입니다. 즉, 루프의 지연은 타이머 실행의 지연을 유발하고, 이러한 지연이 바로 이 API가 감지하려는 대상입니다.

js
import { monitorEventLoopDelay } from 'node:perf_hooks';

const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();
// 뭔가 하기.
h.disable();
console.log(h.min);
console.log(h.max);
console.log(h.mean);
console.log(h.stddev);
console.log(h.percentiles);
console.log(h.percentile(50));
console.log(h.percentile(99));
js
const { monitorEventLoopDelay } = require('node:perf_hooks');
const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();
// 뭔가 하기.
h.disable();
console.log(h.min);
console.log(h.max);
console.log(h.mean);
console.log(h.stddev);
console.log(h.percentiles);
console.log(h.percentile(50));
console.log(h.percentile(99));

Class: Histogram

Added in: v11.10.0

histogram.count

Added in: v17.4.0, v16.14.0

히스토그램에 기록된 샘플 수입니다.

histogram.countBigInt

Added in: v17.4.0, v16.14.0

히스토그램에 기록된 샘플 수입니다.

histogram.exceeds

추가된 버전: v11.10.0

이벤트 루프 지연이 최대 1시간 이벤트 루프 지연 임계값을 초과한 횟수입니다.

histogram.exceedsBigInt

추가된 버전: v17.4.0, v16.14.0

이벤트 루프 지연이 최대 1시간 이벤트 루프 지연 임계값을 초과한 횟수입니다.

histogram.max

추가된 버전: v11.10.0

기록된 최대 이벤트 루프 지연 시간입니다.

histogram.maxBigInt

추가된 버전: v17.4.0, v16.14.0

기록된 최대 이벤트 루프 지연 시간입니다.

histogram.mean

추가된 버전: v11.10.0

기록된 이벤트 루프 지연 시간의 평균입니다.

histogram.min

추가된 버전: v11.10.0

기록된 최소 이벤트 루프 지연 시간입니다.

histogram.minBigInt

추가된 버전: v17.4.0, v16.14.0

기록된 최소 이벤트 루프 지연 시간입니다.

histogram.percentile(percentile)

추가된 버전: v11.10.0

  • percentile <number> 범위 (0, 100]의 백분위수 값입니다.
  • 반환: <number>

지정된 백분위수의 값을 반환합니다.

histogram.percentileBigInt(percentile)

추가된 버전: v17.4.0, v16.14.0

  • percentile <number> 범위 (0, 100]의 백분위수 값입니다.
  • 반환: <bigint>

지정된 백분위수의 값을 반환합니다.

histogram.percentiles

Added in: v11.10.0

누적 백분위 분포를 자세히 설명하는 Map 객체를 반환합니다.

histogram.percentilesBigInt

Added in: v17.4.0, v16.14.0

누적 백분위 분포를 자세히 설명하는 Map 객체를 반환합니다.

histogram.reset()

Added in: v11.10.0

수집된 히스토그램 데이터를 재설정합니다.

histogram.stddev

Added in: v11.10.0

기록된 이벤트 루프 지연의 표준 편차입니다.

Class: IntervalHistogram extends Histogram

주어진 간격으로 주기적으로 업데이트되는 Histogram입니다.

histogram.disable()

Added in: v11.10.0

업데이트 간격 타이머를 비활성화합니다. 타이머가 중지되었으면 true를 반환하고 이미 중지되었으면 false를 반환합니다.

histogram.enable()

Added in: v11.10.0

업데이트 간격 타이머를 활성화합니다. 타이머가 시작되었으면 true를 반환하고 이미 시작되었으면 false를 반환합니다.

IntervalHistogram 복제하기

<IntervalHistogram> 인스턴스는 <MessagePort>를 통해 복제할 수 있습니다. 수신 측에서 히스토그램은 enable()disable() 메서드를 구현하지 않는 일반 <Histogram> 객체로 복제됩니다.

Class: RecordableHistogram extends Histogram

Added in: v15.9.0, v14.18.0

histogram.add(other)

Added in: v17.4.0, v16.14.0

other의 값을 이 히스토그램에 추가합니다.

histogram.record(val)

추가된 버전: v15.9.0, v14.18.0

histogram.recordDelta()

추가된 버전: v15.9.0, v14.18.0

recordDelta()를 마지막으로 호출한 이후 경과된 시간(나노초)을 계산하고 해당 양을 히스토그램에 기록합니다.

예시

비동기 작업의 지속 시간 측정

다음 예제는 Async Hooks 및 Performance API를 사용하여 Timeout 작업의 실제 지속 시간(콜백 실행에 걸린 시간 포함)을 측정합니다.

js
import { createHook } from 'node:async_hooks';
import { performance, PerformanceObserver } from 'node:perf_hooks';

const set = new Set();
const hook = createHook({
  init(id, type) {
    if (type === 'Timeout') {
      performance.mark(`Timeout-${id}-Init`);
      set.add(id);
    }
  },
  destroy(id) {
    if (set.has(id)) {
      set.delete(id);
      performance.mark(`Timeout-${id}-Destroy`);
      performance.measure(`Timeout-${id}`,
                          `Timeout-${id}-Init`,
                          `Timeout-${id}-Destroy`);
    }
  },
});
hook.enable();

const obs = new PerformanceObserver((list, observer) => {
  console.log(list.getEntries()[0]);
  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ entryTypes: ['measure'], buffered: true });

setTimeout(() => {}, 1000);
js
'use strict';
const async_hooks = require('node:async_hooks');
const {
  performance,
  PerformanceObserver,
} = require('node:perf_hooks');

const set = new Set();
const hook = async_hooks.createHook({
  init(id, type) {
    if (type === 'Timeout') {
      performance.mark(`Timeout-${id}-Init`);
      set.add(id);
    }
  },
  destroy(id) {
    if (set.has(id)) {
      set.delete(id);
      performance.mark(`Timeout-${id}-Destroy`);
      performance.measure(`Timeout-${id}`,
                          `Timeout-${id}-Init`,
                          `Timeout-${id}-Destroy`);
    }
  },
});
hook.enable();

const obs = new PerformanceObserver((list, observer) => {
  console.log(list.getEntries()[0]);
  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ entryTypes: ['measure'], buffered: true });

setTimeout(() => {}, 1000);

종속성을 로드하는 데 걸리는 시간 측정하기

다음 예제는 종속성을 로드하기 위해 require() 작업의 지속 시간을 측정합니다.

js
import { performance, PerformanceObserver } from 'node:perf_hooks';

// 관찰자 활성화
const obs = new PerformanceObserver((list) => {
  const entries = list.getEntries();
  entries.forEach((entry) => {
    console.log(`import('${entry[0]}')`, entry.duration);
  });
  performance.clearMarks();
  performance.clearMeasures();
  obs.disconnect();
});
obs.observe({ entryTypes: ['function'], buffered: true });

const timedImport = performance.timerify(async (module) => {
  return await import(module);
});

await timedImport('some-module');
js
'use strict';
const {
  performance,
  PerformanceObserver,
} = require('node:perf_hooks');
const mod = require('node:module');

// require 함수 몽키 패치
mod.Module.prototype.require =
  performance.timerify(mod.Module.prototype.require);
require = performance.timerify(require);

// 관찰자 활성화
const obs = new PerformanceObserver((list) => {
  const entries = list.getEntries();
  entries.forEach((entry) => {
    console.log(`require('${entry[0]}')`, entry.duration);
  });
  performance.clearMarks();
  performance.clearMeasures();
  obs.disconnect();
});
obs.observe({ entryTypes: ['function'], buffered: true });

require('some-module');

HTTP 왕복에 걸리는 시간 측정하기

다음 예제는 HTTP 클라이언트(OutgoingMessage)와 HTTP 요청(IncomingMessage)이 소비하는 시간을 추적하는 데 사용됩니다. HTTP 클라이언트의 경우 요청 시작과 응답 수신 사이의 시간 간격을 의미하고, HTTP 요청의 경우 요청 수신과 응답 전송 사이의 시간 간격을 의미합니다.

js
import { PerformanceObserver } from 'node:perf_hooks';
import { createServer, get } from 'node:http';

const obs = new PerformanceObserver((items) => {
  items.getEntries().forEach((item) => {
    console.log(item);
  });
});

obs.observe({ entryTypes: ['http'] });

const PORT = 8080;

createServer((req, res) => {
  res.end('ok');
}).listen(PORT, () => {
  get(`http://127.0.0.1:${PORT}`);
});
js
'use strict';
const { PerformanceObserver } = require('node:perf_hooks');
const http = require('node:http');

const obs = new PerformanceObserver((items) => {
  items.getEntries().forEach((item) => {
    console.log(item);
  });
});

obs.observe({ entryTypes: ['http'] });

const PORT = 8080;

http.createServer((req, res) => {
  res.end('ok');
}).listen(PORT, () => {
  http.get(`http://127.0.0.1:${PORT}`);
});

net.connect가 성공했을 때 소요 시간 측정 (TCP에만 해당)

js
import { PerformanceObserver } from 'node:perf_hooks';
import { connect, createServer } from 'node:net';

const obs = new PerformanceObserver((items) => {
  items.getEntries().forEach((item) => {
    console.log(item);
  });
});
obs.observe({ entryTypes: ['net'] });
const PORT = 8080;
createServer((socket) => {
  socket.destroy();
}).listen(PORT, () => {
  connect(PORT);
});
js
'use strict';
const { PerformanceObserver } = require('node:perf_hooks');
const net = require('node:net');
const obs = new PerformanceObserver((items) => {
  items.getEntries().forEach((item) => {
    console.log(item);
  });
});
obs.observe({ entryTypes: ['net'] });
const PORT = 8080;
net.createServer((socket) => {
  socket.destroy();
}).listen(PORT, () => {
  net.connect(PORT);
});

요청이 성공했을 때 DNS 소요 시간 측정

js
import { PerformanceObserver } from 'node:perf_hooks';
import { lookup, promises } from 'node:dns';

const obs = new PerformanceObserver((items) => {
  items.getEntries().forEach((item) => {
    console.log(item);
  });
});
obs.observe({ entryTypes: ['dns'] });
lookup('localhost', () => {});
promises.resolve('localhost');
js
'use strict';
const { PerformanceObserver } = require('node:perf_hooks');
const dns = require('node:dns');
const obs = new PerformanceObserver((items) => {
  items.getEntries().forEach((item) => {
    console.log(item);
  });
});
obs.observe({ entryTypes: ['dns'] });
dns.lookup('localhost', () => {});
dns.promises.resolve('localhost');