-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm: implement vm.measureMemory() for per-context memory measurement
This patch implements `vm.measureMemory()` with the new `v8::Isolate::MeasureMemory()` API to measure per-context memory usage. This should be experimental, since detailed memory measurement requires further integration with the V8 API that should be available in a future V8 update. PR-URL: #31824 Refs: https://github.com/ulan/performance-measure-memory Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
- Loading branch information
1 parent
24aa9bd
commit fb73045
Showing
6 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
|
||
common.expectWarning('ExperimentalWarning', | ||
'vm.measureMemory is an experimental feature. ' + | ||
'This feature could change at any time'); | ||
|
||
// The formats could change when V8 is updated, then the tests should be | ||
// updated accordingly. | ||
function assertSummaryShape(result) { | ||
assert.strictEqual(typeof result, 'object'); | ||
assert.strictEqual(typeof result.total, 'object'); | ||
assert.strictEqual(typeof result.total.jsMemoryEstimate, 'number'); | ||
assert(Array.isArray(result.total.jsMemoryRange)); | ||
assert.strictEqual(typeof result.total.jsMemoryRange[0], 'number'); | ||
assert.strictEqual(typeof result.total.jsMemoryRange[1], 'number'); | ||
} | ||
|
||
function assertDetailedShape(result) { | ||
// For now, the detailed shape is the same as the summary shape. This | ||
// should change in future versions of V8. | ||
return assertSummaryShape(result); | ||
} | ||
|
||
// Test measuring memory of the current context | ||
{ | ||
vm.measureMemory() | ||
.then(assertSummaryShape); | ||
|
||
vm.measureMemory({}) | ||
.then(assertSummaryShape); | ||
|
||
vm.measureMemory({ mode: 'summary' }) | ||
.then(assertSummaryShape); | ||
|
||
vm.measureMemory({ mode: 'detailed' }) | ||
.then(assertDetailedShape); | ||
|
||
assert.throws(() => vm.measureMemory(null), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
assert.throws(() => vm.measureMemory('summary'), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
assert.throws(() => vm.measureMemory({ mode: 'random' }), { | ||
code: 'ERR_INVALID_ARG_VALUE' | ||
}); | ||
} | ||
|
||
// Test measuring memory of the sandbox | ||
{ | ||
const context = vm.createContext(); | ||
vm.measureMemory({ context }) | ||
.then(assertSummaryShape); | ||
|
||
vm.measureMemory({ mode: 'summary', context },) | ||
.then(assertSummaryShape); | ||
|
||
vm.measureMemory({ mode: 'detailed', context }) | ||
.then(assertDetailedShape); | ||
|
||
assert.throws(() => vm.measureMemory({ mode: 'summary', context: null }), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
assert.throws(() => vm.measureMemory({ mode: 'summary', context: {} }), { | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
}); | ||
} |