Skip to content

Commit

Permalink
add forceGC
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Oct 6, 2024
1 parent 4ff78cf commit d1e66e9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/istanbul/istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
};
Expand Down
12 changes: 6 additions & 6 deletions lib/reports/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 = {
Expand Down Expand Up @@ -99,7 +99,7 @@ const rawReport = async (reportData, reportOptions, options) => {

// merge
if (rawOptions.merge) {
await mergeReport(rawDir, rawParent);
await mergeReport(rawDir, options);
}

// zip
Expand Down
9 changes: 9 additions & 0 deletions lib/utils/gc.js
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 5 additions & 11 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand All @@ -40,6 +39,10 @@ const Util = {
markdownGrid,
mergeV8Coverage,

forceGC: () => {
gc();
},

relativePath: function(p, root) {
p = `${p}`;
root = `${root || Util.root}`;
Expand Down Expand Up @@ -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: {
Expand Down
6 changes: 2 additions & 4 deletions lib/v8/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d1e66e9

Please sign in to comment.