From d1e66e925672bf19c4a3f5deef2acfa4d0750ff1 Mon Sep 17 00:00:00 2001 From: cenfun Date: Sun, 6 Oct 2024 15:36:10 +0800 Subject: [PATCH] add forceGC --- lib/istanbul/istanbul.js | 4 ++-- lib/reports/raw.js | 12 ++++++------ lib/utils/gc.js | 9 +++++++++ lib/utils/util.js | 16 +++++----------- lib/v8/v8.js | 6 ++---- 5 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 lib/utils/gc.js diff --git a/lib/istanbul/istanbul.js b/lib/istanbul/istanbul.js index e50d562..850b3c2 100644 --- a/lib/istanbul/istanbul.js +++ b/lib/istanbul/istanbul.js @@ -144,7 +144,7 @@ const addUntestedFiles = async (istanbulData, options) => { }; -const mergeIstanbulDataList = (dataList) => { +const mergeIstanbulDataList = (dataList, options) => { const istanbulCoverageList = dataList.map((it) => it.data); const coverageMap = istanbulLibCoverage.createCoverageMap(); istanbulCoverageList.forEach((coverage) => { @@ -155,7 +155,7 @@ const mergeIstanbulDataList = (dataList) => { }; const mergeIstanbulCoverage = async (dataList, options) => { - const istanbulData = mergeIstanbulDataList(dataList); + const istanbulData = mergeIstanbulDataList(dataList, options); await addUntestedFiles(istanbulData, options); return istanbulData; }; diff --git a/lib/reports/raw.js b/lib/reports/raw.js index 18f7f30..340ee2c 100644 --- a/lib/reports/raw.js +++ b/lib/reports/raw.js @@ -5,14 +5,14 @@ const { ZipFile } = require('../packages/monocart-coverage-vendor.js'); const { mergeIstanbulDataList } = require('../istanbul/istanbul.js'); const { mergeV8DataList } = require('../v8/v8.js'); -const getMergedData = (type, dataList) => { +const getMergedData = (type, dataList, options) => { if (type === 'istanbul') { - return mergeIstanbulDataList(dataList); + return mergeIstanbulDataList(dataList, options); } - return mergeV8DataList(dataList); + return mergeV8DataList(dataList, options); }; -const mergeReport = async (rawDir, rawParent) => { +const mergeReport = async (rawDir, options) => { const coverageFiles = fs.readdirSync(rawDir).filter((n) => n.endsWith('.json') && n.startsWith('coverage-')); // console.log(coverageFiles); @@ -29,7 +29,7 @@ const mergeReport = async (rawDir, rawParent) => { Util.rmSync(jsonPath); } - const mergedData = await getMergedData(type, dataList); + const mergedData = await getMergedData(type, dataList, options); const dataId = Util.uid(); const results = { @@ -99,7 +99,7 @@ const rawReport = async (reportData, reportOptions, options) => { // merge if (rawOptions.merge) { - await mergeReport(rawDir, rawParent); + await mergeReport(rawDir, options); } // zip diff --git a/lib/utils/gc.js b/lib/utils/gc.js new file mode 100644 index 0000000..316337d --- /dev/null +++ b/lib/utils/gc.js @@ -0,0 +1,9 @@ +const v8 = require('node:v8'); +const vm = require('node:vm'); + +if (typeof global.gc !== 'function') { + v8.setFlagsFromString('--expose_gc'); + global.gc = vm.runInNewContext('gc'); +} + +module.exports = global.gc; diff --git a/lib/utils/util.js b/lib/utils/util.js index 2fc35ec..9378e44 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -3,8 +3,6 @@ const { writeFile, readFile } = require('fs/promises'); const path = require('path'); const os = require('os'); const crypto = require('crypto'); -const v8 = require('node:v8'); -const vm = require('node:vm'); const EC = require('eight-colors'); const CG = require('console-grid'); @@ -15,6 +13,7 @@ const request = require('./request.js'); const version = require('../../package.json').version; const markdownGrid = require('./markdown.js'); const { mergeV8Coverage } = require('./merge/merge.js'); +const gc = require('./gc.js'); const { findUpSync, supportsColor, minimatch @@ -40,6 +39,10 @@ const Util = { markdownGrid, mergeV8Coverage, + forceGC: () => { + gc(); + }, + relativePath: function(p, root) { p = `${p}`; root = `${root || Util.root}`; @@ -791,15 +794,6 @@ const Util = { return major1 - major2; }, - forceGC: () => { - if (!Util.expose_gc) { - v8.setFlagsFromString('--expose_gc'); - Util.expose_gc = true; - } - const gc = vm.runInNewContext('gc'); - gc(); - }, - // ========================================================================================== loggingLevels: { diff --git a/lib/v8/v8.js b/lib/v8/v8.js index c46fdaf..33a3a85 100644 --- a/lib/v8/v8.js +++ b/lib/v8/v8.js @@ -173,7 +173,7 @@ const mergeJsFunctions = async (itemList) => { return res.functions; }; -const mergeV8DataList = async (dataList) => { +const mergeV8DataList = async (dataList, options) => { const allList = dataList.map((d) => d.data).flat(); // separate coverage and empty items @@ -233,14 +233,12 @@ const mergeV8DataList = async (dataList) => { // merged list + empty list const mergedList = Object.values(itemMap).concat(emptyList); - // Util.forceGC(); - return mergedList; }; const mergeV8Coverage = async (dataList, sourceCache, options) => { - const mergedList = await mergeV8DataList(dataList); + const mergedList = await mergeV8DataList(dataList, options); // try to load coverage and source by id for (const entry of mergedList) {