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 temp = hidebug.getSharedDirty(); 124 expect(temp>=BigInt(0)).assertTrue(); 125 } catch (error) { 126 expect().assertFail(); 127 } 128 console.log('************* DFX_DFR_Hiprofiler_Interface_1200 Test end*************'); 129 }); 130 131 /* 132 * @tc.number : DFX_DFR_Hiprofiler_Interface_1600 133 * @tc.name : startProfiling with normal parameter 134 * @tc.desc : Start CPU Profiling. 135 * @tc.number: test_1600 136 * @tc.level: Level 2 137 * @tc.size: MediumTest 138 * @tc.type: Function 139 */ 140 it('DFX_DFR_Hiprofiler_Interface_1600', 0, function() { 141 console.log('************* DFX_DFR_Hiprofiler_Interface_1600 Test start*************'); 142 try { 143 let timestamp = Date.now(); 144 let filename = 'cpuprofiler-' + timestamp.toString(); 145 console.info(filename); 146 let path = '/data/app/el2/100/base/com.hidebug.test/files/' + filename + '.json' 147 hidebug.startProfiling(filename); 148 let temp = 100; 149 hidebug.stopProfiling(); 150 console.info('file is exists:', path); 151 expect(true).assertTrue(); 152 } catch (error) { 153 expect().assertFail(); 154 } 155 console.log('************* DFX_DFR_Hiprofiler_Interface_1600 Test end*************'); 156 }); 157 158 /* 159 * @tc.number : DFX_DFR_Hiprofiler_Interface_1500 160 * @tc.name : startProfiling repeatedly 161 * @tc.desc : Start CPU Profiling. 162 * @tc.number: test_1500 163 * @tc.level: Level 2 164 * @tc.size: MediumTest 165 * @tc.type: Function 166 */ 167 it('DFX_DFR_Hiprofiler_Interface_1500', 0, function() { 168 console.log('************* DFX_DFR_Hiprofiler_Interface_1500 Test start*************'); 169 try { 170 let path1 = '/data/app/el2/100/base/com.hidebug.test/files/cpuprofiler-111.json' 171 let path2 = '/data/app/el2/100/base/com.hidebug.test/files/cpuprofiler-222.json' 172 let path3 = '/data/app/el2/100/base/com.hidebug.test/files/cpuprofiler-333.json' 173 hidebug.startProfiling('cpuprofiler-111'); 174 hidebug.startProfiling('cpuprofiler-222'); 175 hidebug.startProfiling('cpuprofiler-333'); 176 let temp = 100; 177 hidebug.stopProfiling(); 178 hidebug.stopProfiling(); 179 console.info('file is exists:', path1); 180 console.info('file is exists:', path2); 181 console.info('file is exists:', path3); 182 expect(true).assertTrue(); 183 } catch (error) { 184 expect().assertFail(); 185 } 186 console.log('************* DFX_DFR_Hiprofiler_Interface_1500 Test end*************'); 187 }); 188 189 /* 190 * @tc.number : DFX_DFR_Hiprofiler_Interface_1400 191 * @tc.name : startProfiling with empty parameter 192 * @tc.desc : Start CPU Profiling. 193 * @tc.number: test_1400 194 * @tc.level: Level 2 195 * @tc.size: MediumTest 196 * @tc.type: Function 197 */ 198 it('DFX_DFR_Hiprofiler_Interface_1400', 0, function() { 199 console.log('************* DFX_DFR_Hiprofiler_Interface_1400 Test start*************'); 200 let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.json' 201 try { 202 fileio.accessSync(path, 0); 203 let res = fileio.unlinkSync(path); 204 console.log(JSON.stringify(res)); 205 } catch(e) { 206 console.info('the file is not exists'); 207 } 208 try { 209 hidebug.startProfiling(''); 210 let temp = 100; 211 hidebug.stopProfiling(); 212 console.info('file is exists:', path); 213 expect(true).assertTrue(); 214 } catch (error) { 215 expect().assertFail(); 216 } 217 console.log('************* DFX_DFR_Hiprofiler_Interface_1400 Test end*************'); 218 }); 219 220 /* 221 * @tc.number : DFX_DFR_Hiprofiler_Interface_1300 222 * @tc.name : startProfiling with overlong parameter 223 * @tc.desc : Start CPU Profiling. 224 * @tc.number: test_1300 225 * @tc.level: Level 2 226 * @tc.size: MediumTest 227 * @tc.type: Function 228 */ 229 it('DFX_DFR_Hiprofiler_Interface_1300', 0, function() { 230 console.log('************* DFX_DFR_Hiprofiler_Interface_1300 Test start*************'); 231 let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.json' 232 try { 233 fileio.accessSync(path, 0); 234 let res = fileio.unlinkSync(path); 235 console.log(JSON.stringify(res)); 236 } catch(e) { 237 console.info('the file is not exists'); 238 } 239 let str = ''; 240 for (let i = 0; i < 1000; i++) { 241 str += 'abcd'; 242 } 243 try { 244 hidebug.startProfiling(str); 245 let temp = 100; 246 hidebug.stopProfiling(); 247 console.info('file is exists:', path); 248 expect(true).assertTrue(); 249 } catch (error) { 250 expect().assertFail(); 251 } 252 console.log('************* DFX_DFR_Hiprofiler_Interface_1300 Test end*************'); 253 }); 254 255 /* 256 * @tc.number : DFX_DFR_Hiprofiler_Interface_0300 257 * @tc.name : dumpHeapData with normal parameter 258 * @tc.desc : Dump JS Virtual Machine Heap Snapshot. 259 * @tc.number: test_0300 260 * @tc.level: Level 2 261 * @tc.size: MediumTest 262 * @tc.type: Function 263 */ 264 it('DFX_DFR_Hiprofiler_Interface_0300', 0, function() { 265 console.log('************* DFX_DFR_Hiprofiler_Interface_0300 Test start*************'); 266 try { 267 let timestamp = Date.now(); 268 let filename = 'heap-' + timestamp.toString(); 269 console.info(filename); 270 let path = '/data/app/el2/100/base/com.hidebug.test/files/' + filename + '.heapsnapshot' 271 hidebug.dumpHeapData(filename); 272 console.info('file is exists:', path); 273 expect(true).assertTrue(); 274 } catch (error) { 275 expect().assertFail(); 276 } 277 console.log('************* DFX_DFR_Hiprofiler_Interface_0300 Test end*************'); 278 }); 279 280 /* 281 * @tc.number : DFX_DFR_Hiprofiler_Interface_0200 282 * @tc.name : dumpHeapData with empty parameter 283 * @tc.desc : Dump JS Virtual Machine Heap Snapshot. 284 * @tc.number: test_0200 285 * @tc.level: Level 2 286 * @tc.size: MediumTest 287 * @tc.type: Function 288 */ 289 it('DFX_DFR_Hiprofiler_Interface_0200', 0, function() { 290 console.log('************* DFX_DFR_Hiprofiler_Interface_0200 Test start*************'); 291 let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.heapsnapshot' 292 try { 293 fileio.accessSync(path, 0); 294 let res = fileio.unlinkSync(path); 295 console.log(JSON.stringify(res)); 296 } catch(e) { 297 console.info('the file is not exists'); 298 } 299 try { 300 hidebug.dumpHeapData(''); 301 console.info('file is exists:', path); 302 expect(true).assertTrue(); 303 } catch (error) { 304 expect().assertFail(); 305 } 306 console.log('************* DFX_DFR_Hiprofiler_Interface_0200 Test end*************'); 307 }); 308 309 /* 310 * @tc.number : DFX_DFR_Hiprofiler_Interface_0100 311 * @tc.name : dumpHeapData with overlog parameter 312 * @tc.desc : Dump JS Virtual Machine Heap Snapshot. 313 * @tc.number: test_0100 314 * @tc.level: Level 2 315 * @tc.size: MediumTest 316 * @tc.type: Function 317 */ 318 it('DFX_DFR_Hiprofiler_Interface_0100', 0, function() { 319 console.log('************* DFX_DFR_Hiprofiler_Interface_0100 Test start*************'); 320 let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.heapsnapshot' 321 try { 322 fileio.accessSync(path, 0); 323 let res = fileio.unlinkSync(path); 324 console.log(JSON.stringify(res)); 325 } catch(e) { 326 console.info('the file is not exists'); 327 } 328 let str = ''; 329 for (let i = 0; i < 1000; i++) { 330 str += 'abcd'; 331 } 332 try { 333 hidebug.dumpHeapData(str); 334 console.info('file is exists:', path); 335 expect(true).assertTrue(); 336 } catch (error) { 337 expect().assertFail(); 338 } 339 console.log('************* DFX_DFR_Hiprofiler_Interface_0100 Test end*************'); 340 }); 341 342 /* 343 * @tc.number : DFX_DFR_Hiprofiler_Interface_0700 344 * @tc.name : getPrivateDirty 345 * @tc.desc : Get the size of the private dirty memory 346 * @tc.number: test_0700 347 * @tc.level: Level 2 348 * @tc.size: MediumTest 349 * @tc.type: Function 350 */ 351 it('DFX_DFR_Hiprofiler_Interface_0700', 0, function() { 352 console.log('************* DFX_DFR_Hiprofiler_Interface_0700 Test start*************'); 353 try { 354 let temp = hidebug.getPrivateDirty(); 355 expect(temp>=BigInt(0)).assertTrue(); 356 } catch (error) { 357 expect().assertFail(); 358 } 359 console.log('************* DFX_DFR_Hiprofiler_Interface_0700 Test end*************'); 360 }); 361 362 /* 363 * @tc.number : DFX_DFR_Hiprofiler_Interface_1700 364 * @tc.name : getCpuUsage 365 * @tc.desc : Get the cpu usage of a process 366 * @tc.number: test_1700 367 * @tc.level: Level 2 368 * @tc.size: MediumTest 369 * @tc.type: Function 370 */ 371 it('DFX_DFR_Hiprofiler_Interface_1700', 0, function() { 372 console.log('************* DFX_DFR_Hiprofiler_Interface_1700 Test start*************'); 373 try { 374 let temp = -1; 375 for(let i=0;i<=101;i++){ 376 for(let j=0;j<=101;j++){ 377 if(i+j == 202){ 378 temp = hidebug.getCpuUsage(); 379 console.info("CpuUsage is " + temp); 380 } 381 } 382 } 383 expect(temp>=0 && temp<=1).assertTrue(); 384 } catch (error) { 385 expect().assertFail(); 386 } 387 console.log('************* DFX_DFR_Hiprofiler_Interface_1700 Test end*************'); 388 }); 389 390 /* 391 * @tc.number : DFX_DFR_Hiprofiler_Interface_1100 392 * @tc.name : getServiceDump with normal parameter 393 * @tc.desc : getServiceDump 394 * @tc.number: test_1100 395 * @tc.level: Level 2 396 * @tc.size: MediumTest 397 * @tc.type: Function 398 */ 399 it('DFX_DFR_Hiprofiler_Interface_1100', 0, function() { 400 console.log('************* DFX_DFR_Hiprofiler_Interface_1100 Test start*************'); 401 let context = featureAbility.getContext(); 402 context.getFilesDir().then((data) => { 403 var path = data + "/serviceInfo1.txt" 404 let fd = fileio.openSync(path, 0o102, 0o666) 405 var serviceId = 10 406 var args = new Array("allInfo") 407 try { 408 hidebug.getServiceDump(serviceId, fd, args) 409 expect(true).assertTrue(); 410 } catch (error) { 411 expect().assertFail(); 412 console.info(error.code) 413 console.info(error.message) 414 } 415 fileio.closeSync(fd); 416 }) 417 console.log('************* DFX_DFR_Hiprofiler_Interface_1100 Test end*************'); 418 }); 419 420 /* 421 * @tc.number : DFX_DFR_Hiprofiler_Interface_0900 422 * @tc.name : getServiceDump with parameter error 423 * @tc.desc : getServiceDump 424 * @tc.number: test_0900 425 * @tc.level: Level 2 426 * @tc.size: MediumTest 427 * @tc.type: Function 428 */ 429 it('DFX_DFR_Hiprofiler_Interface_0900', 0, function() { 430 console.log('************* DFX_DFR_Hiprofiler_Interface_0900 Test start*************'); 431 let context = featureAbility.getContext(); 432 context.getFilesDir().then((data) => { 433 var path = data + "/serviceInfo2.txt" 434 let fd = fileio.openSync(path, 0o102, 0o666) 435 var serviceId = 10 436 var args = new Array("allInfo") 437 try { 438 hidebug.getServiceDump(serviceId) 439 expect().assertFail(); 440 } catch (error) { 441 console.info(error.code) 442 console.info(error.message) 443 expect(error.code == 401).assertTrue(); 444 } 445 fileio.closeSync(fd); 446 }) 447 console.log('************* DFX_DFR_Hiprofiler_Interface_0900 Test end*************'); 448 }); 449 450 /* 451 * @tc.number : DFX_DFR_Hiprofiler_Interface_1000 452 * @tc.name : getServiceDump with check system ability failed 453 * @tc.desc : getServiceDump 454 * @tc.number: test_1000 455 * @tc.level: Level 2 456 * @tc.size: MediumTest 457 * @tc.type: Function 458 */ 459 it('DFX_DFR_Hiprofiler_Interface_1000', 0, function() { 460 console.log('************* DFX_DFR_Hiprofiler_Interface_1000 Test start*************'); 461 let context = featureAbility.getContext(); 462 context.getFilesDir().then((data) => { 463 var path = data + "/serviceInfo3.txt" 464 let fd = fileio.openSync(path, 0o102, 0o666) 465 var serviceId = -10 466 var args = new Array("allInfo") 467 try { 468 hidebug.getServiceDump(serviceId, fd, args) 469 expect().assertFail(); 470 } catch (error) { 471 console.info(error.code) 472 console.info(error.message) 473 expect(error.code == 11400101).assertTrue(); 474 } 475 fileio.closeSync(fd); 476 }) 477 console.log('************* DFX_DFR_Hiprofiler_Interface_1000 Test end*************'); 478 }); 479 480 /* 481 * @tc.number : DFX_DFR_Hiprofiler_Interface_1800 482 * @tc.name : startJsCpuProfiling/stopJsCpuProfiling with normal parameter 483 * @tc.desc : startJsCpuProfiling/stopJsCpuProfiling 484 * @tc.number: test_1800 485 * @tc.level: Level 2 486 * @tc.size: MediumTest 487 * @tc.type: Function 488 */ 489 it('DFX_DFR_Hiprofiler_Interface_1800', 0, function() { 490 console.log('************* DFX_DFR_Hiprofiler_Interface_1800 Test start*************'); 491 try { 492 let timestamp = Date.now(); 493 let filename = "cpuprofiler_" + timestamp.toString(); 494 hidebug.startJsCpuProfiling(filename); 495 for (var i = 0; i < 3; i++) { 496 hidebug.getSharedDirty(); 497 } 498 hidebug.stopJsCpuProfiling(); 499 let path = "/proc/self/root/data/storage/el2/base/files/" + filename + ".json"; 500 let data = fileio.readTextSync(path); 501 if (data.includes("napi")) { 502 expect(true).assertTrue(); 503 } else { 504 expect(false).assertTrue(); 505 } 506 } catch (err) { 507 console.error('DFX_DFR_Hiprofiler_Interface_1800 has failed for ' + err); 508 expect(false).assertTrue(); 509 } 510 console.log('************* DFX_DFR_Hiprofiler_Interface_1800 Test end*************'); 511 }); 512 513 /* 514 * @tc.number : DFX_DFR_Hiprofiler_Interface_1900 515 * @tc.name : startJsCpuProfiling/stopJsCpuProfiling with abnormal parameter 516 * @tc.desc : startJsCpuProfiling/stopJsCpuProfiling 517 * @tc.number: test_1900 518 * @tc.level: Level 2 519 * @tc.size: MediumTest 520 * @tc.type: Function 521 */ 522 it('DFX_DFR_Hiprofiler_Interface_1900', 0, function() { 523 console.log('************* DFX_DFR_Hiprofiler_Interface_1900 Test start*************'); 524 try { 525 hidebug.startJsCpuProfiling(); 526 for (var i = 0; i < 3; i++) { 527 hidebug.getSharedDirty(); 528 } 529 hidebug.stopJsCpuProfiling(); 530 } catch (error) { 531 console.info(error.code); 532 console.info(error.message); 533 expect(error.code == 401).assertTrue(); 534 } 535 console.log('************* DFX_DFR_Hiprofiler_Interface_1900 Test end*************'); 536 }); 537 538 /* 539 * @tc.number : DFX_DFR_Hiprofiler_Interface_2000 540 * @tc.name : dumpJsHeapData with normal parameter 541 * @tc.desc : dumpJsHeapData 542 * @tc.number: test_2000 543 * @tc.level: Level 2 544 * @tc.size: MediumTest 545 * @tc.type: Function 546 */ 547 it('DFX_DFR_Hiprofiler_Interface_2000', 0, function() { 548 console.log('************* DFX_DFR_Hiprofiler_Interface_2000 Test start*************'); 549 try { 550 hidebug.dumpJsHeapData("heapData"); 551 expect(true).assertTrue(); 552 } catch (error) { 553 console.info(error.code); 554 console.info(error.message); 555 } 556 console.log('************* DFX_DFR_Hiprofiler_Interface_2000 Test end*************'); 557 }); 558 559 /* 560 * @tc.number : DFX_DFR_Hiprofiler_Interface_2100 561 * @tc.name : dumpJsHeapData with abnormal parameter 562 * @tc.desc : dumpJsHeapData 563 * @tc.number: test_2100 564 * @tc.level: Level 2 565 * @tc.size: MediumTest 566 * @tc.type: Function 567 */ 568 it('DFX_DFR_Hiprofiler_Interface_2100', 0, function() { 569 console.log('************* DFX_DFR_Hiprofiler_Interface_2100 Test start*************'); 570 try { 571 hidebug.dumpJsHeapData(); 572 } catch (error) { 573 console.info(error.code); 574 console.info(error.message); 575 expect(error.code == 401).assertTrue(); 576 } 577 console.log('************* DFX_DFR_Hiprofiler_Interface_2100 Test end*************'); 578 }); 579 580 /* 581 * @tc.number : DFX_DFR_Hiprofiler_Interface_2200 582 * @tc.name : dumpJsHeapData with abnormal parameter 583 * @tc.desc : dumpJsHeapData 584 * @tc.number: test_2200 585 * @tc.level: Level 2 586 * @tc.size: MediumTest 587 * @tc.type: Function 588 */ 589 it('DFX_DFR_Hiprofiler_Interface_2200', 0, function() { 590 console.log('************* DFX_DFR_Hiprofiler_Interface_2200 Test start*************'); 591 try { 592 let temp = hidebug.getVss(); 593 expect(temp>=BigInt(0)).assertTrue(); 594 } catch (error) { 595 expect().assertFail(); 596 } 597 console.log('************* DFX_DFR_Hiprofiler_Interface_2200 Test end*************'); 598 }); 599 600}) 601} 602