如何以编程方式在Google Chrome中获取我的网站的内存使用量(JS和总数)?
我看过使用未记录的HeapProfiler从Chrome扩展程序中执行此操作(请参阅此处),但我无法找到从中获取数据的方法.
我想测量每个版本的内存消耗,所以这需要编程.
编辑:我想出了如何使HeapProfiler方法工作.每个addHeapSnapshotChunk事件都有一个JSON对象的块.
chrome.browserAction.onClicked.addListener(function(tab) {
var heapData,
debugId = {tabId:tab.id};
chrome.debugger.attach(debugId, '1.0', function() {
chrome.debugger.sendCommand(debugId, 'Debugger.enable', {}, function() {
function headerListener(source, name, data) {
if(source.tabId == tab.id && name == 'HeapProfiler.addProfileHeader') {
function chunkListener(source, name, data) {
if(name == 'HeapProfiler.addHeapSnapshotChunk') {
heapData += data.chunk;
} else if(name == 'HeapProfiler.finishHeapSnapshot') {
chrome.debugger.onEvent.removeListener(chunkListener);
chrome.debugger.detach(debugId);
//do something with data
console.log('Collected ' + heapData.length + ' bytes of JSON data');
}
}
chrome.debugger.onEvent.addListener(chunkListener);
chrome.debugger.sendCommand(debugId, 'HeapProfiler.getHeapSnapshot', {uid:data.header.uid, type:data.header.typeId});
} …Run Code Online (Sandbox Code Playgroud) javascript performance google-chrome memory-profiling google-chrome-devtools