From 6e33f9ecfc3dce804e2c221852e8b2473c4479d0 Mon Sep 17 00:00:00 2001 From: cenfun Date: Mon, 1 Jul 2024 13:22:41 +0800 Subject: [PATCH] fix OOM --- lib/generate.js | 85 +++++++++++++++++++++++++---------------------- lib/utils/util.js | 11 +++++- 2 files changed, 55 insertions(+), 41 deletions(-) diff --git a/lib/generate.js b/lib/generate.js index 12f5d885..801f8409 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -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; @@ -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)) { @@ -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); diff --git a/lib/utils/util.js b/lib/utils/util.js index e02ea252..b2bfb2db 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -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) { @@ -620,6 +628,7 @@ const Util = { ls.push(EC.red(durationH)); } ls.push(')'); + console.log(ls.join('')); }