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

3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
S
shawn_he 已提交
4 5 6 7 8
> 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

9
```js
S
shawn_he 已提交
10 11 12 13 14 15
import hiTraceMeter from '@ohos.hiTraceMeter';
```


## hiTraceMeter.startTrace

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

S
shawn_he 已提交
18
Starts a trace task. **expectedTime** is an optional parameter, which specifies the expected duration of the trace.
S
shawn_he 已提交
19

S
shawn_he 已提交
20
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 已提交
21

S
shawn_he 已提交
22
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 已提交
23

S
shawn_he 已提交
24 25
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
26
**Parameters**
S
shawn_he 已提交
27

28
| Name| Type | Mandatory | Description |
S
shawn_he 已提交
29
| -------- | -------- | -------- | -------- |
30 31
| name | string | Yes | Name of the trace task to start. |
| taskId | number | Yes| Task ID. |
S
shawn_he 已提交
32 33 34

**Example**

35
```js
S
shawn_he 已提交
36
hiTraceMeter.startTrace("myTestFunc", 1);
S
shawn_he 已提交
37
hiTraceMeter.startTrace("myTestFunc", 1, 5); // The expected duration of the trace task is 5 ms.
S
shawn_he 已提交
38
```
S
shawn_he 已提交
39 40 41 42 43 44 45 46


## hiTraceMeter.finishTrace

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

Stops a trace task.

S
shawn_he 已提交
47 48
To stop a trace task, the values of name and task ID in **finishTrace** must be the same as those in [startTrace](#hitracemeterstarttrace).

S
shawn_he 已提交
49 50
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
51 52 53 54
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
55 56
| name | string | Yes| Name of the trace task to start. |
| taskId | number | Yes| Task ID. |
S
shawn_he 已提交
57 58 59

**Example**

60
```js
S
shawn_he 已提交
61 62 63
hiTraceMeter.finishTrace("myTestFunc", 1);
```

64
```js
S
shawn_he 已提交
65 66 67 68 69 70 71 72 73
// 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

75
```js
S
shawn_he 已提交
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
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
95 96 97 98
**Parameters**

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

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