• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用HiTraceMeter跟踪性能(ArkTS/JS2
3## 简介
4
5HiTraceMeter提供系统性能打点接口。开发者通过在关键代码位置调用HiTraceMeter接口提供的API接口,能够有效跟踪进程轨迹、查看系统性能。
6
7## 基本概念
8
9**HiTraceMeter Tag**:跟踪数据使用类别分类,称作HiTraceMeter Tag或HiTraceMeter Category。一般每个软件子系统对应一个Tag,该Tag在打点API中以类别Tag参数传入。HiTraceMeter命令行工具采集跟踪数据时,只采集给定的Tag类别选项指定的跟踪数据。
10
11## 实现原理
12
131. 应用程序通过HiTraceMeter函数接口进行打点,HiTraceMeter函数将跟踪数据通过内核sysfs文件接口输出到内核的ftrace数据缓冲区。
14
152. HiTraceMeter命令行工具读取内核ftrace缓冲区中的跟踪数据,将文本格式的跟踪数据保存到设备侧的文件中。
16
17## 约束与限制
18
19由于JS程序的异步IO特性,目前HiTraceMeter只提供了异步接口。
20
21## 接口说明
22
23性能打点跟踪接口由HiTraceMeter模块提供,详细API请参考[性能打点跟踪API参考](../reference/apis-performance-analysis-kit/js-apis-hitracemeter.md)。
24
25| 接口名 | 描述 |
26| -------- | -------- |
27| hiTraceMeter.startTrace(name: string, taskId: number) | 标记一个预跟踪耗时任务的开始。taskId是trace中用来表示关联的ID,如果有多个name相同的任务并行执行,则每次调用startTrace的taskId不同;如果具有相同name的任务是串行执行的,则taskId可以相同。 |
28| hiTraceMeter.finishTrace(name: string, taskId: number) | name和taskId必须与流程开始的hiTraceMeter.startTrace对应参数值保持一致。 |
29| hiTraceMeter.traceByValue(name: string, value: number) | 用来标记一个预跟踪的数值变量,该变量的数值会不断变化。 |
30
31## 开发步骤
32
33在应用启动执行页面加载后,开始分布式跟踪;完成业务之后,停止分布式跟踪。
34
351. 新建一个ArkTS应用工程,在“Project”窗口点击“entry > src > main > ets > pages > index”,打开工程中的“index.ets”文件;在页面执行加载后,在自己的业务中调用hiTraceMeter的接口,进行性能打点跟踪,以任务名name为HITRACE_TAG_APP为例, 示例代码如下:
36
37   ```ts
38   import hitrace from '@ohos.hiTraceMeter';
39
40   @Entry
41   @Component
42   struct Index {
43     @State message: string = 'Hello World';
44
45     build() {
46       Row() {
47         Column() {
48           Text(this.message)
49             .fontSize(50)
50             .fontWeight(FontWeight.Bold)
51             .onClick(() => {
52               this.message = 'Hello ArkUI';
53
54               // 跟踪并行执行的同名任务
55               hitrace.startTrace("HITRACE_TAG_APP", 1001);
56               // 业务流程
57               console.log(`HITRACE_TAG_APP running`);
58
59               // 第二个跟踪任务开始,同时第一个跟踪的同名任务还没结束,出现了并行执行,对应接口的taskId需要不同。
60               hitrace.startTrace("HITRACE_TAG_APP", 1002);
61               // 业务流程
62               console.log(`HITRACE_TAG_APP running`);
63
64               hitrace.finishTrace("HITRACE_TAG_APP", 1001);
65               hitrace.finishTrace("HITRACE_TAG_APP", 1002);
66
67               // 跟踪串行执行的同名任务,taskId可以不同,也可以相同
68               hitrace.startTrace("HITRACE_TAG_APP", 1003);
69               // 业务流程
70               console.log(`HITRACE_TAG_APP running`);
71               //第一个跟踪的任务结束
72               hitrace.finishTrace("HITRACE_TAG_APP", 1003);
73
74               // 第二个跟踪任务开始,同名的待跟踪任务串行执行,且taskId不同
75               hitrace.startTrace("HITRACE_TAG_APP", 1004);
76               // 业务流程
77               console.log(`HITRACE_TAG_APP running`);
78               let traceCount = 3;
79               hitrace.traceByValue("myTestCount", traceCount);
80               hitrace.finishTrace("HITRACE_TAG_APP", 1004);
81
82               // 第三个跟踪任务开始,同名的待跟踪任务串行执行,且taskId与上一个相同
83               hitrace.startTrace("HITRACE_TAG_APP", 1004);
84               // 业务流程
85               console.log(`HITRACE_TAG_APP running`);
86               //第三个跟踪的任务结束
87               hitrace.finishTrace("HITRACE_TAG_APP", 1004);
88
89             })
90          }
91          .width('100%')
92        }
93        .height('100%')
94      }
95   }
96   ```
97
982. 运行项目,单击IDE界面上的运行按钮,在shell中依次执行如下命令:
99
100   ```shell
101   hdc shell
102   hitrace --trace_begin app
103   ```
104
105   执行抓取trace命令后,先在设备中自己的业务调用接口,继续依次执行如下命令:
106
107   ```shell
108   hitrace --trace_dump | grep tracing_mark_write
109   hitrace --trace_finish
110   ```
111
112   抓取trace成功的数据如下所示:
113
114   ```text
115   <...>-3310    (-------) [005] .... 351382.921936: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1001
116   <...>-3310    (-------) [005] .... 351382.922138: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1002
117   <...>-3310    (-------) [005] .... 351382.922165: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1001
118   <...>-3310    (-------) [005] .... 351382.922175: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1002
119   <...>-3310    (-------) [005] .... 351382.922182: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1003
120   <...>-3310    (-------) [005] .... 351382.922203: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1003
121   <...>-3310    (-------) [005] .... 351382.922210: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1004
122   <...>-3310    (-------) [005] .... 351382.922233: tracing_mark_write: C|3310|H:myTestCount 3
123   <...>-3310    (-------) [005] .... 351382.922240: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1004
124   <...>-3310    (-------) [005] .... 351382.922247: tracing_mark_write: S|3310|H:HITRACE_TAG_APP 1004
125   <...>-3310    (-------) [005] .... 351382.922266: tracing_mark_write: F|3310|H:HITRACE_TAG_APP 1004
126   ```
127