1# Copyright 2020 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15"""Test the aicpu parser.""" 16import os 17import tempfile 18import shutil 19 20from unittest import TestCase 21 22from mindspore.profiler.parser.aicpu_data_parser import DataPreProcessParser 23 24 25def get_result(file_path): 26 """ 27 Get result from the aicpu file. 28 29 Args: 30 file_path (str): The aicpu file path. 31 32 Returns: 33 list[list], the parsed aicpu information. 34 """ 35 result = [] 36 file = None 37 try: 38 file = open(file_path, 'r') 39 result.append(file.read()) 40 return result 41 finally: 42 if file: 43 file.close() 44 45 46class TestAicpuParser(TestCase): 47 """Test the class of Aicpu Parser.""" 48 49 def setUp(self) -> None: 50 """Initialization before test case execution.""" 51 self.profiling_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), 52 '../../../data/profiler_data/' 53 'JOB_AICPU/data')) 54 self.expect_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), 55 '../../../data/profiler_data/' 56 'JOB_AICPU/expect')) 57 self.output_path = tempfile.mkdtemp(prefix='output_data_preprocess_aicpu_') 58 self.output_file = os.path.join(self.output_path, 'output_data_preprocess_aicpu_0.txt') 59 self.expect_file = os.path.join(self.expect_dir, 'output_data_preprocess_aicpu_0.txt') 60 61 def test_aicpu_parser(self): 62 """Test the class of Aicpu Parser.""" 63 data = DataPreProcessParser(self.profiling_dir, self.output_file) 64 data.execute() 65 expect_result = get_result(self.expect_file) 66 result = get_result(self.output_file) 67 shutil.rmtree(self.output_path) 68 assert expect_result == result 69 70 def test_aicpu_parser_file_not_exist(self): 71 """Test the class of Aicpu Parser.""" 72 profiling_dir = os.path.realpath(os.path.join(self.profiling_dir, 'data')) 73 data = DataPreProcessParser(profiling_dir, self.output_file) 74 data.execute() 75 shutil.rmtree(self.output_path) 76