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 Profiler Domain Interfaces 18""" 19 20import json 21 22from aw import communicate_with_debugger_server 23from aw.cdp import profiler 24from aw.types import ProtocolType 25from aw.api.protocol_api import ProtocolImpl 26 27 28class ProfilerImpl(ProtocolImpl): 29 30 def __init__(self, id_generator, websocket): 31 super().__init__(id_generator, websocket) 32 self.dispatch_table = {"start": (self.start, ProtocolType.send), 33 "stop": (self.stop, ProtocolType.send), 34 "setSamplingInterval": (self.set_sampling_interval, ProtocolType.send)} 35 36 async def start(self, message_id, connection, params): 37 response = await communicate_with_debugger_server(connection.instance_id, 38 connection.send_msg_queue, 39 connection.received_msg_queue, 40 profiler.start(), message_id) 41 response = json.loads(response) 42 assert response == {"id": message_id, "result": {}} 43 return response 44 45 async def stop(self, message_id, connection, params): 46 response = await communicate_with_debugger_server(connection.instance_id, 47 connection.send_msg_queue, 48 connection.received_msg_queue, 49 profiler.stop(), message_id) 50 response = json.loads(response) 51 assert response['id'] == message_id 52 assert all(i >= 0 for i in response['result']['profile']['timeDeltas']) 53 return response 54 55 async def set_sampling_interval(self, message_id, connection, params): 56 response = await communicate_with_debugger_server(connection.instance_id, 57 connection.send_msg_queue, 58 connection.received_msg_queue, 59 profiler.set_sampling_interval(params), message_id) 60 response = json.loads(response) 61 assert response == {"id": message_id, "result": {}} 62 return response