1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021-2024 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 18import logging 19from unittest import TestCase 20from pathlib import Path 21from typing import List 22 23from runner.logger import Log 24from runner.utils import write_2_file 25from runner.plugins.ets.ets_templates.params import Params 26from runner.plugins.ets.ets_templates.template import Template 27 28 29TEMPLATE_EXTENSION = ".sts" 30OUT_EXTENSION = ".sts" 31 32_LOGGER = logging.getLogger("runner.plugins.ets.ets_templates.benchmark") 33 34 35class Benchmark: 36 def __init__(self, test_path: Path, output: Path, test_full_name: str) -> None: 37 self.__input = test_path 38 self.__output = output.parent 39 self.__name = test_path.name 40 self.__full_name = test_full_name[:-len(TEMPLATE_EXTENSION)] 41 42 def generate(self) -> List[str]: 43 Log.all(_LOGGER, f"Generating test: {self.__name}") 44 name_without_ext, _ = self.__name.split(".") 45 params = Params(self.__input, name_without_ext).generate() 46 47 template = Template(self.__input, params) 48 Log.all(_LOGGER, f"Starting generate test template: {self.__name}") 49 rendered_tests = template.render_template() 50 TestCase().assertTrue(len(rendered_tests) > 0, f"Internal error: there should be tests in {self.__name}") 51 52 tests = [] 53 for i, test in enumerate(rendered_tests): 54 name = f"{name_without_ext}_{i}" if len(rendered_tests) > 1 else name_without_ext 55 full_name = f"{self.__full_name}_{i}" if len(rendered_tests) > 1 else self.__full_name 56 test_content = template.generate_test(test, full_name) 57 file_path = self.__output / f"{name}{OUT_EXTENSION}" 58 write_2_file(file_path=file_path, content=test_content) 59 tests.append(str(file_path)) 60 61 Log.all(_LOGGER, f"Finish generating test template for: {self.__name}") 62 return tests 63