1/* 2 * Copyright (c) 2024 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 */ 15 16import {assert} from 'chai'; 17import {MemoryUtils} from '../../../src/utils/MemoryUtils'; 18 19describe('test for MemoryUtils', function () { 20 const disableGC = false; 21 const allowGC = true; 22 const iniBaseMemory = undefined; 23 const lowBaseLine = 0; 24 const highBaseLine = 1000 * 1024 * 1024; // 1000MB 25 const highBase = 1000 * 1024 * 1024; // 1000MB 26 27 describe('test for method tryGC', function () { 28 it('test for allowGC === true', function () { 29 MemoryUtils.setGC(allowGC); 30 MemoryUtils.setBaseMemorySize(iniBaseMemory); 31 assert.strictEqual(MemoryUtils.getBaseMemorySize(), undefined); 32 33 // Initialize base 34 MemoryUtils.tryGC(); 35 assert.notEqual(MemoryUtils.getBaseMemorySize(), undefined); 36 37 // Trigger GC 38 MemoryUtils.setMinGCThreshold(lowBaseLine); 39 const memoryBeforeGC = process.memoryUsage().heapUsed; 40 MemoryUtils.tryGC(); 41 assert.isAbove(MemoryUtils.getBaseMemorySize(), 0); 42 assert.isBelow(MemoryUtils.getBaseMemorySize(), memoryBeforeGC); 43 44 // Lower base 45 MemoryUtils.setMinGCThreshold(highBaseLine); 46 MemoryUtils.setBaseMemorySize(highBase); 47 MemoryUtils.tryGC(); 48 assert.isBelow(MemoryUtils.getBaseMemorySize(), highBase); 49 }); 50 51 it('test for allowGC === false', function () { 52 MemoryUtils.setGC(disableGC); 53 MemoryUtils.setBaseMemorySize(iniBaseMemory); 54 assert.strictEqual(MemoryUtils.getBaseMemorySize(), undefined); 55 MemoryUtils.tryGC(); 56 assert.strictEqual(MemoryUtils.getBaseMemorySize(), undefined); 57 }); 58 59 it('test function tryGC for updateBaseMemory function', function() { 60 MemoryUtils.setGC(allowGC); 61 MemoryUtils.setMinGCThreshold(MemoryUtils.getMinGCThreshold()); 62 MemoryUtils.setBaseMemorySize(undefined); 63 MemoryUtils.tryGC(); 64 const currentMemory = MemoryUtils.getBaseMemorySize(); 65 // currentMemory * 0.3 is greater than minGCBaseline 66 if (MemoryUtils.getMinGCThreshold() < currentMemory * MemoryUtils.GC_THRESHOLD_RATIO) { 67 assert.isAbove(MemoryUtils.getGCThreshold(), MemoryUtils.getMinGCThreshold()); 68 } else { 69 assert.strictEqual(MemoryUtils.getGCThreshold(), MemoryUtils.getMinGCThreshold()); 70 } 71 }); 72 73 it('test function updateBaseMemory', function() { 74 // 0.35: make the value of memoryValue1 * 0.3 less than minGCBaseline 75 const memoryValue1: number = MemoryUtils.getMinGCThreshold() / 0.35; 76 MemoryUtils.updateBaseMemory(memoryValue1); 77 assert.strictEqual(MemoryUtils.getGCThreshold(), MemoryUtils.getMinGCThreshold()); 78 assert.strictEqual(MemoryUtils.getBaseMemorySize(), memoryValue1); 79 // 0.25: make the value of memoryValue2 * 0.3 greater than minGCBaseline 80 const memoryValue2: number = MemoryUtils.getMinGCThreshold() / 0.25; 81 MemoryUtils.updateBaseMemory(memoryValue2); 82 assert.isAbove(MemoryUtils.getGCThreshold(), MemoryUtils.getMinGCThreshold()); 83 assert.strictEqual(MemoryUtils.getBaseMemorySize(), memoryValue2); 84 }); 85 }); 86});