- assert断言
- async_hooks异步钩子
- async_hooks/context异步上下文
- buffer缓冲区
- C++插件
- C/C++插件(使用Node-API)
- C++嵌入器
- child_process子进程
- cluster集群
- CLI命令行
- console控制台
- Corepack核心包
- crypto加密
- crypto/webcrypto网络加密
- debugger调试器
- deprecation弃用
- dgram数据报
- diagnostics_channel诊断通道
- dns域名服务器
- domain域
- Error错误
- events事件触发器
- fs文件系统
- global全局变量
- http超文本传输协议
- http2超文本传输协议2.0
- https安全超文本传输协议
- inspector检查器
- Intl国际化
- module模块
- module/cjsCommonJS模块
- module/esmECMAScript模块
- module/package包模块
- net网络
- os操作系统
- path路径
- perf_hooks性能钩子
- policy安全策略
- process进程
- punycode域名代码
- querystring查询字符串
- readline逐行读取
- repl交互式解释器
- report诊断报告
- stream流
- stream/web网络流
- string_decoder字符串解码器
- test测试
- timers定时器
- tls安全传输层
- trace_events跟踪事件
- tty终端
- url网址
- util实用工具
- v8引擎
- vm虚拟机
- wasi网络汇编系统接口
- worker_threads工作线程
- zlib压缩
Node.js v18.7.0 文档
- Node.js 18.7.0
-
►
目录
- perf_hooks 性能钩子
perf_hooks.performance
performance.clearMarks([name])
performance.clearMeasures([name])
performance.clearResourceTimings([name])
performance.eventLoopUtilization([utilization1[, utilization2]])
performance.getEntries()
performance.getEntriesByName(name[, type])
performance.getEntriesByType(type)
performance.mark([name[, options]])
performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, global, cacheMode)
performance.measure(name[, startMarkOrOptions[, endMark]])
performance.nodeTiming
performance.now()
performance.timeOrigin
performance.timerify(fn[, options])
performance.toJSON()
PerformanceEntry
类PerformanceNodeTiming
类PerformanceResourceTiming
类performanceResourceTiming.workerStart
performanceResourceTiming.redirectStart
performanceResourceTiming.redirectEnd
performanceResourceTiming.fetchStart
performanceResourceTiming.domainLookupStart
performanceResourceTiming.domainLookupEnd
performanceResourceTiming.connectStart
performanceResourceTiming.connectEnd
performanceResourceTiming.secureConnectionStart
performanceResourceTiming.requestStart
performanceResourceTiming.responseEnd
performanceResourceTiming.transferSize
performanceResourceTiming.encodedBodySize
performanceResourceTiming.decodedBodySize
performanceResourceTiming.toJSON()
perf_hooks.PerformanceObserver
类PerformanceObserverEntryList
类perf_hooks.createHistogram([options])
perf_hooks.monitorEventLoopDelay([options])
Histogram
类histogram.count
histogram.countBigInt
histogram.exceeds
histogram.exceedsBigInt
histogram.max
histogram.maxBigInt
histogram.mean
histogram.min
histogram.minBigInt
histogram.percentile(percentile)
histogram.percentileBigInt(percentile)
histogram.percentiles
histogram.percentilesBigInt
histogram.reset()
histogram.stddev
- IntervalHistogram 类继承 Histogram 类
- RecordableHistogram 类继承 Histogram 类
- 示例
- perf_hooks 性能钩子
-
►
索引
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS模块
- module/esm ECMAScript模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- policy 安全策略
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- ► 其他版本
- 文档搜索
- 会员登录
目录
- perf_hooks 性能钩子
perf_hooks.performance
performance.clearMarks([name])
performance.clearMeasures([name])
performance.clearResourceTimings([name])
performance.eventLoopUtilization([utilization1[, utilization2]])
performance.getEntries()
performance.getEntriesByName(name[, type])
performance.getEntriesByType(type)
performance.mark([name[, options]])
performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, global, cacheMode)
performance.measure(name[, startMarkOrOptions[, endMark]])
performance.nodeTiming
performance.now()
performance.timeOrigin
performance.timerify(fn[, options])
performance.toJSON()
PerformanceEntry
类PerformanceNodeTiming
类PerformanceResourceTiming
类performanceResourceTiming.workerStart
performanceResourceTiming.redirectStart
performanceResourceTiming.redirectEnd
performanceResourceTiming.fetchStart
performanceResourceTiming.domainLookupStart
performanceResourceTiming.domainLookupEnd
performanceResourceTiming.connectStart
performanceResourceTiming.connectEnd
performanceResourceTiming.secureConnectionStart
performanceResourceTiming.requestStart
performanceResourceTiming.responseEnd
performanceResourceTiming.transferSize
performanceResourceTiming.encodedBodySize
performanceResourceTiming.decodedBodySize
performanceResourceTiming.toJSON()
perf_hooks.PerformanceObserver
类PerformanceObserverEntryList
类perf_hooks.createHistogram([options])
perf_hooks.monitorEventLoopDelay([options])
Histogram
类histogram.count
histogram.countBigInt
histogram.exceeds
histogram.exceedsBigInt
histogram.max
histogram.maxBigInt
histogram.mean
histogram.min
histogram.minBigInt
histogram.percentile(percentile)
histogram.percentileBigInt(percentile)
histogram.percentiles
histogram.percentilesBigInt
histogram.reset()
histogram.stddev
- IntervalHistogram 类继承 Histogram 类
- RecordableHistogram 类继承 Histogram 类
- 示例
perf_hooks 性能钩子#
此模块提供了 W3C Web 性能 API 子集的实现,以及用于 Node.js 特定性能测量的其他 API。
perf_hooks.performance
#
可用于从当前 Node.js 实例收集性能指标的对象。
类似于浏览器中的 window.performance
。
performance.clearMarks([name])
#
如果未提供 name
,则从性能时间轴中删除所有 PerformanceMark
对象。
如果提供了 name
,则仅删除命名标记。
performance.clearMeasures([name])
#
如果未提供 name
,则从性能时间轴中删除所有 PerformanceMeasure
对象。
如果提供了 name
,则只删除指定的测量。
performance.clearResourceTimings([name])
#
如果未提供 name
,则从资源时间线中删除所有 PerformanceResourceTiming
对象。
如果提供了 name
,则只删除指定的资源。
performance.eventLoopUtilization([utilization1[, utilization2]])
#
eventLoopUtilization()
方法返回包含事件循环作为高解析度毫秒计时器的既空闲又活动的累积持续时间的对象。
utilization
值是计算的事件循环利用率 (ELU)。
performance.getEntries()
#
返回 PerformanceEntry
对象的列表,按照相对于 performanceEntry.startTime
的时间顺序排列。
如果您只对某些类型或具有某些名称的性能条目感兴趣,则参阅 performance.getEntriesByType()
和 performance.getEntriesByName()
。
performance.getEntriesByName(name[, type])
#
返回按时间顺序的 PerformanceEntry
对象列表,其中 performanceEntry.startTime
的 performanceEntry.name
等于 name
,并且可选地,其 performanceEntry.entryType
等于 type
。
performance.getEntriesByType(type)
#
返回按时间顺序排列的 PerformanceEntry
对象列表,其中 performanceEntry.startTime
的 performanceEntry.entryType
等于 type
。
performance.mark([name[, options]])
#
在性能时间轴中创建新的 PerformanceMark
条目。
PerformanceMark
是 PerformanceEntry
的子类,其 performanceEntry.entryType
始终为 'mark'
,而其 performanceEntry.duration
始终为 0
。
性能标记用于标记性能时间轴中的特定重要时刻。
performance.markResourceTiming(timingInfo, requestedUrl, initiatorType, global, cacheMode)
#
此属性是 Node.js 的扩展。 它在 Web 浏览器中不可用。
performance.measure(name[, startMarkOrOptions[, endMark]])
#
在性能时间轴中创建新的 PerformanceMeasure
条目。
PerformanceMeasure
是 PerformanceEntry
的子类,其 performanceEntry.entryType
始终为 'measure'
,其 performanceEntry.duration
测量自 startMark
和 endMark
以来经过的毫秒数。
performance.nodeTiming
#
此属性是 Node.js 的扩展。 它在 Web 浏览器中不可用。
performance.now()
#
返回当前的高解析度毫秒时间戳,其中 0 表示当前的 node
进程的开始。
performance.timeOrigin
#
timeOrigin
指定了当前的 node
进程开始的高解析度毫秒时间戳,以 Unix 时间度量。
performance.timerify(fn[, options])
#
此属性是 Node.js 的扩展。 它在 Web 浏览器中不可用。
performance.toJSON()
#
performance
对象的 JSON 表示的对象。
类似于浏览器中的 window.performance.toJSON
。
PerformanceEntry
类#
performanceEntry.detail
#
entryType
特有的附加细节。
performanceEntry.duration
#
此条目经过的总毫秒数。 此值对所有性能条目类型都没有意义。
performanceEntry.entryType
#
性能条目的类型。 它可能是以下之一:
performanceEntry.flags
#
此属性是 Node.js 的扩展。 它在 Web 浏览器中不可用。
performanceEntry.name
#
性能条目的名称。
performanceEntry.kind
#
此属性是 Node.js 的扩展。 它在 Web 浏览器中不可用。
performanceEntry.startTime
#
标记性能条目开始时间的高解析度毫秒时间戳。
垃圾回收的详细信息#
当 performanceEntry.type
等于 'gc'
时,则 performanceEntry.detail
属性将是具有两个属性的 <Object>:
HTTP 的详细信息#
当 performanceEntry.type
等于 'http'
时,则 performanceEntry.detail
属性将是一个包含额外信息的 <Object>。
HTTP/2 的详细信息#
当 performanceEntry.type
等于 'http2'
时,则 performanceEntry.detail
属性将是包含附加性能信息的 <Object>。
计时器化的详细信息#
当 performanceEntry.type
等于 'function'
时,则 performanceEntry.detail
属性将是列出计时函数的输入参数的 <Array>。
网络('net')的详细信息#
当 performanceEntry.type
等于 'net'
时,则 performanceEntry.detail
属性将是一个包含额外信息的 <Object>。
域名系统('dns')的详细信息#
当 performanceEntry.type
等于 'dns'
时,则 performanceEntry.detail
属性将是一个包含额外信息的 <Object>。
PerformanceNodeTiming
类#
此属性是 Node.js 的扩展。 它在 Web 浏览器中不可用。
performanceNodeTiming.bootstrapComplete
#
Node.js 进程完成引导的高解析度毫秒时间戳。 如果引导尚未完成,则该属性的值为 -1。
performanceNodeTiming.environment
#
Node.js 环境初始化的高解析度毫秒时间戳。
performanceNodeTiming.idleTime
#
事件循环在事件循环的事件提供者(例如 epoll_wait
)中空闲的时间量的高解析度毫秒时间戳。
这不考虑 CPU 使用率。
如果事件循环尚未开始(例如,在主脚本的第一个滴答中),则该属性的值为 0。
performanceNodeTiming.loopExit
#
Node.js 事件循环退出时的高解析度毫秒时间戳。
如果事件循环尚未退出,则该属性的值为 -1。
它只能在 'exit'
事件的句柄中具有非 -1 的值。
performanceNodeTiming.loopStart
#
Node.js 事件循环开始的高解析度毫秒时间戳。 如果事件循环尚未开始(例如,在主脚本的第一个滴答中),则该属性的值为 -1。
performanceNodeTiming.nodeStart
#
Node.js 进程初始化的高解析度毫秒时间戳。
performanceNodeTiming.v8Start
#
V8 平台初始化的高解析度毫秒时间戳。
PerformanceResourceTiming
类#
提供有关应用程序资源加载的详细网络计时数据。
performanceResourceTiming.workerStart
#
即将发送 fetch
请求之前的高解析度毫秒时间戳。
如果资源没有被工作进程截获,该属性将始终返回 0。
performanceResourceTiming.redirectStart
#
表示启动重定向的获取的开始时间的高解析度毫秒时间戳。
performanceResourceTiming.redirectEnd
#
接收到最后一个重定向响应的最后一个字节后立即创建的高解析度毫秒时间戳。
performanceResourceTiming.fetchStart
#
Node.js 开始获取资源之前的高解析度毫秒时间戳。
performanceResourceTiming.domainLookupStart
#
Node.js 开始查找资源之前的高解析度毫秒时间戳。
performanceResourceTiming.domainLookupEnd
#
表示 Node.js 完成对资源的域名查找之后的时间高解析度毫秒时间戳。
performanceResourceTiming.connectStart
#
表示 Node.js 开始与服务器建立连接以检索资源之前的时间的高解析度毫秒时间戳。
performanceResourceTiming.connectEnd
#
表示 Node.js 完成与服务器建立连接以检索资源后的时间的高解析度毫秒时间戳。
performanceResourceTiming.secureConnectionStart
#
表示 Node.js 开始握手过程以保护当前连接之前的时间的高解析度毫秒时间戳。
performanceResourceTiming.requestStart
#
表示 Node.js 从服务器接收到响应的第一个字节之前的时间的高解析度毫秒时间戳。
performanceResourceTiming.responseEnd
#
表示 Node.js 接收到资源的最后一个字节之后或传输连接关闭之前的时间(以先到者为准)的高解析度毫秒时间戳。
performanceResourceTiming.transferSize
#
表示获取资源大小(以八位字节为单位)的数值。 大小包括响应头字段加上响应负载正文。
performanceResourceTiming.encodedBodySize
#
表示在删除任何应用的内容编码之前从有效负载主体的提取(HTTP 或缓存)接收到的大小(以八位字节为单位)的数值。
performanceResourceTiming.decodedBodySize
#
表示在删除任何应用的内容编码后,从消息主体的提取(HTTP 或缓存)接收到的大小(以八位字节为单位)的数值。
performanceResourceTiming.toJSON()
#
返回 object
,其是 PerformanceResourceTiming
对象的 JSON 表示形式
perf_hooks.PerformanceObserver
类#
new PerformanceObserver(callback)
#
当新的 PerformanceEntry
实例被添加到性能时间线时,则 PerformanceObserver
对象会提供通知。
performanceObserver.disconnect()
#
断开 PerformanceObserver
实例与所有通知的连接。
performanceObserver.observe(options)
#
为 <PerformanceObserver> 实例订阅由 options.entryTypes
或 options.type
标识的新 <PerformanceEntry> 实例的通知:
PerformanceObserverEntryList
类#
PerformanceObserverEntryList
类用于提供对传给 PerformanceObserver
的 PerformanceEntry
实例的访问。
此类的构造函数不会暴露给用户。
performanceObserverEntryList.getEntries()
#
返回 PerformanceEntry
对象的列表,按照相对于 performanceEntry.startTime
的时间顺序排列。
performanceObserverEntryList.getEntriesByName(name[, type])
#
返回按时间顺序的 PerformanceEntry
对象列表,其中 performanceEntry.startTime
的 performanceEntry.name
等于 name
,并且可选地,其 performanceEntry.entryType
等于 type
。
performanceObserverEntryList.getEntriesByType(type)
#
返回按时间顺序排列的 PerformanceEntry
对象列表,其中 performanceEntry.startTime
的 performanceEntry.entryType
等于 type
。
perf_hooks.createHistogram([options])
#
perf_hooks.monitorEventLoopDelay([options])
#
此属性是 Node.js 的扩展。 它在 Web 浏览器中不可用。
Histogram
类#
histogram.count
#
直方图记录的样本数。
histogram.countBigInt
#
直方图记录的样本数。
histogram.exceeds
#
事件循环延迟超过最大 1 小时事件循环延迟阈值的次数。
histogram.exceedsBigInt
#
事件循环延迟超过最大 1 小时事件循环延迟阈值的次数。
histogram.max
#
记录的事件循环延迟的最大值。
histogram.maxBigInt
#
记录的事件循环延迟的最大值。
histogram.mean
#
记录的事件循环延迟的平均值。
histogram.min
#
记录的事件循环延迟的最小值。
histogram.minBigInt
#
记录的事件循环延迟的最小值。
histogram.percentile(percentile)
#
返回给定的百分位数的值。
histogram.percentileBigInt(percentile)
#
返回给定的百分位数的值。
histogram.percentiles
#
返回详细说明累积的百分位分布的 Map
对象。
histogram.percentilesBigInt
#
返回详细说明累积的百分位分布的 Map
对象。
histogram.reset()
#
重置收集的直方图数据。
histogram.stddev
#
记录的事件循环延迟的标准偏差。
IntervalHistogram 类继承 Histogram 类#
在给定的时间间隔内定期更新的 Histogram
。
histogram.disable()
#
禁用更新间隔计时器。
如果计时器被停止,则返回 true
,如果已被停止,则返回 false
。
histogram.enable()
#
启用更新间隔计时器。
如果计时器被启动,则返回 true
,如果已被启动,则返回 false
。
克隆 IntervalHistogram#
<IntervalHistogram> 实例可以通过 <MessagePort> 克隆。
在接收端,直方图被克隆为没有实现 enable()
和 disable()
方法的普通 <Histogram> 对象。
RecordableHistogram 类继承 Histogram 类#
histogram.add(other)
#
将 other
中的值添加到此直方图中。
histogram.record(val)
#
histogram.recordDelta()
#
计算自上次调用 recordDelta()
以来经过的时间量(以纳秒为单位),并在直方图中记录该量。
示例#
测量异步操作的时长#
以下示例使用异步钩子和性能 API 来测量超时操作的实际持续时间(包括执行回调所花费的时间)。
测量加载依赖的耗时#
以下示例测量加载依赖项的 require()
操作的持续时间:
测量一次 HTTP 往返需要多长时间#
以下示例用于跟踪 HTTP 客户端 (OutgoingMessage
) 和 HTTP 请求 (IncomingMessage
) 花费的时间。
对于 HTTP 客户端,是指发起请求到收到响应的时间间隔,对于 HTTP 请求,是指从接收请求到发送响应的时间间隔:
测量连接成功时 net.connect(仅适用于 TCP)需要多长时间#
'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 需要多长时间#
'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');