Skip to content

Commit

Permalink
fix OOM
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jul 1, 2024
1 parent c80ad95 commit 6e33f9e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 41 deletions.
85 changes: 45 additions & 40 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,16 @@ const resolveEntrySourceMap = (entry, sourceMapCache) => {
}
};

const readCoverageData = (item, entryFilter, sourceCache) => {
const readCoverageData = async (dir, filename, entryFilter, sourceCache) => {

const json = item.json;
const content = await Util.readFile(path.resolve(dir, filename));
if (!content) {
return;
}
const json = JSON.parse(content);
if (!json) {
return;
}

// raw v8 json
let coverageData = json.result;
Expand Down Expand Up @@ -886,6 +893,26 @@ const readCoverageData = (item, entryFilter, sourceCache) => {
return coverageData;
};

const readSourceList = async (dir, sourceList) => {
const sourceCache = {};

for (const filename of sourceList) {
const content = await Util.readFile(path.resolve(dir, filename));
if (!content) {
continue;
}
const json = JSON.parse(content);
if (!json) {
continue;
}
if (json.url) {
sourceCache[json.url] = json;
}
}

return sourceCache;
};

const readFromDir = async (mcr, dir) => {

if (!dir || !fs.existsSync(dir)) {
Expand All @@ -894,56 +921,34 @@ const readFromDir = async (mcr, dir) => {
}

const files = fs.readdirSync(dir);
const list = [];

const coverageList = [];
const sourceList = [];

files.forEach((filename) => {
if (filename.startsWith('coverage-')) {
list.push({
type: 'coverage',
filename
});
return;
}
if (filename.startsWith('source-')) {
list.push({
type: 'source',
filename
});
// read all json files
if (filename.endsWith('.json')) {
// could be source files generated by register hooks
if (filename.startsWith('source-')) {
sourceList.push(filename);
} else {
coverageList.push(filename);
}
}
});

if (!list.length) {
if (!coverageList.length) {
Util.logInfo(`No coverage files in the dir: ${dir}`);
return;
}

const contentList = await Promise.all(list.map((it) => Util.readFile(path.resolve(dir, it.filename))));
const sourceCache = {};
const coverageFiles = [];

contentList.forEach((content, i) => {
if (!content) {
return;
}
const json = JSON.parse(content);
if (!json) {
return;
}
const item = list[i];
if (item.type === 'source') {
if (json.url) {
sourceCache[json.url] = json;
}
} else {
item.json = json;
coverageFiles.push(item);
}
});
const sourceCache = await readSourceList(dir, sourceList);

const entryFilter = mcr.getEntryFilter();

const results = [];
for (const item of coverageFiles) {
const coverageData = readCoverageData(item, entryFilter, sourceCache);
for (const filename of coverageList) {
const coverageData = await readCoverageData(dir, filename, entryFilter, sourceCache);
if (coverageData) {
const res = await mcr.add(coverageData);
results.push(res);
Expand Down
11 changes: 10 additions & 1 deletion lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,15 @@ const Util = {
}
const duration = Date.now() - time_start;
const durationH = Util.TF(duration);
const ls = [`[MCR] ${message}`, ' ('];
const ls = [`[MCR] ${message}`];

// memory
const { heapUsed } = process.memoryUsage();
const memory = parseFloat((heapUsed / 1024 ** 2).toFixed(1));
ls.push(` (memory: ${memory}MB)`);

// time
ls.push(' (');
if (duration <= 100) {
ls.push(EC.green(durationH));
} else if (duration < 500) {
Expand All @@ -620,6 +628,7 @@ const Util = {
ls.push(EC.red(durationH));
}
ls.push(')');

console.log(ls.join(''));
}

Expand Down

0 comments on commit 6e33f9e

Please sign in to comment.