-
Notifications
You must be signed in to change notification settings - Fork 33
#96 Improve takeSnapshot
#97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const v8 = require("v8"); | ||
|
||
module.exports = function mockHeapStatistics () { | ||
v8.getHeapStatistics = () => ({ | ||
heap_size_limit: 100, | ||
used_heap_size: 55, | ||
}) | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -355,17 +355,19 @@ class WorkerNodes extends EventEmitter { | |||||
|
||||||
/** | ||||||
* Take Heap Snapshot and save result on main process directory | ||||||
* | ||||||
* @returns {void} | ||||||
* @param {TakeSnapshotOptions} options | ||||||
* @returns {Promise<string>} | ||||||
*/ | ||||||
takeSnapshot() { | ||||||
takeSnapshot(options = {}) { | ||||||
const worker = this.pickWorker(); | ||||||
|
||||||
if (worker) { | ||||||
worker.takeSnapshot(); | ||||||
return worker.takeSnapshot(options); | ||||||
} else { | ||||||
// There might not be availble worker, let it start. | ||||||
setTimeout(() => this.takeSnapshot(), 500); | ||||||
return new Promise((resolve) => { | ||||||
// There might not be available worker, let it start. | ||||||
setTimeout(() => resolve(this.takeSnapshot()), 500); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we pass the same options to the second call?
Suggested change
|
||||||
}) | ||||||
} | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
const v8 = require('v8'); | ||
const fs = require('fs'); | ||
|
||
const getHeapSnapshot = (callback) => { | ||
const stream = v8.getHeapSnapshot(); | ||
const file = fs.createWriteStream(`HeapSnapshot-${process.pid}-${Date.now()}.heapsnapshot`); | ||
|
||
stream.on('data', (chunk) => file.write(chunk)); | ||
const hasEnoughMemory = () => { | ||
const heapStats = v8.getHeapStatistics(); | ||
return heapStats.heap_size_limit >= heapStats.used_heap_size * 2; | ||
} | ||
|
||
stream.on('end', () => { | ||
if (callback) { callback('heap snapshot done'); } | ||
}); | ||
const getHeapSnapshot = (callback) => { | ||
if (hasEnoughMemory()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about an early return if not enough memory to avoid having the main function body enclosed in an if statement? |
||
const filePath = v8.writeHeapSnapshot(`HeapSnapshot-${process.pid}-${Date.now()}.heapsnapshot`); | ||
callback(filePath); | ||
} else { | ||
callback(undefined, { | ||
type: 'Error', | ||
message: 'Not enough memory to perform heap snapshot' | ||
}); | ||
} | ||
} | ||
|
||
module.exports = getHeapSnapshot; | ||
module.exports = getHeapSnapshot; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const promiseWithResolvers = () => { | ||
let resolve, reject | ||
const promise = new Promise((res, rej) => { | ||
resolve = res | ||
reject = rej | ||
}) | ||
return { promise, resolve, reject } | ||
} | ||
|
||
module.exports = promiseWithResolvers; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can remove the changelog, we're using github releases for release notes ;)