Hidebug.test.js 16.0 KB
Newer Older
U
uoengopen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import hidebug from '@ohos.hidebug'
import fileio from '@ohos.fileio'
17
import featureAbility from '@ohos.ability.featureAbility'
J
jiyong_sd 已提交
18
import {describe, it, expect} from '@ohos/hypium'
U
uoengopen 已提交
19

J
jiyong_sd 已提交
20
export default function HidebugJsTest() {
U
uoengopen 已提交
21 22 23
describe('HidebugJsTest', function () {

    /*
C
chenxuihui 已提交
24
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_0600
U
uoengopen 已提交
25 26 27
     * @tc.name      : getNativeHeapSize
     * @tc.desc      : Get total native heap memory size
     */
C
chenxuihui 已提交
28 29
    it('DFX_DFR_Hiprofiler_Interface_0600', 0, function() {
        console.log('************* DFX_DFR_Hiprofiler_Interface_0600 Test start*************');
U
uoengopen 已提交
30 31
        try {
            let heapSize = hidebug.getNativeHeapSize();
U
uoengopen 已提交
32
            expect(heapSize>=BigInt(0)).assertTrue();
U
uoengopen 已提交
33 34 35
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
36
        console.log('************* DFX_DFR_Hiprofiler_Interface_0600 Test end*************');
U
uoengopen 已提交
37 38 39
    });

    /*
C
chenxuihui 已提交
40
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_0400
U
uoengopen 已提交
41 42 43
     * @tc.name      : getNativeHeapAllocatedSize
     * @tc.desc      : Get Native heap memory allocation size.
     */
C
chenxuihui 已提交
44 45
    it('DFX_DFR_Hiprofiler_Interface_0400', 0, function() {
        console.log('************* DFX_DFR_Hiprofiler_Interface_0400 Test start*************');
U
uoengopen 已提交
46 47
        try {
            let heapAllocatedSize = hidebug.getNativeHeapAllocatedSize();
U
uoengopen 已提交
48
            expect(heapAllocatedSize>=BigInt(0)).assertTrue();
U
uoengopen 已提交
49 50 51
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
52
        console.log('************* DFX_DFR_Hiprofiler_Interface_0400 Test end*************');
U
uoengopen 已提交
53 54 55
    });

    /*
C
chenxuihui 已提交
56
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_0500
U
uoengopen 已提交
57 58 59
     * @tc.name      : getNativeHeapFreeSize
     * @tc.desc      : Get Native heap memory free size
     */
C
chenxuihui 已提交
60 61
    it('DFX_DFR_Hiprofiler_Interface_0500', 0, function() {
        console.log('************* DFX_DFR_Hiprofiler_Interface_0500 Test start*************');
U
uoengopen 已提交
62 63
        try {
            let heapFreeSize = hidebug.getNativeHeapFreeSize();
U
uoengopen 已提交
64
            expect(heapFreeSize>=BigInt(0)).assertTrue();
U
uoengopen 已提交
65 66 67
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
68
        console.log('************* DFX_DFR_Hiprofiler_Interface_0500 Test end*************');
U
uoengopen 已提交
69 70 71
    });

    /*
C
chenxuihui 已提交
72
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_0800
U
uoengopen 已提交
73 74 75
     * @tc.name      : getPss
     * @tc.desc      : Get application process proportional set size memory information
     */
76
    it('DFX_DFR_Hiprofiler_Interface_0800', 0, function() {
C
chenxuihui 已提交
77
        console.log('************* DFX_DFR_Hiprofiler_Interface_0800 Test start*************');
U
uoengopen 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90
        try {
            const times = 3;
            let pssValue = BigInt(0);
            for(let i = 0; i < times; i++){
                let arr = new Array(i*100000).fill(1);
                let tmp = hidebug.getPss();
                console.info('No.' + i + ' tmp: ' + tmp);
                expect(tmp).assertLarger(pssValue);
                pssValue = tmp;
            }
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
91
        console.log('************* DFX_DFR_Hiprofiler_Interface_0800 Test end*************');
U
uoengopen 已提交
92 93 94
    });

    /*
C
chenxuihui 已提交
95
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_1200
U
uoengopen 已提交
96 97 98
     * @tc.name      : getSharedDirty
     * @tc.desc      : Obtains the size of the shared dirty memory of a process.
     */
99
    it('DFX_DFR_Hiprofiler_Interface_1200', 0, function() {
C
chenxuihui 已提交
100
        console.log('************* DFX_DFR_Hiprofiler_Interface_1200 Test start*************');
U
uoengopen 已提交
101 102 103 104 105 106 107
        try {
            let dirty = hidebug.getSharedDirty();
            let temp = hidebug.getSharedDirty();
            expect(dirty).assertEqual(temp);
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
108
        console.log('************* DFX_DFR_Hiprofiler_Interface_1200 Test end*************');
U
uoengopen 已提交
109 110 111
    });

    /*
C
chenxuihui 已提交
112
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_1600
U
uoengopen 已提交
113 114 115
     * @tc.name      : startProfiling with normal parameter
     * @tc.desc      : Start CPU Profiling.
     */
116
    it('DFX_DFR_Hiprofiler_Interface_1600', 0, function() {
C
chenxuihui 已提交
117
        console.log('************* DFX_DFR_Hiprofiler_Interface_1600 Test start*************');
U
uoengopen 已提交
118 119 120 121
        try {
            let timestamp = Date.now();
            let filename = 'cpuprofiler-' + timestamp.toString();
            console.info(filename);
U
uoengopen 已提交
122
            let path = '/data/app/el2/100/base/com.hidebug.test/files/' + filename + '.json'
U
uoengopen 已提交
123 124 125 126 127 128 129 130
            hidebug.startProfiling(filename);
            let temp = 100;
            hidebug.stopProfiling();
            console.info('file is exists:', path);
            expect(true).assertTrue();
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
131
        console.log('************* DFX_DFR_Hiprofiler_Interface_1600 Test end*************');
U
uoengopen 已提交
132 133 134
    });

    /*
C
chenxuihui 已提交
135
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_1500
U
uoengopen 已提交
136 137 138
     * @tc.name      : startProfiling repeatedly
     * @tc.desc      : Start CPU Profiling.
     */
139
    it('DFX_DFR_Hiprofiler_Interface_1500', 0, function() {
C
chenxuihui 已提交
140
        console.log('************* DFX_DFR_Hiprofiler_Interface_1500 Test start*************');
U
uoengopen 已提交
141
        try {
U
uoengopen 已提交
142 143 144
            let path1 = '/data/app/el2/100/base/com.hidebug.test/files/cpuprofiler-111.json'
            let path2 = '/data/app/el2/100/base/com.hidebug.test/files/cpuprofiler-222.json'
            let path3 = '/data/app/el2/100/base/com.hidebug.test/files/cpuprofiler-333.json'
U
uoengopen 已提交
145 146 147 148 149 150
            hidebug.startProfiling('cpuprofiler-111');
            hidebug.startProfiling('cpuprofiler-222');
            hidebug.startProfiling('cpuprofiler-333');
            let temp = 100;
            hidebug.stopProfiling();
            hidebug.stopProfiling();
U
uoengopen 已提交
151 152 153 154
            console.info('file is exists:', path1);
            console.info('file is exists:', path2);
            console.info('file is exists:', path3);
            expect(true).assertTrue();
U
uoengopen 已提交
155 156 157
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
158
        console.log('************* DFX_DFR_Hiprofiler_Interface_1500 Test end*************');
U
uoengopen 已提交
159 160 161
    });

    /*
C
chenxuihui 已提交
162
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_1400
U
uoengopen 已提交
163 164 165
     * @tc.name      : startProfiling with empty parameter
     * @tc.desc      : Start CPU Profiling.
     */
166
    it('DFX_DFR_Hiprofiler_Interface_1400', 0, function() {
C
chenxuihui 已提交
167
        console.log('************* DFX_DFR_Hiprofiler_Interface_1400 Test start*************');
U
uoengopen 已提交
168
        let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.json'
U
uoengopen 已提交
169 170 171 172 173 174
        try {
            fileio.accessSync(path, 0);
            let res = fileio.unlinkSync(path);
            console.log(JSON.stringify(res));
        } catch(e) {
            console.info('the file is not exists');
U
uoengopen 已提交
175
        }
U
uoengopen 已提交
176 177 178 179 180 181
        try {
            hidebug.startProfiling('');
            let temp = 100;
            hidebug.stopProfiling();
            console.info('file is exists:', path);
            expect(true).assertTrue();
U
uoengopen 已提交
182 183
        } catch (error) {
            expect().assertFail();
U
uoengopen 已提交
184
        }
C
chenxuihui 已提交
185
        console.log('************* DFX_DFR_Hiprofiler_Interface_1400 Test end*************');
U
uoengopen 已提交
186 187 188
    });

    /*
C
chenxuihui 已提交
189
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_1300
U
uoengopen 已提交
190 191 192
     * @tc.name      : startProfiling with overlong parameter
     * @tc.desc      : Start CPU Profiling.
     */
193
    it('cDFX_DFR_Hiprofiler_Interface_1300', 0, function() {
C
chenxuihui 已提交
194
        console.log('************* DFX_DFR_Hiprofiler_Interface_1300 Test start*************');
U
uoengopen 已提交
195
        let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.json'
U
uoengopen 已提交
196 197 198 199 200 201
        try {
            fileio.accessSync(path, 0);
            let res = fileio.unlinkSync(path);
            console.log(JSON.stringify(res));
        } catch(e) {
            console.info('the file is not exists');
U
uoengopen 已提交
202
        }
U
uoengopen 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215
        let str = '';
        for (let i = 0; i < 1000; i++) {
            str += 'abcd';
        }
        try {
            hidebug.startProfiling(str);
            let temp = 100;
            hidebug.stopProfiling();
            console.info('file is exists:', path);
            expect(true).assertTrue();
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
216
        console.log('************* DFX_DFR_Hiprofiler_Interface_1300 Test end*************');
U
uoengopen 已提交
217 218 219
    });

    /*
C
chenxuihui 已提交
220
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_0300
U
uoengopen 已提交
221 222 223
     * @tc.name      : dumpHeapData with normal parameter
     * @tc.desc      : Dump JS Virtual Machine Heap Snapshot.
     */
224
    it('DFX_DFR_Hiprofiler_Interface_0300', 0, function() {
C
chenxuihui 已提交
225
        console.log('************* DFX_DFR_Hiprofiler_Interface_0300 Test start*************');
U
uoengopen 已提交
226 227 228 229
        try {
            let timestamp = Date.now();
            let filename = 'heap-' + timestamp.toString();
            console.info(filename);
U
uoengopen 已提交
230
            let path = '/data/app/el2/100/base/com.hidebug.test/files/' + filename + '.heapsnapshot'
U
uoengopen 已提交
231 232 233 234 235 236
            hidebug.dumpHeapData(filename);
            console.info('file is exists:', path);
            expect(true).assertTrue();
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
237
        console.log('************* DFX_DFR_Hiprofiler_Interface_0300 Test end*************');
U
uoengopen 已提交
238 239 240
      });

    /*
C
chenxuihui 已提交
241
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_0200
U
uoengopen 已提交
242 243 244
     * @tc.name      : dumpHeapData with empty parameter
     * @tc.desc      : Dump JS Virtual Machine Heap Snapshot.
     */
245
    it('DFX_DFR_Hiprofiler_Interface_0200', 0, function() {
C
chenxuihui 已提交
246
        console.log('************* DFX_DFR_Hiprofiler_Interface_0200 Test start*************');
U
uoengopen 已提交
247
        let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.heapsnapshot'
U
uoengopen 已提交
248 249 250 251 252 253
        try {
            fileio.accessSync(path, 0);
            let res = fileio.unlinkSync(path);
            console.log(JSON.stringify(res));
        } catch(e) {
            console.info('the file is not exists');
U
uoengopen 已提交
254
        }
U
uoengopen 已提交
255 256 257 258
        try {
            hidebug.dumpHeapData('');
            console.info('file is exists:', path);
            expect(true).assertTrue();
U
uoengopen 已提交
259 260
        } catch (error) {
            expect().assertFail();
U
uoengopen 已提交
261
        }
C
chenxuihui 已提交
262
        console.log('************* DFX_DFR_Hiprofiler_Interface_0200 Test end*************');
U
uoengopen 已提交
263 264 265
      });

    /*
C
chenxuihui 已提交
266
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_0100
U
uoengopen 已提交
267 268 269
     * @tc.name      : dumpHeapData with overlog parameter
     * @tc.desc      : Dump JS Virtual Machine Heap Snapshot.
     */
270
    it('DFX_DFR_Hiprofiler_Interface_0100', 0, function() {
C
chenxuihui 已提交
271
        console.log('************* DFX_DFR_Hiprofiler_Interface_0100 Test start*************');
U
uoengopen 已提交
272
        let path = '/data/app/el2/100/base/com.hidebug.test/files/undefined.heapsnapshot'
U
uoengopen 已提交
273 274 275 276 277 278
        try {
            fileio.accessSync(path, 0);
            let res = fileio.unlinkSync(path);
            console.log(JSON.stringify(res));
        } catch(e) {
            console.info('the file is not exists');
U
uoengopen 已提交
279
        }
U
uoengopen 已提交
280 281 282 283 284 285 286 287 288 289 290
        let str = '';
        for (let i = 0; i < 1000; i++) {
            str += 'abcd';
        }
        try {
            hidebug.dumpHeapData(str);
            console.info('file is exists:', path);
            expect(true).assertTrue();
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
291
        console.log('************* DFX_DFR_Hiprofiler_Interface_0100 Test end*************');
U
uoengopen 已提交
292
    });
U
uoengopen 已提交
293 294

    /*
C
chenxuihui 已提交
295
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_0700
U
uoengopen 已提交
296 297 298
     * @tc.name      : getPrivateDirty
     * @tc.desc      : Get the size of the private dirty memory
     */
C
chenxuihui 已提交
299 300
    it('DFX_DFR_Hiprofiler_Interface_0700', 0, function() {
        console.log('************* DFX_DFR_Hiprofiler_Interface_0700 Test start*************');
U
uoengopen 已提交
301 302 303 304 305 306
        try {
            let temp = hidebug.getPrivateDirty();
            expect(temp>=BigInt(0)).assertTrue();
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
307
        console.log('************* DFX_DFR_Hiprofiler_Interface_0700 Test end*************');
U
uoengopen 已提交
308 309 310
    });

    /*
C
chenxuihui 已提交
311
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_1700
U
uoengopen 已提交
312 313 314
     * @tc.name      : getCpuUsage
     * @tc.desc      : Get the cpu usage of a process
     */
C
chenxuihui 已提交
315 316
    it('DFX_DFR_Hiprofiler_Interface_1700', 0, function() {
        console.log('************* DFX_DFR_Hiprofiler_Interface_1700 Test start*************');
U
uoengopen 已提交
317 318
        try {
            let temp = -1;
U
uoengopen 已提交
319 320
            for(let i=0;i<=101;i++){
                for(let j=0;j<=101;j++){
U
uoengopen 已提交
321
                    if(i+j == 202){
U
uoengopen 已提交
322 323 324 325 326 327 328 329 330
                        temp = hidebug.getCpuUsage();
                        console.info("CpuUsage is " + temp);
                    }
                }
            }
            expect(temp>=0 && temp<=1).assertTrue();
        } catch (error) {
            expect().assertFail();
        }
C
chenxuihui 已提交
331
        console.log('************* DFX_DFR_Hiprofiler_Interface_1700 Test end*************');
U
uoengopen 已提交
332 333 334
    });

    /*
C
chenxuihui 已提交
335
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_1100
U
uoengopen 已提交
336 337 338
     * @tc.name      : getServiceDump with normal parameter
     * @tc.desc      : getServiceDump
     */
C
chenxuihui 已提交
339 340
    it('DFX_DFR_Hiprofiler_Interface_1100', 0, function() {
        console.log('************* DFX_DFR_Hiprofiler_Interface_1100 Test start*************');
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        let context = featureAbility.getContext();
        context.getFilesDir().then((data) => {
            var path = data + "/serviceInfo1.txt"
            let fd = fileio.openSync(path, 0o102, 0o666)
            var serviceId = 10
            var args = new Array("allInfo")
            try {
                hidebug.getServiceDump(serviceId, fd, args)
                expect(true).assertTrue();
            } catch (error) {
                expect().assertFail();
                console.info(error.code)
                console.info(error.message)
            }
            fileio.closeSync(fd);
        })
C
chenxuihui 已提交
357
        console.log('************* DFX_DFR_Hiprofiler_Interface_1100 Test end*************');
U
uoengopen 已提交
358 359 360
    });

    /*
C
chenxuihui 已提交
361
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_0900
362
     * @tc.name      : getServiceDump with parameter error
U
uoengopen 已提交
363 364
     * @tc.desc      : getServiceDump
     */
C
chenxuihui 已提交
365 366
    it('DFX_DFR_Hiprofiler_Interface_0900', 0, function() {
        console.log('************* DFX_DFR_Hiprofiler_Interface_0900 Test start*************');
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
        let context = featureAbility.getContext();
        context.getFilesDir().then((data) => {
            var path = data + "/serviceInfo2.txt"
            let fd = fileio.openSync(path, 0o102, 0o666)
            var serviceId = 10
            var args = new Array("allInfo")
            try {
                hidebug.getServiceDump(serviceId)
                expect().assertFail();
            } catch (error) {
                console.info(error.code)
                console.info(error.message)
                expect(error.code == 401).assertTrue();
            }
            fileio.closeSync(fd);
        })
C
chenxuihui 已提交
383
        console.log('************* DFX_DFR_Hiprofiler_Interface_0900 Test end*************');
U
uoengopen 已提交
384 385 386
    });

    /*
C
chenxuihui 已提交
387
     * @tc.number    : DFX_DFR_Hiprofiler_Interface_1000
388
     * @tc.name      : getServiceDump with check system ability failed
U
uoengopen 已提交
389 390
     * @tc.desc      : getServiceDump
     */
C
chenxuihui 已提交
391 392
    it('DFX_DFR_Hiprofiler_Interface_1000', 0, function() {
        console.log('************* DFX_DFR_Hiprofiler_Interface_1000 Test start*************');
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
        let context = featureAbility.getContext();
        context.getFilesDir().then((data) => {
            var path = data + "/serviceInfo3.txt"
            let fd = fileio.openSync(path, 0o102, 0o666)
            var serviceId = -10
            var args = new Array("allInfo")
            try {
                hidebug.getServiceDump(serviceId, fd, args)
                expect().assertFail();
            } catch (error) {
                console.info(error.code)
                console.info(error.message)
                expect(error.code == 11400101).assertTrue();
            }
            fileio.closeSync(fd);
        })
C
chenxuihui 已提交
409
        console.log('************* DFX_DFR_Hiprofiler_Interface_1000 Test end*************');
U
uoengopen 已提交
410
    });
U
uoengopen 已提交
411 412
    
})
J
jiyong_sd 已提交
413
}