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