Skip to content

Commit

Permalink
added option gc for running gc manually
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Oct 7, 2024
1 parent e562723 commit 282a9cf
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 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.11.1
- added option `gc` for running gc manually

- 2.11.0
- added `zip` and `merge` option for `raw` report

Expand Down
3 changes: 3 additions & 0 deletions lib/default/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ module.exports = {
// {boolean} Indicates whether to clean previous cache in output dir before generating report. Defaults to false.
cleanCache: false,

// {number} gc threshold
gc: null,

// {function} onEntry hook
// onEntry: async (entry) => {}
onEntry: null,
Expand Down
7 changes: 7 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,13 @@ declare namespace MCR {
*/
cleanCache?: boolean;

/**
* {number} gc threshold
* for example: sets gc to 1024 means that force gc when the memory > 1024M at certain critical stages
* https://nodejs.org/docs/latest/api/v8.html#v8setflagsfromstringflags
*/
gc?: number;

/** (V8 only) {function} onEntry hook */
onEntry?: (entry: V8CoverageEntry) => Promise<void>;

Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class CoverageReport {
// init logging for clean after generated
this.logging = Util.initLoggingLevel(this.options.logging);

if (Util.isNum(this.options.gc)) {
Util.setGC(this.options.gc);
}

// init outputDir
const outputDir = `${this.options.outputDir || defaultOptions.outputDir}`;
this.options.outputDir = outputDir;
Expand Down Expand Up @@ -121,10 +125,6 @@ class CoverageReport {
return results;
}

setGC(value) {
Util.setGC(value);
}

// add coverage from dir
async addFromDir(dir) {
const time_start = Date.now();
Expand Down
10 changes: 5 additions & 5 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,11 +869,11 @@ const Util = {
console.log(`[MCR] ${message} before ${lengthBefore} => after ${lengthAfter}`);
},

setGC: (value) => {
if (!Util.isNum(value)) {
value = 1024;
setGC: (threshold) => {
if (!Util.isNum(threshold)) {
threshold = 1024;
}
Util.gcValue = value;
Util.gcThreshold = threshold;
},

getMemory: () => {
Expand All @@ -887,7 +887,7 @@ const Util = {

// memory gc
let memory = Util.getMemory();
if (Util.gcValue && Util.gcValue < memory) {
if (Util.gcThreshold && Util.gcThreshold < memory) {
Util.forceGC();
}

Expand Down

0 comments on commit 282a9cf

Please sign in to comment.