Skip to content

Commit

Permalink
improved performance for addFromDir
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jun 28, 2024
1 parent 76396fe commit c1b2954
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

- 2.8.6
- improved performance

- 2.8.5
- fixed a sourcemap content issue

Expand Down
73 changes: 46 additions & 27 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -870,49 +867,71 @@ 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}`);
return;
}

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 = {
Expand Down
8 changes: 1 addition & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit c1b2954

Please sign in to comment.