js-apis-hitracemeter.md 2.9 KB
Newer Older
S
shawn_he 已提交
1 2
# Performance Tracing

S
shawn_he 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
S
shawn_he 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.


## Modules to Import

```
import hiTraceMeter from '@ohos.hiTraceMeter';
```


## System Capabilities

SystemCapability.HiviewDFX.HiTrace


## hiTraceMeter.startTrace

X
xuyong 已提交
21
startTrace(name: string, taskId: number): void
S
shawn_he 已提交
22

X
xuyong 已提交
23
Starts a trace taske.
S
shawn_he 已提交
24

S
shawn_he 已提交
25
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**.
S
shawn_he 已提交
26

S
shawn_he 已提交
27
If the trace tasks with the same name are not performed at the same time, the same taskId can be used. For a specific example, refer to an example in [hiTraceMeter.finishTrace](#hitracemeterfinishtrace).
S
shawn_he 已提交
28

S
shawn_he 已提交
29
**Parameters**
S
shawn_he 已提交
30

S
shawn_he 已提交
31 32 33 34 35 36 37 38 39 40
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the trace task to start.|
| taskId | number | Yes| Task ID.|

**Example**

```
hiTraceMeter.startTrace("myTestFunc", 1);
```
S
shawn_he 已提交
41 42 43 44 45 46 47 48


## hiTraceMeter.finishTrace

finishTrace(name: string, taskId: number): void

Stops a trace task.

S
shawn_he 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
To stop a trace task, the values of name and task ID in **finishTrace** must be the same as those in [startTrace](#hitracemeterstarttrace).

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the trace task to start.|
| taskId | number | Yes| Task ID.|

**Example**

```
hiTraceMeter.finishTrace("myTestFunc", 1);
```

```
// Start track tasks with the same name concurrently.
hiTraceMeter.startTrace("myTestFunc", 1);
// Service flow
hiTraceMeter.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.
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 1);
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 2);
```
S
shawn_he 已提交
74

S
shawn_he 已提交
75 76 77 78 79 80 81 82 83 84
```
// Start track tasks with the same name at different times.
hiTraceMeter.startTrace("myTestFunc", 1);
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 1);  // The first trace task ends.
// Service flow
hiTraceMeter.startTrace("myTestFunc", 1);   // The second trace task starts after the first task ends. The two tasks have the same name and task ID. 
// Service flow
hiTraceMeter.finishTrace("myTestFunc", 1);
```
S
shawn_he 已提交
85 86 87 88


## hiTraceMeter.traceByValue

X
xuyong 已提交
89
traceByValue(name: string, count: number): void
S
shawn_he 已提交
90 91 92

Traces the value changes of a variable.

S
shawn_he 已提交
93 94 95 96 97
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Name of the variable.|
X
xuyong 已提交
98
| count | number | Yes| Value of the variable.|
S
shawn_he 已提交
99

S
shawn_he 已提交
100 101 102 103 104 105 106 107
**Example**
```
let traceCount = 3;
hiTraceMeter.traceByValue("myTestCount", traceCount);
traceCount = 4;
hiTraceMeter.traceByValue("myTestCount", traceCount);
// Service flow
```