• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HiDebug接口使用示例(ArkTS)
2
3<!--Kit: Performance Analysis Kit-->
4<!--Subsystem: HiviewDFX-->
5<!--Owner: @hello_harmony; @yu_haoqiaida-->
6<!--Designer: @kutcherzhou1-->
7<!--Tester: @gcw_KuLfPSbe-->
8<!--Adviser: @foryourself-->
9
10HiDebug ArkTS接口功能独立,需要获取调试信息时直接调用。具体调用方式请参考[API参考文档](../reference/apis-performance-analysis-kit/js-apis-hidebug.md)中的示例。
11
12## 开发示例
13
14本文以获取系统CPU使用率为例,展示如何调用HiDebug ArkTS接口。
15
161. 使用DevEco Studio新建工程,选择“Empty Ability”。
17
182. 在Project窗口单击entry > src > main > ets > pages,打开并编辑Index.ets文件:
19
20   ```typescript
21   import { hidebug } from '@kit.PerformanceAnalysisKit';
22   import { BusinessError } from '@kit.BasicServicesKit';
23
24   function testHiDebugArk() {  // 按照需要调用的接口实现
25     try {
26       let ret = hidebug.getSystemCpuUsage();
27       console.info(`TestTag getSystemCpuUsage: ${ret}`);
28     } catch (error) {
29       console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
30     }
31   }
32
33   @Entry
34   @Component
35   struct Index {
36     build() {
37       Row() {
38         Column() {
39           Button("testHiDebugArk")
40             .type(ButtonType.Capsule)
41             .margin({
42               top: 20
43             })
44             .backgroundColor('#0D9FFB')
45             .width('60%')
46             .height('5%')
47             // 添加点击事件
48             .onClick(testHiDebugArk);
49         }
50         .width('100%')
51       }
52       .height('100%')
53     }
54   }
55   ```
56
573. 点击运行,然后在设备上点击“testHiDebugArk”按钮,触发接口调用。
58
594. 若接口调用存在日志输出,在DevEco Studio的底部,切换到“Log”窗口,即可查看相关日志。
60
61   ```Text
62   06-02 16:53:22.538   31077-31077   A03D00/com.exa...ication/JSAPP  com.examp...lication  I     TestTag getSystemCpuUsage: 0.09963547995139732
63   ```
64