13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ciimport {assert} from 'chai';
173af6ab5fSopenharmony_ciimport {MemoryUtils} from '../../../src/utils/MemoryUtils';
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_cidescribe('test for MemoryUtils', function () {
203af6ab5fSopenharmony_ci  const disableGC = false;
213af6ab5fSopenharmony_ci  const allowGC = true;
223af6ab5fSopenharmony_ci  const iniBaseMemory = undefined;
233af6ab5fSopenharmony_ci  const lowBaseLine = 0;
243af6ab5fSopenharmony_ci  const highBaseLine = 1000 * 1024 * 1024; // 1000MB
253af6ab5fSopenharmony_ci  const highBase = 1000 * 1024 * 1024; // 1000MB
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_ci  describe('test for method tryGC', function () {
283af6ab5fSopenharmony_ci    it('test for allowGC === true', function () {
293af6ab5fSopenharmony_ci      MemoryUtils.setGC(allowGC);
303af6ab5fSopenharmony_ci      MemoryUtils.setBaseMemorySize(iniBaseMemory);
313af6ab5fSopenharmony_ci      assert.strictEqual(MemoryUtils.getBaseMemorySize(), undefined);
323af6ab5fSopenharmony_ci
333af6ab5fSopenharmony_ci      // Initialize base
343af6ab5fSopenharmony_ci      MemoryUtils.tryGC();
353af6ab5fSopenharmony_ci      assert.notEqual(MemoryUtils.getBaseMemorySize(), undefined);
363af6ab5fSopenharmony_ci
373af6ab5fSopenharmony_ci      // Trigger GC
383af6ab5fSopenharmony_ci      MemoryUtils.setMinGCThreshold(lowBaseLine);
393af6ab5fSopenharmony_ci      const memoryBeforeGC = process.memoryUsage().heapUsed;
403af6ab5fSopenharmony_ci      MemoryUtils.tryGC();
413af6ab5fSopenharmony_ci      assert.isAbove(MemoryUtils.getBaseMemorySize(), 0);
423af6ab5fSopenharmony_ci      assert.isBelow(MemoryUtils.getBaseMemorySize(), memoryBeforeGC);
433af6ab5fSopenharmony_ci
443af6ab5fSopenharmony_ci      // Lower base
453af6ab5fSopenharmony_ci      MemoryUtils.setMinGCThreshold(highBaseLine);
463af6ab5fSopenharmony_ci      MemoryUtils.setBaseMemorySize(highBase);
473af6ab5fSopenharmony_ci      MemoryUtils.tryGC();
483af6ab5fSopenharmony_ci      assert.isBelow(MemoryUtils.getBaseMemorySize(), highBase);
493af6ab5fSopenharmony_ci    });
503af6ab5fSopenharmony_ci
513af6ab5fSopenharmony_ci    it('test for allowGC === false', function () {
523af6ab5fSopenharmony_ci        MemoryUtils.setGC(disableGC);
533af6ab5fSopenharmony_ci        MemoryUtils.setBaseMemorySize(iniBaseMemory);
543af6ab5fSopenharmony_ci        assert.strictEqual(MemoryUtils.getBaseMemorySize(), undefined);
553af6ab5fSopenharmony_ci        MemoryUtils.tryGC();
563af6ab5fSopenharmony_ci        assert.strictEqual(MemoryUtils.getBaseMemorySize(), undefined);
573af6ab5fSopenharmony_ci    });
583af6ab5fSopenharmony_ci
593af6ab5fSopenharmony_ci    it('test function tryGC for updateBaseMemory function', function() {
603af6ab5fSopenharmony_ci      MemoryUtils.setGC(allowGC);
613af6ab5fSopenharmony_ci      MemoryUtils.setMinGCThreshold(MemoryUtils.getMinGCThreshold()); 
623af6ab5fSopenharmony_ci      MemoryUtils.setBaseMemorySize(undefined);
633af6ab5fSopenharmony_ci      MemoryUtils.tryGC();
643af6ab5fSopenharmony_ci      const currentMemory = MemoryUtils.getBaseMemorySize();
653af6ab5fSopenharmony_ci      // currentMemory * 0.3 is greater than minGCBaseline
663af6ab5fSopenharmony_ci      if (MemoryUtils.getMinGCThreshold() < currentMemory * MemoryUtils.GC_THRESHOLD_RATIO) {
673af6ab5fSopenharmony_ci        assert.isAbove(MemoryUtils.getGCThreshold(), MemoryUtils.getMinGCThreshold());
683af6ab5fSopenharmony_ci      } else {
693af6ab5fSopenharmony_ci        assert.strictEqual(MemoryUtils.getGCThreshold(), MemoryUtils.getMinGCThreshold());
703af6ab5fSopenharmony_ci      }
713af6ab5fSopenharmony_ci    });
723af6ab5fSopenharmony_ci
733af6ab5fSopenharmony_ci    it('test function updateBaseMemory', function() {
743af6ab5fSopenharmony_ci      // 0.35: make the value of memoryValue1 * 0.3 less than minGCBaseline
753af6ab5fSopenharmony_ci      const memoryValue1: number = MemoryUtils.getMinGCThreshold() / 0.35;
763af6ab5fSopenharmony_ci      MemoryUtils.updateBaseMemory(memoryValue1); 
773af6ab5fSopenharmony_ci      assert.strictEqual(MemoryUtils.getGCThreshold(), MemoryUtils.getMinGCThreshold());
783af6ab5fSopenharmony_ci      assert.strictEqual(MemoryUtils.getBaseMemorySize(), memoryValue1);
793af6ab5fSopenharmony_ci      // 0.25: make the value of memoryValue2 * 0.3 greater than minGCBaseline
803af6ab5fSopenharmony_ci      const memoryValue2: number = MemoryUtils.getMinGCThreshold() / 0.25;
813af6ab5fSopenharmony_ci      MemoryUtils.updateBaseMemory(memoryValue2);
823af6ab5fSopenharmony_ci      assert.isAbove(MemoryUtils.getGCThreshold(), MemoryUtils.getMinGCThreshold());
833af6ab5fSopenharmony_ci      assert.strictEqual(MemoryUtils.getBaseMemorySize(), memoryValue2);
843af6ab5fSopenharmony_ci    });
853af6ab5fSopenharmony_ci  });
863af6ab5fSopenharmony_ci});