PerformanceServerTiming - Web APIs 编辑

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.

The PerformanceServerTiming interface surfaces server metrics that are sent with the response in the Server-Timing HTTP header.

This interface is restricted to the same origin, but you can use the Timing-Allow-Origin header to specify the domains that are allowed to access the server metrics. Note that this interface is only available in secure contexts (HTTPS) in some browsers.

Properties

PerformanceServerTiming.descriptionRead only
A DOMString value of the server-specified metric description, or an empty string.
PerformanceServerTiming.durationRead only
A double that contains the server-specified metric duration, or value 0.0.
PerformanceServerTiming.nameRead only
A DOMString value of the server-specified metric name.

Methods

PerformanceServerTiming.toJSON()
Returns a DOMString that is the JSON representation of the PerformanceServerTiming object.

Example

Given a server that sends the Server-Timing header, for example a node.js server like this:

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);

The PerformanceServerTiming entries are now observable from JavaScript via the PerformanceResourceTiming.serverTiming property:

let entries = performance.getEntriesByType('resource');
console.log(entries[0].serverTiming);
// 0: PerformanceServerTiming {name: "cache", duration: 23.2, description: "Cache Read"}
// 1: PerformanceServerTiming {name: "db", duration: 53, description: ""}
// 2: PerformanceServerTiming {name: "app", duration: 47.2, description: ""}

Specifications

SpecificationStatusComment
Server Timing
The definition of 'PerformanceServerTiming' in that specification.
Working DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:104 次

字数:4818

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文