• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bytrace (Performance Tracing)
2
3<!--Kit: Performance Analysis Kit-->
4<!--Subsystem: HiviewDFX-->
5<!--Owner: @qq_437963121-->
6<!--SE: @MontSaintMichel-->
7<!--TSE: @gcw_KuLfPSbe-->
8
9The **bytrace** module implements performance tracing for processes.
10
11> **NOTE**
12> - The APIs of this module are no longer maintained since API version 8. You are advised to use [`@ohos.hiTraceMeter`](js-apis-hitracemeter.md).
13> - The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
14
15## Modules to Import
16
17```js
18import { bytrace } from '@kit.PerformanceAnalysisKit';
19```
20
21## bytrace.startTrace
22
23startTrace(name: string, taskId: number, expectedTime?: number): void
24
25Marks the start of a timeslice trace task.
26
27> **NOTE**
28> If multiple trace tasks with the same name need to be performed at the same time or a trace task needs to be performed multiple times concurrently, different task IDs must be specified in **startTrace**. If the trace tasks with the same name are not performed at the same time, the same task ID can be used. For details, see the bytrace.finishTrace example.
29
30**System capability**: SystemCapability.HiviewDFX.HiTrace
31
32**Parameters:**
33
34| Name| Type| Mandatory| Description|
35| -------- | -------- | -------- | -------- |
36| name | string | Yes| Name of a timeslice trace task.|
37| taskId | number | Yes| ID of a timeslice trace task.|
38| expectedTime | number | No| Expected duration of the trace, in ms. Optional. By default, this parameter is left blank.|
39
40
41**Example**
42
43```js
44bytrace.startTrace("myTestFunc", 1);
45bytrace.startTrace("myTestFunc", 1, 5); // The expected duration of the trace is 5 ms.
46```
47
48## bytrace.finishTrace
49
50finishTrace(name: string, taskId: number): void
51
52Marks the end of a timeslice trace task.
53
54> **NOTE**<br>
55> To stop a trace task, the values of name and task ID in **finishTrace** must be the same as those in **startTrace**.
56
57**System capability**: SystemCapability.HiviewDFX.HiTrace
58
59**Parameters:**
60
61| Name| Type| Mandatory| Description|
62| -------- | -------- | -------- | -------- |
63| name | string | Yes| Name of a timeslice trace task.|
64| taskId | number | Yes| ID of a timeslice trace task.|
65
66
67**Example**
68
69```js
70bytrace.finishTrace("myTestFunc", 1);
71```
72
73```
74// Start trace tasks with the same name concurrently.
75bytrace.startTrace("myTestFunc", 1);
76// Service flow...
77bytrace.startTrace("myTestFunc", 2);  // The second trace task starts while the first task is still running. The first and second tasks have the same name but different task IDs.
78// Service flow...
79bytrace.finishTrace("myTestFunc", 1);
80// Service flow...
81bytrace.finishTrace("myTestFunc", 2);
82```
83
84```
85// Start trace tasks with the same name in serial mode.
86bytrace.startTrace("myTestFunc", 1);
87// Service flow...
88bytrace.finishTrace("myTestFunc", 1);  // The first trace task ends.
89// Service flow...
90bytrace.startTrace("myTestFunc", 1);   // The second trace task starts after the first task ends. The two tasks have the same name and task ID.
91// Service flow...
92bytrace.finishTrace("myTestFunc", 1);
93```
94
95## bytrace.traceByValue
96
97traceByValue(name: string, count: number): void
98
99Defines a numeric variable that indicates the number of timeslice trace tasks.
100
101**System capability**: SystemCapability.HiviewDFX.HiTrace
102
103**Parameters:**
104| Name| Type| Mandatory| Description|
105| -------- | -------- | -------- | -------- |
106| name | string | Yes| Name of the numeric variable.|
107| count | number | Yes| Value of the numeric variable.|
108
109**Example**
110
111```js
112let traceCount = 3;
113bytrace.traceByValue("myTestCount", traceCount);
114traceCount = 4;
115bytrace.traceByValue("myTestCount", traceCount);
116// Service flow
117```
118