1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# 4# Copyright (c) 2025 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18from __future__ import annotations 19from dataclasses import dataclass 20import typing 21 22import cdp.util as util 23import cdp.profiler as profiler 24 25 26@dataclass 27class ProfileArray: 28 """ 29 Custom container for holding multiple profiling results. 30 31 This is required for 1.2 profiler because: 32 1. The 1.2 profiler may generate multiple profile segments that need to be 33 processed together, while standard CDP typically works with single profiles. 34 2. Provides structured handling of profile arrays which is more common in 35 the 1.2 profiler's use cases. 36 """ 37 38 profile: typing.List[profiler.Profile] 39 40 @classmethod 41 def from_json(cls, json: typing.List[util.T_JSON_DICT]) -> ProfileArray: 42 profiles = [profiler.Profile.from_json(item) for item in json] 43 return cls(profiles) 44 45 46def profiler_stop() -> typing.Generator[util.T_JSON_DICT, util.T_JSON_DICT, ProfileArray]: 47 """ 48 Custom implementation of profiler stop command. 49 50 This differs from standard CDP implementation because: 51 1. Maintains compatibility with CDP protocol while extending it for 1.2 52 profiler's specific needs. 53 2. Returns a ProfileArray instead of a single Profile object to accommodate 54 the 1.2 profiler's capability of returning multiple profile segments. 55 """ 56 cmd_dict: util.T_JSON_DICT = { 57 "method": "Profiler.stop", 58 } 59 json = yield cmd_dict 60 return ProfileArray.from_json(json["profile"]) 61