PerformanceServerTiming

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

Note: This feature is available in Web Workers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Giao diện PerformanceServerTiming hiển thị các số liệu máy chủ được gửi cùng với phản hồi trong tiêu đề HTTP Server-Timing.

Giao diện này bị hạn chế ở cùng một nguồn gốc, nhưng bạn có thể sử dụng tiêu đề Timing-Allow-Origin để chỉ định các tên miền được phép truy cập các số liệu máy chủ. Lưu ý rằng giao diện này chỉ khả dụng trong các ngữ cảnh bảo mật (HTTPS) trong một số trình duyệt.

Thuộc tính phiên bản

PerformanceServerTiming.description Read only

Giá trị chuỗi của mô tả số liệu do máy chủ chỉ định, hoặc một chuỗi rỗng.

PerformanceServerTiming.duration Read only

Một số double chứa thời lượng số liệu do máy chủ chỉ định, hoặc giá trị 0.0.

PerformanceServerTiming.name Read only

Giá trị chuỗi của tên số liệu do máy chủ chỉ định.

Phương thức phiên bản

PerformanceServerTiming.toJSON()

Trả về biểu diễn JSON của đối tượng PerformanceServerTiming.

Ví dụ

Cho một máy chủ gửi tiêu đề Server-Timing, ví dụ máy chủ Node.js như thế này:

js
const http = require("http");

function requestHandler(request, response) {
  const headers = {
    "Server-Timing": `
      cache;desc="Cache Read";dur=23.2,
      db;dur=53,
      app;dur=47.2
    `.replace(/\n/g, ""),
  };
  response.writeHead(200, headers);
  response.write("");
  return setTimeout(() => {
    response.end();
  }, 1000);
}

http.createServer(requestHandler).listen(3000).on("error", console.error);

Các mục nhập PerformanceServerTiming hiện có thể quan sát được từ JavaScript thông qua thuộc tính PerformanceResourceTiming.serverTiming và tồn tại trên các mục nhập navigationresource.

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    entry.serverTiming.forEach((serverEntry) => {
      console.log(
        `${serverEntry.name} (${serverEntry.description}) duration: ${serverEntry.duration}`,
      );
    });
  });
});

["navigation", "resource"].forEach((type) =>
  observer.observe({ type, buffered: true }),
);

Thông số kỹ thuật

Thông số kỹ thuật
Server Timing
# the-performanceservertiming-interface

Tương thích trình duyệt

Xem thêm