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