1e41f4b71Sopenharmony_ci# @ohos.bytrace (ByTrace)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **bytrace** module implements performance tracing for processes.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci> - The APIs of this module are no longer maintained since API version 8. You are advised to use [`@ohos.hiTraceMeter`](js-apis-hitracemeter.md).
7e41f4b71Sopenharmony_ci> - 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.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Modules to Import
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```js
12e41f4b71Sopenharmony_ciimport { bytrace } from '@kit.PerformanceAnalysisKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## bytrace.startTrace
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_cistartTrace(name: string, taskId: number, expectedTime?: number): void
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciMarks the start of a timeslice trace task.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci> **NOTE**
22e41f4b71Sopenharmony_ci> 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.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiTrace
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**Parameters:**
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
29e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
30e41f4b71Sopenharmony_ci| name | string | Yes| Name of a timeslice trace task.|
31e41f4b71Sopenharmony_ci| taskId | number | Yes| ID of a timeslice trace task.|
32e41f4b71Sopenharmony_ci| expectedTime | number | No| Expected duration of the trace, in ms. Optional. By default, this parameter is left blank.|
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**Example**
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci```js
38e41f4b71Sopenharmony_cibytrace.startTrace("myTestFunc", 1);
39e41f4b71Sopenharmony_cibytrace.startTrace("myTestFunc", 1, 5); // The expected duration of the trace is 5 ms.
40e41f4b71Sopenharmony_ci```
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci## bytrace.finishTrace
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_cifinishTrace(name: string, taskId: number): void
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ciMarks the end of a timeslice trace task.
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci> **NOTE**<br>
49e41f4b71Sopenharmony_ci> To stop a trace task, the values of name and task ID in **finishTrace** must be the same as those in **startTrace**.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiTrace
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci**Parameters:**
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
56e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
57e41f4b71Sopenharmony_ci| name | string | Yes| Name of a timeslice trace task.|
58e41f4b71Sopenharmony_ci| taskId | number | Yes| ID of a timeslice trace task.|
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci**Example**
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci```js
64e41f4b71Sopenharmony_cibytrace.finishTrace("myTestFunc", 1);
65e41f4b71Sopenharmony_ci```
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci```
68e41f4b71Sopenharmony_ci// Start trace tasks with the same name concurrently.
69e41f4b71Sopenharmony_cibytrace.startTrace("myTestFunc", 1);
70e41f4b71Sopenharmony_ci// Service flow...
71e41f4b71Sopenharmony_cibytrace.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.
72e41f4b71Sopenharmony_ci// Service flow...
73e41f4b71Sopenharmony_cibytrace.finishTrace("myTestFunc", 1);
74e41f4b71Sopenharmony_ci// Service flow...
75e41f4b71Sopenharmony_cibytrace.finishTrace("myTestFunc", 2);
76e41f4b71Sopenharmony_ci```
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci```
79e41f4b71Sopenharmony_ci// Start trace tasks with the same name in serial mode.
80e41f4b71Sopenharmony_cibytrace.startTrace("myTestFunc", 1);
81e41f4b71Sopenharmony_ci// Service flow...
82e41f4b71Sopenharmony_cibytrace.finishTrace("myTestFunc", 1);  // The first trace task ends.
83e41f4b71Sopenharmony_ci// Service flow...
84e41f4b71Sopenharmony_cibytrace.startTrace("myTestFunc", 1);   // The second trace task starts after the first task ends. The two tasks have the same name and task ID.
85e41f4b71Sopenharmony_ci// Service flow...
86e41f4b71Sopenharmony_cibytrace.finishTrace("myTestFunc", 1);
87e41f4b71Sopenharmony_ci```
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci## bytrace.traceByValue
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_citraceByValue(name: string, count: number): void
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ciDefines a numeric variable that indicates the number of timeslice trace tasks.
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiTrace
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci**Parameters:**
98e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description|
99e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
100e41f4b71Sopenharmony_ci| name | string | Yes| Name of the numeric variable.|
101e41f4b71Sopenharmony_ci| count | number | Yes| Value of the numeric variable.|
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**Example**
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci```js
106e41f4b71Sopenharmony_cilet traceCount = 3;
107e41f4b71Sopenharmony_cibytrace.traceByValue("myTestCount", traceCount);
108e41f4b71Sopenharmony_citraceCount = 4;
109e41f4b71Sopenharmony_cibytrace.traceByValue("myTestCount", traceCount);
110e41f4b71Sopenharmony_ci// Service flow
111e41f4b71Sopenharmony_ci```
112