1/* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import hidebug from '@ohos.hidebug' 16import fileio from '@ohos.fileio' 17import process from '@ohos.process' 18import featureAbility from '@ohos.ability.featureAbility' 19import {describe, it, expect} from '@ohos/hypium' 20 21export default function HidebugJsTest() { 22describe('HidebugJsTest', function () { 23 24 /* 25 * @tc.number : DFX_DFR_Hiprofiler_Interface_0600 26 * @tc.name : getNativeHeapSize 27 * @tc.desc : Get total native heap memory size 28 * @tc.number: test_0600 29 * @tc.level: Level 2 30 * @tc.size: MediumTest 31 * @tc.type: Function 32 */ 33 it('DFX_DFR_Hiprofiler_Interface_0600', 0, function() { 34 console.log('************* DFX_DFR_Hiprofiler_Interface_0600 Test start*************'); 35 try { 36 let heapSize = hidebug.getNativeHeapSize(); 37 expect(heapSize>=BigInt(0)).assertTrue(); 38 } catch (error) { 39 expect().assertFail(); 40 } 41 console.log('************* DFX_DFR_Hiprofiler_Interface_0600 Test end*************'); 42 }); 43 44 /* 45 * @tc.number : DFX_DFR_Hiprofiler_Interface_0400 46 * @tc.name : getNativeHeapAllocatedSize 47 * @tc.desc : Get Native heap memory allocation size. 48 * @tc.number: test_0400 49 * @tc.level: Level 2 50 * @tc.size: MediumTest 51 * @tc.type: Function 52 */ 53 it('DFX_DFR_Hiprofiler_Interface_0400', 0, function() { 54 console.log('************* DFX_DFR_Hiprofiler_Interface_0400 Test start*************'); 55 try { 56 let heapAllocatedSize = hidebug.getNativeHeapAllocatedSize(); 57 expect(heapAllocatedSize>=BigInt(0)).assertTrue(); 58 } catch (error) { 59 expect().assertFail(); 60 } 61 console.log('************* DFX_DFR_Hiprofiler_Interface_0400 Test end*************'); 62 }); 63 64 /* 65 * @tc.number : DFX_DFR_Hiprofiler_Interface_0500 66 * @tc.name : getNativeHeapFreeSize 67 * @tc.desc : Get Native heap memory free size 68 * @tc.number: test_0500 69 * @tc.level: Level 2 70 * @tc.size: MediumTest 71 * @tc.type: Function 72 */ 73 it('DFX_DFR_Hiprofiler_Interface_0500', 0, function() { 74 console.log('************* DFX_DFR_Hiprofiler_Interface_0500 Test start*************'); 75 try { 76 let heapFreeSize = hidebug.getNativeHeapFreeSize(); 77 expect(heapFreeSize>=BigInt(0)).assertTrue(); 78 } catch (error) { 79 expect().assertFail(); 80 } 81 console.log('************* DFX_DFR_Hiprofiler_Interface_0500 Test end*************'); 82 }); 83 84 /* 85 * @tc.number : DFX_DFR_Hiprofiler_Interface_0800 86 * @tc.name : getPss 87 * @tc.desc : Get application process proportional set size memory information 88 * @tc.number: test_0800 89 * @tc.level: Level 2 90 * @tc.size: MediumTest 91 * @tc.type: Function 92 */ 93 it('DFX_DFR_Hiprofiler_Interface_0800', 0, function() { 94 console.log('************* DFX_DFR_Hiprofiler_Interface_0800 Test start*************'); 95 try { 96 const times = 3; 97 let pssValue = BigInt(0); 98 for(let i = 0; i < times; i++){ 99 let arr = new Array(i*100000).fill(1); 100 let tmp = hidebug.getPss(); 101 console.info('No.' + i + ' tmp: ' + tmp); 102 expect(tmp).assertLarger(pssValue); 103 pssValue = tmp; 104 } 105 } catch (error) { 106 expect().assertFail(); 107 } 108 console.log('************* DFX_DFR_Hiprofiler_Interface_0800 Test end*************'); 109 }); 110 111 /* 112 * @tc.number : DFX_DFR_Hiprofiler_Interface_1200 113 * @tc.name : getSharedDirty 114 * @tc.desc : Obtains the size of the shared dirty memory of a process. 115 * @tc.number: test_1200 116 * @tc.level: Level 2 117 * @tc.size: MediumTest 118 * @tc.type: Function 119 */ 120 it('DFX_DFR_Hiprofiler_Interface_1200', 0, function() { 121 console.log('************* DFX_DFR_Hiprofiler_Interface_1200 Test start*************'); 122 try { 123 let dirty = hidebug.getSharedDirty(); 124 let temp = hidebug.getSharedDirty(); 125 expect(dirty).assertEqual(temp); 126 } catch (error) { 127 expect().assertFail(); 128 } 129 console.log('************* DFX_DFR_Hiprofiler_Interface_1200 Test end*************'); 130 }); 131 132 /* 133 * @tc.number : DFX_DFR_Hiprofiler_Interface_1600 134 * @tc.name : startProfiling with normal parameter 135 * @tc.desc : Start CPU Profiling. 136 * @tc.number: test_1600 137 * @tc.level: Level 2 138 * @tc.size: MediumTest 139 * @tc.type: Function 140 */ 141 it('DFX_DFR_Hiprofiler_Interface_1600', 0, function() { 142 console.log('************* DFX_DFR_Hiprofiler_Interface_1600 Test start*************'); 143 try { 144 let timestamp = Date.now(); 145 let filename = 'cpuprofiler-' + timestamp.toString(); 146 console.info(filename); 147 let path = '/data/app/el2/100/base/com.hidebug.test/files/' + filename + '.json' 148 hidebug.startProfiling(filename); 149 let temp = 100; 150 hidebug.stopProfiling(); 151 console.info('file is exists:', path); 152 expect(true).assertTrue(); 153 } catch (error) { 154 expect().assertFail(); 155 } 156 console.log('************* DFX_DFR_Hiprofiler_Interface_1600 Test end*************'); 157 }); 158 159 /* 160 * @tc.number : DFX_DFR_Hiprofiler_Interface_1500 161 * @tc.name : startProfiling repeatedly 162 * @tc.desc : Start CPU Profiling. 163 * @tc.number: test_1500 164 * @tc.level: Level 2 165 * @tc.size: MediumTest 166 * @tc.type: Function 167 */ 168 it('DFX_DFR_Hiprofiler_Interface_1500', 0, function() { 169 console.log('************* DFX_DFR_Hiprofiler_Interface_1500 Test start*************'); 170 try { 171 let path1 = '/data/app/el2/100/base/com.hidebug.test/files/cpuprofiler-111.json' 172 let path2 = '/data/app/el2/100/base/com.hidebug.test/files/cpuprofiler-222.json' 173 let path3 = '/data/app/el2/100/base/com.hidebug.test/files/cpuprofiler-333.json' 174 hidebug.startProfiling('cpuprofiler-111'); 175 hidebug.startProfiling('cpuprofiler-222'); 176 hidebug.startProfiling('cpuprofiler-333'); 177 let temp = 100; 178 hidebug.stopProfiling(); 179 hidebug.stopProfiling(); 180 console.info('file is exists:', path1); 181 console.info('file is exists:', path2); 182 console.info('file is exists:', path3); 183 expect(true).assertTrue(); 184 } catch (error) { 185 expect().assertFail(); 186 } 187 console.log('************* DFX_DFR_Hiprofiler_Interface_1500 Test end*************'); 188 }); 189 190 /* 191 * @tc.number : DFX_DFR_Hiprofiler_Interface_1400 192 * @tc.name : startProfiling with empty parameter 193 * @tc.desc : Start CPU Profiling. 194 * @tc.number: test_1400 195 * @tc.level: Level 2 196 * @tc.size: MediumTest 197 * @tc.type: Function 198 */ 199 it('DFX_DFR_Hiprofiler_Interface_1400', 0, function() { 200 console.log('************* DFX_DFR_Hiprofiler_Interface_1400 Test start*************'); 201 let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.json' 202 try { 203 fileio.accessSync(path, 0); 204 let res = fileio.unlinkSync(path); 205 console.log(JSON.stringify(res)); 206 } catch(e) { 207 console.info('the file is not exists'); 208 } 209 try { 210 hidebug.startProfiling(''); 211 let temp = 100; 212 hidebug.stopProfiling(); 213 console.info('file is exists:', path); 214 expect(true).assertTrue(); 215 } catch (error) { 216 expect().assertFail(); 217 } 218 console.log('************* DFX_DFR_Hiprofiler_Interface_1400 Test end*************'); 219 }); 220 221 /* 222 * @tc.number : DFX_DFR_Hiprofiler_Interface_1300 223 * @tc.name : startProfiling with overlong parameter 224 * @tc.desc : Start CPU Profiling. 225 * @tc.number: test_1300 226 * @tc.level: Level 2 227 * @tc.size: MediumTest 228 * @tc.type: Function 229 */ 230 it('DFX_DFR_Hiprofiler_Interface_1300', 0, function() { 231 console.log('************* DFX_DFR_Hiprofiler_Interface_1300 Test start*************'); 232 let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.json' 233 try { 234 fileio.accessSync(path, 0); 235 let res = fileio.unlinkSync(path); 236 console.log(JSON.stringify(res)); 237 } catch(e) { 238 console.info('the file is not exists'); 239 } 240 let str = ''; 241 for (let i = 0; i < 1000; i++) { 242 str += 'abcd'; 243 } 244 try { 245 hidebug.startProfiling(str); 246 let temp = 100; 247 hidebug.stopProfiling(); 248 console.info('file is exists:', path); 249 expect(true).assertTrue(); 250 } catch (error) { 251 expect().assertFail(); 252 } 253 console.log('************* DFX_DFR_Hiprofiler_Interface_1300 Test end*************'); 254 }); 255 256 /* 257 * @tc.number : DFX_DFR_Hiprofiler_Interface_0300 258 * @tc.name : dumpHeapData with normal parameter 259 * @tc.desc : Dump JS Virtual Machine Heap Snapshot. 260 * @tc.number: test_0300 261 * @tc.level: Level 2 262 * @tc.size: MediumTest 263 * @tc.type: Function 264 */ 265 it('DFX_DFR_Hiprofiler_Interface_0300', 0, function() { 266 console.log('************* DFX_DFR_Hiprofiler_Interface_0300 Test start*************'); 267 try { 268 let timestamp = Date.now(); 269 let filename = 'heap-' + timestamp.toString(); 270 console.info(filename); 271 let path = '/data/app/el2/100/base/com.hidebug.test/files/' + filename + '.heapsnapshot' 272 hidebug.dumpHeapData(filename); 273 console.info('file is exists:', path); 274 expect(true).assertTrue(); 275 } catch (error) { 276 expect().assertFail(); 277 } 278 console.log('************* DFX_DFR_Hiprofiler_Interface_0300 Test end*************'); 279 }); 280 281 /* 282 * @tc.number : DFX_DFR_Hiprofiler_Interface_0200 283 * @tc.name : dumpHeapData with empty parameter 284 * @tc.desc : Dump JS Virtual Machine Heap Snapshot. 285 * @tc.number: test_0200 286 * @tc.level: Level 2 287 * @tc.size: MediumTest 288 * @tc.type: Function 289 */ 290 it('DFX_DFR_Hiprofiler_Interface_0200', 0, function() { 291 console.log('************* DFX_DFR_Hiprofiler_Interface_0200 Test start*************'); 292 let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.heapsnapshot' 293 try { 294 fileio.accessSync(path, 0); 295 let res = fileio.unlinkSync(path); 296 console.log(JSON.stringify(res)); 297 } catch(e) { 298 console.info('the file is not exists'); 299 } 300 try { 301 hidebug.dumpHeapData(''); 302 console.info('file is exists:', path); 303 expect(true).assertTrue(); 304 } catch (error) { 305 expect().assertFail(); 306 } 307 console.log('************* DFX_DFR_Hiprofiler_Interface_0200 Test end*************'); 308 }); 309 310 /* 311 * @tc.number : DFX_DFR_Hiprofiler_Interface_0100 312 * @tc.name : dumpHeapData with overlog parameter 313 * @tc.desc : Dump JS Virtual Machine Heap Snapshot. 314 * @tc.number: test_0100 315 * @tc.level: Level 2 316 * @tc.size: MediumTest 317 * @tc.type: Function 318 */ 319 it('DFX_DFR_Hiprofiler_Interface_0100', 0, function() { 320 console.log('************* DFX_DFR_Hiprofiler_Interface_0100 Test start*************'); 321 let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.heapsnapshot' 322 try { 323 fileio.accessSync(path, 0); 324 let res = fileio.unlinkSync(path); 325 console.log(JSON.stringify(res)); 326 } catch(e) { 327 console.info('the file is not exists'); 328 } 329 let str = ''; 330 for (let i = 0; i < 1000; i++) { 331 str += 'abcd'; 332 } 333 try { 334 hidebug.dumpHeapData(str); 335 console.info('file is exists:', path); 336 expect(true).assertTrue(); 337 } catch (error) { 338 expect().assertFail(); 339 } 340 console.log('************* DFX_DFR_Hiprofiler_Interface_0100 Test end*************'); 341 }); 342 343 /* 344 * @tc.number : DFX_DFR_Hiprofiler_Interface_0700 345 * @tc.name : getPrivateDirty 346 * @tc.desc : Get the size of the private dirty memory 347 * @tc.number: test_0700 348 * @tc.level: Level 2 349 * @tc.size: MediumTest 350 * @tc.type: Function 351 */ 352 it('DFX_DFR_Hiprofiler_Interface_0700', 0, function() { 353 console.log('************* DFX_DFR_Hiprofiler_Interface_0700 Test start*************'); 354 try { 355 let temp = hidebug.getPrivateDirty(); 356 expect(temp>=BigInt(0)).assertTrue(); 357 } catch (error) { 358 expect().assertFail(); 359 } 360 console.log('************* DFX_DFR_Hiprofiler_Interface_0700 Test end*************'); 361 }); 362 363 /* 364 * @tc.number : DFX_DFR_Hiprofiler_Interface_1700 365 * @tc.name : getCpuUsage 366 * @tc.desc : Get the cpu usage of a process 367 * @tc.number: test_1700 368 * @tc.level: Level 2 369 * @tc.size: MediumTest 370 * @tc.type: Function 371 */ 372 it('DFX_DFR_Hiprofiler_Interface_1700', 0, function() { 373 console.log('************* DFX_DFR_Hiprofiler_Interface_1700 Test start*************'); 374 try { 375 let temp = -1; 376 for(let i=0;i<=101;i++){ 377 for(let j=0;j<=101;j++){ 378 if(i+j == 202){ 379 temp = hidebug.getCpuUsage(); 380 console.info("CpuUsage is " + temp); 381 } 382 } 383 } 384 expect(temp>=0 && temp<=1).assertTrue(); 385 } catch (error) { 386 expect().assertFail(); 387 } 388 console.log('************* DFX_DFR_Hiprofiler_Interface_1700 Test end*************'); 389 }); 390 391 /* 392 * @tc.number : DFX_DFR_Hiprofiler_Interface_1100 393 * @tc.name : getServiceDump with normal parameter 394 * @tc.desc : getServiceDump 395 * @tc.number: test_1100 396 * @tc.level: Level 2 397 * @tc.size: MediumTest 398 * @tc.type: Function 399 */ 400 it('DFX_DFR_Hiprofiler_Interface_1100', 0, function() { 401 console.log('************* DFX_DFR_Hiprofiler_Interface_1100 Test start*************'); 402 let context = featureAbility.getContext(); 403 context.getFilesDir().then((data) => { 404 var path = data + "/serviceInfo1.txt" 405 let fd = fileio.openSync(path, 0o102, 0o666) 406 var serviceId = 10 407 var args = new Array("allInfo") 408 try { 409 hidebug.getServiceDump(serviceId, fd, args) 410 expect(true).assertTrue(); 411 } catch (error) { 412 expect().assertFail(); 413 console.info(error.code) 414 console.info(error.message) 415 } 416 fileio.closeSync(fd); 417 }) 418 console.log('************* DFX_DFR_Hiprofiler_Interface_1100 Test end*************'); 419 }); 420 421 /* 422 * @tc.number : DFX_DFR_Hiprofiler_Interface_0900 423 * @tc.name : getServiceDump with parameter error 424 * @tc.desc : getServiceDump 425 * @tc.number: test_0900 426 * @tc.level: Level 2 427 * @tc.size: MediumTest 428 * @tc.type: Function 429 */ 430 it('DFX_DFR_Hiprofiler_Interface_0900', 0, function() { 431 console.log('************* DFX_DFR_Hiprofiler_Interface_0900 Test start*************'); 432 let context = featureAbility.getContext(); 433 context.getFilesDir().then((data) => { 434 var path = data + "/serviceInfo2.txt" 435 let fd = fileio.openSync(path, 0o102, 0o666) 436 var serviceId = 10 437 var args = new Array("allInfo") 438 try { 439 hidebug.getServiceDump(serviceId) 440 expect().assertFail(); 441 } catch (error) { 442 console.info(error.code) 443 console.info(error.message) 444 expect(error.code == 401).assertTrue(); 445 } 446 fileio.closeSync(fd); 447 }) 448 console.log('************* DFX_DFR_Hiprofiler_Interface_0900 Test end*************'); 449 }); 450 451 /* 452 * @tc.number : DFX_DFR_Hiprofiler_Interface_1000 453 * @tc.name : getServiceDump with check system ability failed 454 * @tc.desc : getServiceDump 455 * @tc.number: test_1000 456 * @tc.level: Level 2 457 * @tc.size: MediumTest 458 * @tc.type: Function 459 */ 460 it('DFX_DFR_Hiprofiler_Interface_1000', 0, function() { 461 console.log('************* DFX_DFR_Hiprofiler_Interface_1000 Test start*************'); 462 let context = featureAbility.getContext(); 463 context.getFilesDir().then((data) => { 464 var path = data + "/serviceInfo3.txt" 465 let fd = fileio.openSync(path, 0o102, 0o666) 466 var serviceId = -10 467 var args = new Array("allInfo") 468 try { 469 hidebug.getServiceDump(serviceId, fd, args) 470 expect().assertFail(); 471 } catch (error) { 472 console.info(error.code) 473 console.info(error.message) 474 expect(error.code == 11400101).assertTrue(); 475 } 476 fileio.closeSync(fd); 477 }) 478 console.log('************* DFX_DFR_Hiprofiler_Interface_1000 Test end*************'); 479 }); 480 481 /* 482 * @tc.number : DFX_DFR_Hiprofiler_Interface_1800 483 * @tc.name : startJsCpuProfiling/stopJsCpuProfiling with normal parameter 484 * @tc.desc : startJsCpuProfiling/stopJsCpuProfiling 485 * @tc.number: test_1800 486 * @tc.level: Level 2 487 * @tc.size: MediumTest 488 * @tc.type: Function 489 */ 490 it('DFX_DFR_Hiprofiler_Interface_1800', 0, function() { 491 console.log('************* DFX_DFR_Hiprofiler_Interface_1800 Test start*************'); 492 try { 493 let timestamp = Date.now(); 494 let filename = "cpuprofiler_" + timestamp.toString(); 495 hidebug.startJsCpuProfiling(filename); 496 for (var i = 0; i < 3; i++) { 497 hidebug.getSharedDirty(); 498 } 499 hidebug.stopJsCpuProfiling(); 500 let path = "/proc/self/root/data/storage/el2/base/files/" + filename + ".json"; 501 let data = fileio.readTextSync(path); 502 if (data.includes("napi")) { 503 expect(true).assertTrue(); 504 } else { 505 expect(false).assertTrue(); 506 } 507 } catch (err) { 508 console.error('DFX_DFR_Hiprofiler_Interface_1800 has failed for ' + err); 509 expect(false).assertTrue(); 510 } 511 console.log('************* DFX_DFR_Hiprofiler_Interface_1800 Test end*************'); 512 }); 513 514 /* 515 * @tc.number : DFX_DFR_Hiprofiler_Interface_1900 516 * @tc.name : startJsCpuProfiling/stopJsCpuProfiling with abnormal parameter 517 * @tc.desc : startJsCpuProfiling/stopJsCpuProfiling 518 * @tc.number: test_1900 519 * @tc.level: Level 2 520 * @tc.size: MediumTest 521 * @tc.type: Function 522 */ 523 it('DFX_DFR_Hiprofiler_Interface_1900', 0, function() { 524 console.log('************* DFX_DFR_Hiprofiler_Interface_1900 Test start*************'); 525 try { 526 hidebug.startJsCpuProfiling(); 527 for (var i = 0; i < 3; i++) { 528 hidebug.getSharedDirty(); 529 } 530 hidebug.stopJsCpuProfiling(); 531 } catch (error) { 532 console.info(error.code); 533 console.info(error.message); 534 expect(error.code == 401).assertTrue(); 535 } 536 console.log('************* DFX_DFR_Hiprofiler_Interface_1900 Test end*************'); 537 }); 538 539 /* 540 * @tc.number : DFX_DFR_Hiprofiler_Interface_2000 541 * @tc.name : dumpJsHeapData with normal parameter 542 * @tc.desc : dumpJsHeapData 543 * @tc.number: test_2000 544 * @tc.level: Level 2 545 * @tc.size: MediumTest 546 * @tc.type: Function 547 */ 548 it('DFX_DFR_Hiprofiler_Interface_2000', 0, function() { 549 console.log('************* DFX_DFR_Hiprofiler_Interface_2000 Test start*************'); 550 try { 551 hidebug.dumpJsHeapData("heapData"); 552 expect(true).assertTrue(); 553 } catch (error) { 554 console.info(error.code); 555 console.info(error.message); 556 } 557 console.log('************* DFX_DFR_Hiprofiler_Interface_2000 Test end*************'); 558 }); 559 560 /* 561 * @tc.number : DFX_DFR_Hiprofiler_Interface_2100 562 * @tc.name : dumpJsHeapData with abnormal parameter 563 * @tc.desc : dumpJsHeapData 564 * @tc.number: test_2100 565 * @tc.level: Level 2 566 * @tc.size: MediumTest 567 * @tc.type: Function 568 */ 569 it('DFX_DFR_Hiprofiler_Interface_2100', 0, function() { 570 console.log('************* DFX_DFR_Hiprofiler_Interface_2100 Test start*************'); 571 try { 572 hidebug.dumpJsHeapData(); 573 } catch (error) { 574 console.info(error.code); 575 console.info(error.message); 576 expect(error.code == 401).assertTrue(); 577 } 578 console.log('************* DFX_DFR_Hiprofiler_Interface_2100 Test end*************'); 579 }); 580 581 /* 582 * @tc.number : DFX_DFR_Hiprofiler_Interface_2200 583 * @tc.name : dumpJsHeapData with abnormal parameter 584 * @tc.desc : dumpJsHeapData 585 * @tc.number: test_2200 586 * @tc.level: Level 2 587 * @tc.size: MediumTest 588 * @tc.type: Function 589 */ 590 it('DFX_DFR_Hiprofiler_Interface_2200', 0, function() { 591 console.log('************* DFX_DFR_Hiprofiler_Interface_2200 Test start*************'); 592 try { 593 let temp = hidebug.getVss(); 594 expect(temp>=BigInt(0)).assertTrue(); 595 } catch (error) { 596 expect().assertFail(); 597 } 598 console.log('************* DFX_DFR_Hiprofiler_Interface_2200 Test end*************'); 599 }); 600 601}) 602} 603