PerformanceServerTiming: duration 属性
注意:此功能在 Web Workers 中可用。
duration 只读属性返回一个双精度浮点数,包含服务器指定的指标持续时间,或者返回 0.0。
值
一个数字。
示例
记录服务器计时条目
服务器计时指标需要服务器发送 Server-Timing 标头。例如:
http
Server-Timing: cache;desc="Cache Read";dur=23.2
serverTiming 条目可以存在于 navigation 和 resource 条目上。
使用 PerformanceObserver 的示例,它会在 navigation 和 resource 性能条目被记录到浏览器性能时间线上时通知。使用 buffered 选项可以访问观察者创建之前的条目。
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
entry.serverTiming.forEach((serverEntry) => {
console.log(
`${serverEntry.name} (${serverEntry.description}) duration: ${serverEntry.duration}`,
);
// Logs "cache (Cache Read) duration: 23.2"
});
});
});
["navigation", "resource"].forEach((type) =>
observer.observe({ type, buffered: true }),
);
使用 Performance.getEntriesByType() 的示例,它仅显示在调用此方法时存在于浏览器性能时间线中的 navigation 和 resource 性能条目。
js
for (const entryType of ["navigation", "resource"]) {
for (const { name: url, serverTiming } of performance.getEntriesByType(
entryType,
)) {
if (serverTiming) {
for (const { name, description, duration } of serverTiming) {
console.log(`${name} (${description}) duration: ${duration}`);
// Logs "cache (Cache Read) duration: 23.2"
}
}
}
}
规范
| 规范 |
|---|
| 服务器计时 # dom-performanceservertiming-duration |
浏览器兼容性
加载中…