# -------------------------------------------------------------------------
#  This file is part of the MindStudio project.
# Copyright (c) 2025 Huawei Technologies Co.,Ltd.
#
# MindStudio is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#
#          http://license.coscl.org.cn/MulanPSL2
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# -------------------------------------------------------------------------

from atk.configs.standard_config import PerfStandardConfig, PerfStandard
from atk.common.log import Logger

logging = Logger().get_logger()


class PerfCompareExecutor:
    memory_standard = PerfStandard().memory_standard

    @classmethod
    def compare_case_performance(cls, base_perf: float, bm_perf: float, perf_standard: PerfStandardConfig) -> bool:
        if base_perf == 0 or base_perf is None:
            logging.error(f"The base performance is {base_perf}, please check!")
            return False
        e2e_ratio = bm_perf / base_perf
        ret = e2e_ratio >= perf_standard.radio_bm
        return ret

    @classmethod
    def compare_case_memory(cls, base_memory, bm_memory):
        if bm_memory == 0 or bm_memory is None:
            if base_memory == 0:
                return True
            else:
                return False
        memory_ratio = base_memory / bm_memory
        ret = memory_ratio <= cls.memory_standard
        return ret

    @classmethod
    def compare_summary(cls, perf_info, avg_perf_ratio, perf_standard: PerfStandardConfig):
        perf_ret = None
        if isinstance(avg_perf_ratio, str):
            avg_perf_ratio = float(avg_perf_ratio)
        if avg_perf_ratio is None:
            return perf_ret
        if perf_standard is None:
            return "Failed"
        perf_ret = True if avg_perf_ratio >= perf_standard.aver_radio_bm else False
        perf_ret = True if perf_ret and perf_info.fail_length == 0 else False
        return "Pass" if perf_ret else "Failed"