1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: Python HeapProfiler Domain Interfaces 18""" 19 20import sys 21import json 22from pathlib import Path 23 24sys.path.append(str(Path(__file__).parent.parent)) # add aw path to sys.path 25 26from all_utils import CommonUtils 27from implement_api.protocol_api import ProtocolImpl 28from cdp import heap_profiler 29from customized_types import ProtocolType 30 31comm_with_debugger_server = CommonUtils.communicate_with_debugger_server 32 33 34class HeapProfilerImpl(ProtocolImpl): 35 36 def __init__(self, id_generator, websocket): 37 super().__init__(id_generator, websocket) 38 self.dispatch_table = {"startTrackingHeapObjects": (self.start_tracking_heap_objects, ProtocolType.send), 39 "stopTrackingHeapObjects": (self.stop_tracking_heap_objects, ProtocolType.send), 40 "takeHeapSnapshot": (self.take_heap_snapshot, ProtocolType.send), 41 "startSampling": (self.start_sampling, ProtocolType.send), 42 "stopSampling": (self.stop_sampling, ProtocolType.send)} 43 44 async def start_tracking_heap_objects(self, message_id, connection, params): 45 response = await comm_with_debugger_server(self.websocket, connection, 46 heap_profiler.start_tracking_heap_objects(params), message_id) 47 CommonUtils.assert_equal(json.loads(response), {"id": message_id, "result": {}}) 48 49 async def stop_tracking_heap_objects(self, message_id, connection, params): 50 response = await comm_with_debugger_server(self.websocket, connection, 51 heap_profiler.stop_tracking_heap_objects(), message_id) 52 while response.startswith('{"method":"HeapProfiler.lastSeenObjectId"') \ 53 or response.startswith('{"method":"HeapProfiler.heapStatsUpdate"'): 54 response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, 55 connection.received_msg_queue) 56 assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response, \ 57 f'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"] not in {response}' 58 pre_response = response 59 while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"') \ 60 or response.startswith('{"method":"HeapProfiler.lastSeenObjectId"') \ 61 or response.startswith('{"method":"HeapProfiler.heapStatsUpdate"'): 62 if response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): 63 pre_response = response 64 response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, 65 connection.received_msg_queue) 66 assert pre_response.endswith(r'\n]\n}\n"}}'), 'This is not the last message of addHeapSnapshotChunk' 67 CommonUtils.assert_equal(json.loads(response), {"id": message_id, "result": {}}) 68 69 async def take_heap_snapshot(self, message_id, connection, params): 70 response = await comm_with_debugger_server(self.websocket, connection, 71 heap_profiler.take_heap_snapshot(), message_id) 72 assert r'\"location_fields\":[\"object_index\",\"script_id\",\"line\",\"column\"]' in response, \ 73 'This is not the last message of addHeapSnapshotChunk' 74 pre_response = response 75 while response.startswith('{"method":"HeapProfiler.addHeapSnapshotChunk"'): 76 pre_response = response 77 response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id, 78 connection.received_msg_queue) 79 assert pre_response.endswith(r'\n]\n}\n"}}'), 'This is not the last message of addHeapSnapshotChunk' 80 CommonUtils.assert_equal(json.loads(response), {"id": message_id, "result": {}}) 81 82 async def start_sampling(self, message_id, connection, params): 83 response = await comm_with_debugger_server(self.websocket, connection, 84 heap_profiler.start_sampling(), message_id) 85 CommonUtils.assert_equal(json.loads(response), {"id": message_id, "result": {}}) 86 87 async def stop_sampling(self, message_id, connection, params): 88 response = await comm_with_debugger_server(self.websocket, connection, 89 heap_profiler.stop_sampling(), message_id) 90 response = json.loads(response) 91 CommonUtils.assert_equal(response['id'], message_id) 92 return response 93