1# @ohos.test.PerfTest 2 3PerfTest provides white-box performance testing capabilities. It can test performance data of specified code segments or scenarios, automatically execute test code segments, and collect performance data such as time consumption, CPU, memory, latency, and frame rate. 4 5> **NOTE** 6> - The initial APIs of this module are supported since API version 20. Newly added APIs will be marked with a superscript to indicate their earliest API version. 7> - The APIs of this module can be used only in <!--RP1-->[arkxtest](../../application-test/arkxtest-guidelines.md)<!--RP1End-->. 8> - The APIs of this module do not support concurrent calls. 9> - The APIs of this module are applicable to phones, tablets, PCs/2-in-1 devices, smart TVs, and head units. 10 11 12## Modules to Import 13 14```ts 15import { PerfMetric, PerfTest, PerfTestStrategy, PerfMeasureResult } from '@kit.TestKit'; 16``` 17 18## PerfMetric 19 20Represents performance metrics that can be collected by the framework. 21 22**Atomic service API**: This API can be used in atomic services since API version 20. 23 24**System capability**: SystemCapability.Test.PerfTest 25 26| Name | Value | Description | 27| ---------- | ---- | -------- | 28| DURATION | 0 | Execution duration of a code segment, in milliseconds. | 29| CPU_LOAD | 1 | CPU load of the application process, in percentage. | 30| CPU_USAGE | 2 | CPU usage of the application process, in percentage. | 31| MEMORY_RSS | 3 | Physical memory (including the shared library) occupied by the application process when a code segment is executed, in KB. | 32| MEMORY_PSS | 4 | Physical memory (excluding the shared library) occupied by the application process when a code segment is executed, in KB. | 33| APP_START_RESPONSE_TIME | 5 | Response latency of application startup, in milliseconds. | 34| APP_START_COMPLETE_TIME | 6 | Completion latency of application startup, in milliseconds. | 35| PAGE_SWITCH_COMPLETE_TIME | 7 | Completion latency of page switching in an application, in milliseconds. | 36| LIST_SWIPE_FPS | 8 | List scrolling frame rate in an application, in frames per second (fps).| 37 38> **NOTE** 39> 40> 1. The preceding metrics collect performance data for a specified application process, not for the system. 41> 2. Description of collecting the CPU data (**CPU_LOAD**/**CPU_USAGE**) and memory (**MEMORY_RSS**/**MEMORY_PSS**): 42> - During the test, the CPU and memory data of the specified application process is collected before and after the code segment execution. Therefore, ensure that the application process to be tested exists during the test. 43> 3. Description of collecting the application startup latency data (**APP_START_RESPONSE_TIME**/**APP_START_COMPLETE_TIME**): 44> - Application startup latency data is affected by system log reporting. The start time is when the tap event is reported, the end time of the response latency is when the first frame is displayed on the screen after the tap, and the end time of the completion latency is when the first frame is displayed on the screen after the application is started. The latency is different from what users perceive. 45> - Application startup latency data can be collected in the following scenarios: tapping an application icon on the home screen, tapping an application icon on the dock bar, and tapping an application icon in the application center. 46> - During a test, only the first startup latency of the specified application is collected. 47> 4. Description of collecting the page switching latency data (**PAGE_SWITCH_COMPLETE_TIME**): 48> - The page switching latency calculation is affected by the system log reporting. The start time is when the tap event is reported, and the end time is when the first frame is displayed on the screen after the page switching, which is different from what users perceive. 49> - Page switching latency data can be collected in the **Router** and **Navigation** components. 50> - During a test, only the first page switching latency in the specified application is collected. 51> 5. Description of collecting the list scrolling frame rate (**LIST_SWIPE_FPS**): 52> - **LIST_SWIPE_FPS**: The number of frames rendered and updated on the screen per second when the list is scrolled. 53> - Supported scenarios: list scrolling of the **List**, **Grid**, **Scroll**, and **WaterFlow** components in the ArkUI subsystem. 54> - During a test, only the first list scrolling frame rate in the specified application is collected. 55 56 57## PerfTestStrategy 58 59Represents the performance test strategy. 60 61**Atomic service API**: This API can be used in atomic services since API version 20. 62 63**System capability**: SystemCapability.Test.PerfTest 64 65| Name| Type | Read-Only| Optional| Description | 66| ---- | ------ | ---- | ---- |-----------| 67| metrics | Array\<[PerfMetric](#perfmetric)> | No| No| List of performance metrics to test. | 68| actionCode | Callback\<Callback\<boolean>> | No| No| Code segment to test. | 69| resetCode | Callback\<Callback\<boolean>> | No| Yes| Code segment for resetting the environment after the test is complete. The default value is empty. This code segment is not executed when the framework is running. | 70| bundleName | string | No| Yes| Bundle name of the application to test. The default value is "". The framework tests the performance data of the current application. | 71| iterations | number | No| Yes| Number of test iterations. The default value is 5. | 72| timeout | number | No| Yes| Timeout interval for executing a code segment (**actionCode**/**resetCode**) at a time. The default value is 10,000 ms. | 73 74> **NOTE** 75> 76> The input parameter type of the **actionCode** and **resetCode** attributes is the **Callback\<boolean>**. You need to call this callback in the code segment to notify the framework that the code segment execution is complete. Otherwise, the code segment execution times out. 77> The callback parameter is of the **Boolean** type. The value **true** indicates that the code segment execution meets the expectation, and false indicates the opposite. For details, see [code example](#create) 78 79## PerfMeasureResult 80 81Represents the measurement result data corresponding to the performance metric. 82 83**Atomic service API**: This API can be used in atomic services since API version 20. 84 85**System capability**: SystemCapability.Test.PerfTest 86 87| Name | Type | Read-Only| Optional| Description | 88| ------ | ------ | ---- | ---- | ------------------------- | 89| metric | [PerfMetric](#perfmetric) | Yes| No| Performance metric to test. | 90| roundValues | Array\<number> | Yes| No| Measurement data value of each round of the tested performance metric. If data collection fails, the value **-1** is returned. | 91| maximum | number | Yes| No| Maximum value of the measurement data of each round (the value **-1** is excluded). | 92| minimum | number | Yes| No| Minimum value of the measurement data of each round (the value **-1** is excluded). | 93| average | number | Yes| No| Average value of the measurement data of each round (the value **-1** is excluded). | 94 95 96## PerfTest 97 98Represents the general entry of the white-box performance test framework. It provides capabilities such as test task creation, test code segment execution, data collection, and measurement result obtaining. 99 100### create 101 102static create(strategy: PerfTestStrategy): PerfTest 103 104Creates a **PerfTest** object and returns the object created. This API is a static API. 105 106**Atomic service API**: This API can be used in atomic services since API version 20. 107 108**System capability**: SystemCapability.Test.PerfTest 109 110**Parameters** 111 112| Name | Type | Mandatory| Description | 113| -------- | ------ | ---- | ------------------------------- | 114| strategy | [PerfTestStrategy](#perfteststrategy) | Yes | Performance test strategy.| 115 116**Return value** 117 118| Type| Description | 119| -------- | ---------------------- | 120| [PerfTest](#perftest) | **PerfTest** object created.| 121 122**Error codes** 123 124For details about the error codes, see [PerfTest Error Codes](errorcode-perftest.md). 125 126| Error Code| Error Message | 127| -------- | ---------------------- | 128| 32400001 | Initialization failed. | 129| 32400002 | Internal error. Possible causes: 1. IPC connection failed. 2. The object does not exist. | 130| 32400003 | Parameter verification failed. | 131| 32400007 | The API does not support concurrent calls. | 132 133**Example** 134 135```ts 136import { PerfMetric, PerfTest, PerfTestStrategy } from '@kit.TestKit'; 137 138async function demo() { 139 let metrics: Array<PerfMetric> = [PerfMetric.DURATION]; 140 let num = 0; 141 let actionCode = async (finish: Callback<boolean>) => { // Define the test code segment. The input parameter type is Callback<boolean> and the name is finish. 142 for (let index = 0; index < 10000; index++) { 143 num++; 144 } 145 finish(true); // Call the finish callback to notify that the code segment is executed successfully and as expected. 146 } 147 let resetCode = async (finish: Callback<boolean>) => { // Define the code segment for resetting the environment after the test ends. 148 num = 0; 149 finish(true); 150 } 151 let perfTestStrategy: PerfTestStrategy = { 152 metrics: metrics, 153 actionCode: actionCode, 154 resetCode: resetCode, 155 timeout: 30000, 156 iterations: 10, 157 }; 158 let perfTest: PerfTest = PerfTest.create(perfTestStrategy); // Construct a PerfTest object and create a test task. 159} 160``` 161 162### run 163 164run(): Promise\<void> 165 166Runs a performance test, iteratively executes test code segments, and collects performance data. This API uses a promise to return the result. 167 168**Atomic service API**: This API can be used in atomic services since API version 20. 169 170**System capability**: SystemCapability.Test.PerfTest 171 172**Return value** 173 174| Type| Description | 175| -------- | ---------------------- | 176| Promise\<void> | Promise that returns no value.| 177 178**Error codes** 179 180For details about the error codes, see [PerfTest Error Codes](errorcode-perftest.md). 181 182| Error Code| Error Message | 183| -------- | ---------------------- | 184| 32400002 | Internal error. Possible causes: 1. IPC connection failed. 2. The object does not exist. | 185| 32400004 | Failed to execute the callback. Possible causes: 1. An exception is thrown in the callback. 2. Callback execution timed out. | 186| 32400005 | Failed to collect metric data. | 187| 32400007 | The API does not support concurrent calls. | 188 189**Example** 190 191```ts 192import { PerfMetric, PerfTest, PerfTestStrategy } from '@kit.TestKit'; 193 194async function demo() { 195 let metrics: Array<PerfMetric> = [PerfMetric.DURATION]; 196 let num = 0; 197 let actionCode = async (finish: Callback<boolean>) => { 198 for (let index = 0; index < 10000; index++) { 199 num++; 200 } 201 finish(true); 202 } 203 let perfTestStrategy: PerfTestStrategy = { 204 metrics: metrics, 205 actionCode: actionCode 206 }; 207 let perfTest: PerfTest = PerfTest.create(perfTestStrategy); 208 await perfTest.run(); // Run the performance test. 209} 210``` 211 212### getMeasureResult 213 214getMeasureResult(metric: PerfMetric): PerfMeasureResult 215 216Obtains the measurement data of a specified performance metric. 217 218**Atomic service API**: This API can be used in atomic services since API version 20. 219 220**System capability**: SystemCapability.Test.PerfTest 221 222**Parameters** 223 224| Name | Type | Mandatory| Description | 225| -------- | ------ | ---- | ------------------------------- | 226| metric | [PerfMetric](#perfmetric) | Yes | Performance metric.| 227 228**Return value** 229 230| Type| Description | 231| -------- | ---------------------- | 232| [PerfMeasureResult](#perfmeasureresult) | Measurement result data corresponding to the performance metric.| 233 234**Error codes** 235 236For details about the error codes, see [PerfTest Error Codes](errorcode-perftest.md). 237 238| Error Code| Error Message | 239| -------- | ---------------------- | 240| 32400002 | Internal error. Possible causes: 1. IPC connection failed. 2. The object does not exist. | 241| 32400003 | Parameter verification failed. | 242| 32400006 | Failed to obtain the measurement result. | 243| 32400007 | The API does not support concurrent calls. | 244 245**Example** 246 247```ts 248import { PerfMetric, PerfTest, PerfTestStrategy } from '@kit.TestKit'; 249 250async function demo() { 251 let metrics: Array<PerfMetric> = [PerfMetric.DURATION]; 252 let num = 0; 253 let actionCode = async (finish: Callback<boolean>) => { 254 for (let index = 0; index < 10000; index++) { 255 num++; 256 } 257 finish(true); 258 } 259 let perfTestStrategy: PerfTestStrategy = { 260 metrics: metrics, 261 actionCode: actionCode 262 }; 263 let perfTest: PerfTest = PerfTest.create(perfTestStrategy); 264 await perfTest.run(); 265 let res = perfTest.getMeasureResult(PerfMetric.DURATION); // Obtain the measurement data of a specified performance metric. 266} 267``` 268 269### destroy 270 271destroy(): void 272 273Destroys the **PerfTest** object. 274 275**Atomic service API**: This API can be used in atomic services since API version 20. 276 277**System capability**: SystemCapability.Test.PerfTest 278 279**Error codes** 280 281For details about the error codes, see [PerfTest Error Codes](errorcode-perftest.md). 282 283| Error Code| Error Message | 284| -------- | ---------------------- | 285| 32400002 | Internal error. Possible causes: 1. IPC connection failed. 2. The object does not exist. | 286| 32400007 | The API does not support concurrent calls. | 287 288**Example** 289 290```ts 291import { PerfMetric, PerfTest, PerfTestStrategy } from '@kit.TestKit'; 292 293async function demo() { 294 let metrics: Array<PerfMetric> = [PerfMetric.DURATION]; 295 let num = 0; 296 let actionCode = async (finish: Callback<boolean>) => { 297 for (let index = 0; index < 10000; index++) { 298 num++; 299 } 300 finish(true); 301 } 302 let perfTestStrategy: PerfTestStrategy = { 303 metrics: metrics, 304 actionCode: actionCode 305 }; 306 let perfTest: PerfTest = PerfTest.create(perfTestStrategy); 307 await perfTest.run(); 308 perfTest.destroy(); // Destroy the PerfTest object. 309} 310``` 311