From c1b2954abd2d230c8feb1503d618af690255f29c Mon Sep 17 00:00:00 2001 From: cenfun Date: Fri, 28 Jun 2024 22:20:46 +0800 Subject: [PATCH] improved performance for addFromDir --- CHANGELOG.md | 3 ++ lib/generate.js | 73 +++++++++++++++++++++++++++++++------------------ lib/index.js | 8 +----- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 118e050b..ac30fca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## Changelog +- 2.8.6 + - improved performance + - 2.8.5 - fixed a sourcemap content issue diff --git a/lib/generate.js b/lib/generate.js index da758f85..51b76fbc 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -835,14 +835,11 @@ const resolveEntrySourceMap = (entry, sourceMapCache) => { } }; -const readCoverageData = (filePath, entryFilter, sourceCache) => { +const readCoverageData = (item, entryFilter, sourceCache) => { - const content = fs.readFileSync(filePath).toString('utf-8'); - if (!content) { - return; - } + const json = item.json; - const json = JSON.parse(content); + // raw v8 json let coverageData = json.result; if (!Util.isList(coverageData)) { return; @@ -870,7 +867,7 @@ const readCoverageData = (filePath, entryFilter, sourceCache) => { return coverageData; }; -const readFromDir = async (dir, entryFilter, addHandler) => { +const readFromDir = async (mcr, dir) => { if (!dir || !fs.existsSync(dir)) { Util.logInfo(`Not found V8 coverage dir: ${dir}`); @@ -878,41 +875,63 @@ const readFromDir = async (dir, entryFilter, addHandler) => { } const files = fs.readdirSync(dir); - if (!files.length) { + const list = []; + files.forEach((filename) => { + if (filename.startsWith('coverage-')) { + list.push({ + type: 'coverage', + filename + }); + return; + } + if (filename.startsWith('source-')) { + list.push({ + type: 'source', + filename + }); + } + }); + + if (!list.length) { Util.logInfo(`No coverage files in the dir: ${dir}`); return; } - const coverageFiles = []; + const contentList = await Promise.all(list.map((it) => Util.readFile(path.resolve(dir, it.filename)))); const sourceCache = {}; - files.forEach((filename) => { - const isCoverage = filename.startsWith('coverage-'); - if (isCoverage) { - coverageFiles.push(filename); + const coverageFiles = []; + + contentList.forEach((content, i) => { + if (!content) { return; } - const isSource = filename.startsWith('source-'); - if (isSource) { - const filePath = path.resolve(dir, filename); - const content = fs.readFileSync(filePath).toString('utf-8'); - if (content) { - const json = JSON.parse(content); - if (json.url) { - sourceCache[json.url] = json; - } + 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); } }); - // filter before adding - for (const filename of coverageFiles) { - const filePath = path.resolve(dir, filename); - const coverageData = readCoverageData(filePath, entryFilter, sourceCache); + const entryFilter = mcr.getEntryFilter(); + + const results = []; + for (const item of coverageFiles) { + const coverageData = readCoverageData(item, entryFilter, sourceCache); if (coverageData) { - await addHandler(coverageData); + const res = await mcr.add(coverageData); + results.push(res); } } + return results; }; module.exports = { diff --git a/lib/index.js b/lib/index.js index fc0cfb8d..cb1f241a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -124,14 +124,8 @@ class CoverageReport { // add coverage from dir async addFromDir(dir) { const time_start = Date.now(); - - const entryFilter = this.getEntryFilter(); - const results = await readFromDir(dir, entryFilter, (coverageData) => { - return this.add(coverageData); - }); - + const results = await readFromDir(this, dir); Util.logTime(`added from dir: ${dir}`, time_start); - return results; }