-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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 <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
- Loading branch information
1 parent
c5acf0a
commit ae3929e
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' | ||
}); | ||
} |