From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/user_timing_api/index.html | 94 ++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 files/zh-cn/web/api/user_timing_api/index.html (limited to 'files/zh-cn/web/api/user_timing_api') diff --git a/files/zh-cn/web/api/user_timing_api/index.html b/files/zh-cn/web/api/user_timing_api/index.html new file mode 100644 index 0000000000..e864ea244c --- /dev/null +++ b/files/zh-cn/web/api/user_timing_api/index.html @@ -0,0 +1,94 @@ +--- +title: 自定义时间测量API +slug: Web/API/User_Timing_API +translation_of: Web/API/User_Timing_API +--- +
{{DefaultAPISidebar("自定义时间测量API")}}
+ +

User Timing接口允许开发者在浏览器性能时间线中创建针对特定应用的 {{domxref("DOMHighResTimeStamp","时间戳")}}。有两种自定义时间测量事件类型:"mark" {{domxref("PerformanceEntry.entryType","事件类型")}}和"measure" {{domxref("PerformanceEntry.entryType","事件类型")}}。

+ +

mark事件可以指定任意的名字并且可以在放在应用的任何位置,measure事件也可以指定为任意的名字,但是需要放在两个mark之间,所以它实际上是两个mark的中间点。

+ +

此文档提供了对markmeasure{{domxref("PerformanceEntry.entryType","性能事件类型")}}的概览,包括四个拓展了{{domxref("Performance")}}接口的User Timing方法。 想要了解这两种性能事件类型和相关方法的更多详细内容和示例代码,请查阅使用自定义时间测量API

+ +

性能标记

+ +

性能标记是一个由应用创建并指定名称的{{domxref("PerformanceEntry","性能条目")}}。这个标记是浏览器性能时间线上的一个{{domxref("DOMHighResTimeStamp","时间戳")}}。

+ +

创建一个性能标记

+ +

{{domxref("Performance.mark","performance.mark()")}}方法被用来创建一个性能标记。这个方法需要一个参数,标记的名称(例如performance.mark("mark-1"))。

+ +

标记的{{domxref("PerformanceEntry","性能入口")}}包含以下属性值:

+ + + +

检索一个性能标记

+ +

{{domxref("Performance")}}接口有三个方法可以用来检索标记:

+ +
+
{{domxref("Performance.getEntries","performance.getEntries()")}}
+
返回性能时间线上的所有{{domxref("PerformanceEntry","性能条目")}}。通过筛选{{domxref("PerformanceEntry.entryType","entryType")}}属性为"mark"的条目来获取所有标记条目。
+
{{domxref("Performance.getEntriesByType","performance.getEntriesByType(entryType)")}}
+
返回性能事件线上具有指定entryType的{{domxref("PerformanceEntry","性能条目")}},通过将entryType设置为"mark"来获取所有标记条目。
+
{{domxref("Performance.getEntriesByName","performance.getEntriesByName(name, entryType)")}}
+
返回性能时间线上具有指定nameentryType的{{domxref("PerformanceEntry","性能条目")}} 。将entryType设置为"mark"来获得所有的标记条目(通过指定name来检索更具体的条目)。
+
+ +

移除性能标记

+ +

想要从性能事件线上移除一个特定标记,调用performance.clearMarks(name)name是想要移除的标记的名称。如果这个方法被调用时没有传入任何参数,那么性能时间线上所有标记类型的条目都会被移除。

+ +

性能测量

+ +

measure事件同样由程序指定名称,但是它被放在两个标记之间因此实际上是两个标记间的中间点。

+ +

创建一个性能测量

+ +

测量通过调用performance.measure(measureName, startMarkName, endMarkName)来创建,measureName指定了该测量的名称,startMarkNameendMarkName分别指定了性能时间线上该测量前后的两个标记的名称。

+ +

测量{{domxref("PerformanceEntry","性能条目")}}包含以下属性值:

+ + + +

检索性能测量

+ +

{{domxref("Performance")}}接口有三个方法可以检索一个测量:

+ +
+
{{domxref("Performance.getEntries","performance.getEntries()")}}
+
返回性能时间线上所有的{{domxref("PerformanceEntry","性能条目")}}。通过筛选{{domxref("PerformanceEntry.entryType","entryType")}}属性为"measure"的条目来获取所有测量条目。
+
{{domxref("Performance.getEntriesByType","performance.getEntriesByType(entryType)")}}
+
返回性能事件线上指定entryType的{{domxref("PerformanceEntry","性能条目")}},通过将entryType设置为"measure"来获取所有的测量条目。
+
{{domxref("Performance.getEntriesByName","performance.getEntriesByName(name, entryType)")}}
+
返回性能时间线上具有指定 nameentryType的{{domxref("PerformanceEntry","性能项目")}},将entryType设置为"measure"来获取所有测量条目(通过指定name参数来检索更具体的条目)。
+
+ +

移除性能测量

+ +

想要从性能时间线上移除一个测量,调用performance.clearMeasures(name)name 是要移除的测量的名称。如果该方法被调用时没有传入任何参数,那么性能时间线上所有的测量都会被移除。

+ +

互操作性

+ +

如{{domxref("Performance")}}接口的浏览器兼容性表所示,User Timing 方法被大多数桌面和移动浏览器(唯一的例外是桌面Safari和移动版Safari,Safari Technology Preview 24已经支持该方法)。

+ +

想要测试你的浏览器是否支持该API,运行perf-api-support程序。

+ +

相关内容

+ + -- cgit v1.2.3-54-g00ecf