def TakeJSHeapSnapshot(self, timeout=120): # This is a hack to make the nested function be able to modify the # variables. snapshot_uid = [0] snapshot = [] def OnNotification(res): if res['method'] == 'HeapProfiler.addProfileHeader': snapshot_uid[0] = res['params']['header']['uid'] elif res['method'] == 'HeapProfiler.addHeapSnapshotChunk': snapshot.append(res['params']['chunk']) def OnClose(): pass self.RegisterDomain('HeapProfiler', OnNotification, OnClose) self.SyncRequest({'method': 'Page.getResourceTree'}, timeout) self.SyncRequest({'method': 'Debugger.enable'}, timeout) self.SyncRequest({'method': 'HeapProfiler.clearProfiles'}, timeout) self.SyncRequest({'method': 'HeapProfiler.takeHeapSnapshot'}, timeout) self.SyncRequest( { 'method': 'HeapProfiler.getHeapSnapshot', 'params': { 'uid': snapshot_uid[0] } }, timeout) snapshot = ''.join(snapshot) self.UnregisterDomain('HeapProfiler') return model.Model(snapshot)
def TakeJSHeapSnapshot(self, timeout=120): snapshot = [] def OnNotification(res): if res['method'] == 'HeapProfiler.addHeapSnapshotChunk': snapshot.append(res['params']['chunk']) def OnClose(): pass self.RegisterDomain('HeapProfiler', OnNotification, OnClose) self.SyncRequest({'method': 'Page.getResourceTree'}, timeout) self.SyncRequest({'method': 'Debugger.enable'}, timeout) self.SyncRequest({'method': 'HeapProfiler.takeHeapSnapshot'}, timeout) snapshot = ''.join(snapshot) self.UnregisterDomain('HeapProfiler') return model.Model(snapshot)