From 54f2cf4ef0f8800e832b0dbd468bef67d89321ca Mon Sep 17 00:00:00 2001 From: zhangyihui Date: Tue, 28 Jul 2020 09:29:27 +0800 Subject: [PATCH] add some test case for profiler --- ...ramework.host.vm.graph_desc_info.0.slice_0 | 4 + ...ork.host.vm.graph_desc_info.0.slice_0.done | 0 ...Framework.host.vm.task_desc_info.0.slice_0 | 4 + .../JOB1/pipeline_profiling_0.json | 55 ++++++++ ...race.46.dev.profiler_default_tag.0.slice_0 | Bin 0 -> 4800 bytes .../JOB2/pipeline_profiling_0.json | 48 +++++++ ...ramework.host.vm.graph_desc_info.0.slice_0 | 4 + .../data/Framework.host.vm.point.0.slice_0 | 2 + ...Framework.host.vm.task_desc_info.0.slice_0 | 4 + .../data/DATA_PREPROCESS.dev.AICPU.0.slice_0 | Bin 0 -> 890 bytes .../expect/output_data_preprocess_aicpu_0.txt | 5 + ...ork.host.vm.graph_desc_info.0.JOB2.slice_0 | 4 + ...work.host.vm.task_desc_info.0.JOB2.slice_0 | 4 + .../profiler/aicore_intermediate_1_detail.csv | 11 ++ .../profiler/aicore_intermediate_1_type.csv | 6 + .../profiler/framework_raw_0.csv | 5 + .../profiler/framework_raw_1.csv | 11 ++ .../profiler/minddata_pipeline_raw_0.csv | 5 + .../profiler/pipeline_profiling_1.json | 55 ++++++++ .../profiler/step_trace_raw_0_detail_time.csv | 22 +++ .../step_trace_raw_10_detail_time.csv | 42 ++++++ tests/ut/python/profiler/__init__.py | 21 +++ tests/ut/python/profiler/parser/__init__.py | 14 ++ .../profiler/parser/test_aicpu_parser.py | 74 ++++++++++ .../profiler/parser/test_framework_parser.py | 128 ++++++++++++++++++ .../parser/test_minddata_pipeline_parser.py | 93 +++++++++++++ 26 files changed, 621 insertions(+) create mode 100644 tests/ut/data/profiler_data/JOB1/Framework.host.vm.graph_desc_info.0.slice_0 create mode 100644 tests/ut/data/profiler_data/JOB1/Framework.host.vm.graph_desc_info.0.slice_0.done create mode 100644 tests/ut/data/profiler_data/JOB1/Framework.host.vm.task_desc_info.0.slice_0 create mode 100644 tests/ut/data/profiler_data/JOB1/pipeline_profiling_0.json create mode 100644 tests/ut/data/profiler_data/JOB1/training_trace.46.dev.profiler_default_tag.0.slice_0 create mode 100644 tests/ut/data/profiler_data/JOB2/pipeline_profiling_0.json create mode 100644 tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.graph_desc_info.0.slice_0 create mode 100644 tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.point.0.slice_0 create mode 100644 tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.task_desc_info.0.slice_0 create mode 100644 tests/ut/data/profiler_data/JOB_AICPU/data/DATA_PREPROCESS.dev.AICPU.0.slice_0 create mode 100644 tests/ut/data/profiler_data/JOB_AICPU/expect/output_data_preprocess_aicpu_0.txt create mode 100644 tests/ut/data/profiler_data/container/0/data/Framework.host.vm.graph_desc_info.0.JOB2.slice_0 create mode 100644 tests/ut/data/profiler_data/container/0/data/Framework.host.vm.task_desc_info.0.JOB2.slice_0 create mode 100755 tests/ut/data/profiler_data/profiler/aicore_intermediate_1_detail.csv create mode 100755 tests/ut/data/profiler_data/profiler/aicore_intermediate_1_type.csv create mode 100755 tests/ut/data/profiler_data/profiler/framework_raw_0.csv create mode 100755 tests/ut/data/profiler_data/profiler/framework_raw_1.csv create mode 100644 tests/ut/data/profiler_data/profiler/minddata_pipeline_raw_0.csv create mode 100644 tests/ut/data/profiler_data/profiler/pipeline_profiling_1.json create mode 100755 tests/ut/data/profiler_data/profiler/step_trace_raw_0_detail_time.csv create mode 100755 tests/ut/data/profiler_data/profiler/step_trace_raw_10_detail_time.csv create mode 100644 tests/ut/python/profiler/__init__.py create mode 100644 tests/ut/python/profiler/parser/__init__.py create mode 100644 tests/ut/python/profiler/parser/test_aicpu_parser.py create mode 100644 tests/ut/python/profiler/parser/test_framework_parser.py create mode 100644 tests/ut/python/profiler/parser/test_minddata_pipeline_parser.py diff --git a/tests/ut/data/profiler_data/JOB1/Framework.host.vm.graph_desc_info.0.slice_0 b/tests/ut/data/profiler_data/JOB1/Framework.host.vm.graph_desc_info.0.slice_0 new file mode 100644 index 0000000000..9b3e7b322c --- /dev/null +++ b/tests/ut/data/profiler_data/JOB1/Framework.host.vm.graph_desc_info.0.slice_0 @@ -0,0 +1,4 @@ +op_name:Default/Cast-op6 op_type:Cast input_id:0 input_format:DefaultFormat input_data_type:40 input_shape:"32,3,224,224" output_id:0 output_format:DefaultFormat output_data_type:39 output_shape:"32,3,224,224" +op_name:Default/TransData-op7 op_type:TransData input_id:0 input_format:DefaultFormat input_data_type:39 input_shape:"32,3,224,224" output_id:0 output_format:NC1HWC0 output_data_type:39 output_shape:"32,1,224,224,16" +op_name:Default/network-WithLossCell/_backbone-ResNet/conv1-Conv2d/Cast-op5 op_type:Cast input_id:0 input_format:FracZ input_data_type:40 input_shape:"49,4,16,16" output_id:0 output_format:FracZ output_data_type:39 output_shape:"49,4,16,16" +op_name:Default/network-WithLossCell/_backbone-ResNet/layer1-SequentialCell/0-ResidualBlock/conv1-Conv2d/Cast-op28 op_type:Cast input_id:0 input_format:FracZ input_data_type:40 input_shape:"4,4,16,16" output_id:0 output_format:FracZ output_data_type:39 output_shape:"4,4,16,16" diff --git a/tests/ut/data/profiler_data/JOB1/Framework.host.vm.graph_desc_info.0.slice_0.done b/tests/ut/data/profiler_data/JOB1/Framework.host.vm.graph_desc_info.0.slice_0.done new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/ut/data/profiler_data/JOB1/Framework.host.vm.task_desc_info.0.slice_0 b/tests/ut/data/profiler_data/JOB1/Framework.host.vm.task_desc_info.0.slice_0 new file mode 100644 index 0000000000..e786b35e22 --- /dev/null +++ b/tests/ut/data/profiler_data/JOB1/Framework.host.vm.task_desc_info.0.slice_0 @@ -0,0 +1,4 @@ +Default/Cast-op6 32 51517 0 +Default/TransData-op7 32 51518 0 +Default/network-WithLossCell/_backbone-ResNet/conv1-Conv2d/Cast-op5 32 51519 0 +Default/network-WithLossCell/_backbone-ResNet/layer1-SequentialCell/0-ResidualBlock/conv1-Conv2d/Cast-op28 4 51522 0 diff --git a/tests/ut/data/profiler_data/JOB1/pipeline_profiling_0.json b/tests/ut/data/profiler_data/JOB1/pipeline_profiling_0.json new file mode 100644 index 0000000000..2f5fdca724 --- /dev/null +++ b/tests/ut/data/profiler_data/JOB1/pipeline_profiling_0.json @@ -0,0 +1,55 @@ +{ + "sampling_interval": 10, + "op_info": [ + { + "op_id": 4, + "op_type": "TFReader", + "num_workers": 4, + "metrics": null, + "children": [3] + }, + { + "op_id": 3, + "op_type": "TFReader", + "num_workers": 4, + "metrics": { + "output_queue": { + "size": [10, 20, 30], + "length": 64 + } + }, + "children": null + }, + { + "op_id": 2, + "op_type": "TFReader", + "num_workers": 4, + "metrics": { + "output_queue": { + "size": [10, 20, 30], + "length": 64 + } + }, + "children": null + }, + { + "op_id": 1, + "op_type": "Shuffle", + "num_workers": 1, + "metrics": { + "output_queue": { + "size": [10, 20, 30], + "length": 64 + } + }, + "children": [2, 4] + }, + { + "op_id": 0, + "op_type": "Batch", + "num_workers": 4, + "metrics": null, + "children": [1] + } + ] +} \ No newline at end of file diff --git a/tests/ut/data/profiler_data/JOB1/training_trace.46.dev.profiler_default_tag.0.slice_0 b/tests/ut/data/profiler_data/JOB1/training_trace.46.dev.profiler_default_tag.0.slice_0 new file mode 100644 index 0000000000000000000000000000000000000000..2fd8ba0bc98acdd59e95f57d52cfdbd2644593ae GIT binary patch literal 4800 zcmYk(3vA7I7{>8)^c3wmdZ!rO)Mh2Mj40}|n~HS_hA5TYYN^?b3}e$`?v!lBTsv$; z)O9wdgv?Ol(lJBKjOgmp&MKO<7_HgwdEPv7{z*^&{q+4^zBwLG&@&??C*1w%4WjAy z%<2W%+UowC3wdnep4~~EX}%yEKiVwxvH3mmX=e3{yWvdhD0k3{bG$iR4-2#wD!s-vkFzcXvh)C)^&8J~;&)$8?ydy11E za;-G3O58Iik7dCa9nR>DImk9x$pZjwk-FYbJ? zb2plM<%6E+-NiKZ!sgaHOj9p@m(*USskfdx62&m8KS0 z__6IY_2QoWBaYGBD<9l*>9PS+uui?ObERhLg(cq_#X0r*+!@^y2h!9F+wq2(dVQ{~ z#BZiv*pti6)Qhzjhh%X-^}>8r*);XSmLDHaQ!nho)rmCq;!ahe`84;+2X_i9XgW2x z7j~x1Oueu^l^r>!-cWZ&yVzzl^}^0iHdAk?Ynv2jre4^Jm1gS2+VXdjxSx7q)s@LK z^}_N_bfc-4R^F4QUYs8Y&7iqgJ~;pI;wvaFk2UDm#ore0Y2 zCuZuc@7msNW2RnM_9`>=V(p0`x4EBsVPivTXzGQn-sk-ct?GppAM(-Ei?a$tThiPs zADq=_`39PLVGHk?sTbDise_zTZv%J6tkgo9dSUDG%+%Y!wI!#SsTWqf!%V$c`%G>T z_fsz{CVC%Dy|CDZ$7$+?Mbwti)Qf%hre34DS3cOc?(pboSf^fCpSx!2h5Z>ik8|p6 z=+3B1979tt?7|!~^)_^EJ6|$WFYMZPX6nV-pC(Mct-3)>~-ql@Io4UDRoMa4#(SZ!`76W_$W`PQ797jQdF`H1)!Mm};ipFxR%fiV=KUY%&AC)f?{47&^}8)C>Ex*i60QuI=+dX6l8-oibA|)*jjE=NamS)%1v>sTcNR z*902wg9qmC*NUcI{O_$vsWc9`55M~fG$^L27nYV|re4^lC6_s;UcWozkBv5`Uf4F@ zVa}=7?@lQ%vN`p_PPDK&^s7d&re5Cp>QS1zKA%SJGmtxA7S^d3*0ID)y|8sv zqd2GDM(&KR|Js~-VcmudV++=FjFt=K-mG#sW-}< zaj01#O}((Uv(3~S<=WyC&D0A!z1&Q_SX-H0#QoF@`|#0TntEZ^j~=6`7k2zm2~EAY z^DXZ+ntSCF?LK!e8a)^5)Cx#)SEQ*X38=k(qj9T-&av&D0C~CeKX0So_@2H14NfSZY{0O}((C zCo^d3g{?o6NmDQG{H=c+&Asx8b)SigYias^VZUE6Q!nh**d}?{U%jz7w?!S-su$)P zZKmE>*LE((Ouew;MP};7+V|h|^9=REe!US%Q!lKAH-V;J*v!yYH1*=n!#bqW{s*!N B6kh-U literal 0 HcmV?d00001 diff --git a/tests/ut/data/profiler_data/JOB2/pipeline_profiling_0.json b/tests/ut/data/profiler_data/JOB2/pipeline_profiling_0.json new file mode 100644 index 0000000000..1357a35c83 --- /dev/null +++ b/tests/ut/data/profiler_data/JOB2/pipeline_profiling_0.json @@ -0,0 +1,48 @@ +{ + "sampling_interval": 10, + "op_info": [ + { + "op_id": 3, + "op_type": "TFReader", + "num_workers": 4, + "metrics": { + "output_queue": { + "size": [10, 20, 30], + "length": 64 + } + }, + "children": null + }, + { + "op_id": 2, + "op_type": "TFReader", + "num_workers": 4, + "metrics": { + "output_queue": { + "size": [10, 20, 30], + "length": 64 + } + }, + "children": null + }, + { + "op_id": 1, + "op_type": "Shuffle", + "num_workers": 1, + "metrics": { + "output_queue": { + "size": [10, 20, 30], + "length": 64 + } + }, + "children": [2, 3] + }, + { + "op_id": 0, + "op_type": "Batch", + "num_workers": 4, + "metrics": null, + "children": [1] + } + ] +} \ No newline at end of file diff --git a/tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.graph_desc_info.0.slice_0 b/tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.graph_desc_info.0.slice_0 new file mode 100644 index 0000000000..9b3e7b322c --- /dev/null +++ b/tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.graph_desc_info.0.slice_0 @@ -0,0 +1,4 @@ +op_name:Default/Cast-op6 op_type:Cast input_id:0 input_format:DefaultFormat input_data_type:40 input_shape:"32,3,224,224" output_id:0 output_format:DefaultFormat output_data_type:39 output_shape:"32,3,224,224" +op_name:Default/TransData-op7 op_type:TransData input_id:0 input_format:DefaultFormat input_data_type:39 input_shape:"32,3,224,224" output_id:0 output_format:NC1HWC0 output_data_type:39 output_shape:"32,1,224,224,16" +op_name:Default/network-WithLossCell/_backbone-ResNet/conv1-Conv2d/Cast-op5 op_type:Cast input_id:0 input_format:FracZ input_data_type:40 input_shape:"49,4,16,16" output_id:0 output_format:FracZ output_data_type:39 output_shape:"49,4,16,16" +op_name:Default/network-WithLossCell/_backbone-ResNet/layer1-SequentialCell/0-ResidualBlock/conv1-Conv2d/Cast-op28 op_type:Cast input_id:0 input_format:FracZ input_data_type:40 input_shape:"4,4,16,16" output_id:0 output_format:FracZ output_data_type:39 output_shape:"4,4,16,16" diff --git a/tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.point.0.slice_0 b/tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.point.0.slice_0 new file mode 100644 index 0000000000..01bcf6f3ca --- /dev/null +++ b/tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.point.0.slice_0 @@ -0,0 +1,2 @@ +1 Default/Cast-op6 +2 Default/TransData-op7 diff --git a/tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.task_desc_info.0.slice_0 b/tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.task_desc_info.0.slice_0 new file mode 100644 index 0000000000..e49673789f --- /dev/null +++ b/tests/ut/data/profiler_data/JOB4/data/Framework.host.vm.task_desc_info.0.slice_0 @@ -0,0 +1,4 @@ +Default/Cast-op6 32 1 0 +Default/TransData-op7 32 2 0 +Default/network-WithLossCell/_backbone-ResNet/conv1-Conv2d/Cast-op5 32 3 0 +Default/network-WithLossCell/_backbone-ResNet/layer1-SequentialCell/0-ResidualBlock/conv1-Conv2d/Cast-op28 4 4 0 diff --git a/tests/ut/data/profiler_data/JOB_AICPU/data/DATA_PREPROCESS.dev.AICPU.0.slice_0 b/tests/ut/data/profiler_data/JOB_AICPU/data/DATA_PREPROCESS.dev.AICPU.0.slice_0 new file mode 100644 index 0000000000000000000000000000000000000000..30fe89d6e98cd5d06cdd0492853db30cc1657947 GIT binary patch literal 890 zcmb`F%Wi`(5JmGT{s7tzHhx9QqEV!_n=C4Jh=m14K>>`6soKACO%oM@&tt`YOTq1 z*P&d$m6=W;-KsKc`vgJikLu3IuLj7K+4*4KV$(jWs@M_1DZvbYi(szy#yL~3+Ykhu zHut(PR{P7%iyT5SEj|`b+ogy?CL(|1yzE-#vQu2vdKUtqzViner>ZHTjQi+*lVAdv zGbzOd#gleN{qEKz<`IHdBra?4XA%(+z&I8xf*T~}Rz~a_iK!x-FfuMKB*sEuPHA{W zLJY57gOEZF9FPZb?ZiPBs;}HbZ2KltPUlBsYgOHu7FKY)AB!fLsoUOZ)Qtko4IKok yb)B^zUkm^5mtv4>o@c; None: + """Initialization before test case execution.""" + self.profiling_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), + '../../../data/profiler_data/' + 'JOB_AICPU/data')) + self.expect_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), + '../../../data/profiler_data/' + 'JOB_AICPU/expect')) + self.output_path = tempfile.mkdtemp(prefix='output_data_preprocess_aicpu_') + self.output_file = os.path.join(self.output_path, 'output_data_preprocess_aicpu_0.txt') + self.expect_file = os.path.join(self.expect_dir, 'output_data_preprocess_aicpu_0.txt') + + def test_aicpu_parser(self): + """Test the class of Aicpu Parser.""" + data = DataPreProcessParser(self.profiling_dir, self.output_file) + data.execute() + expect_result = get_result(self.expect_file) + result = get_result(self.output_file) + shutil.rmtree(self.output_path) + assert expect_result == result + + def test_aicpu_parser_file_not_exist(self): + """Test the class of Aicpu Parser.""" + profiling_dir = os.path.realpath(os.path.join(self.profiling_dir, 'data')) + data = DataPreProcessParser(profiling_dir, self.output_file) + data.execute() + shutil.rmtree(self.output_path) diff --git a/tests/ut/python/profiler/parser/test_framework_parser.py b/tests/ut/python/profiler/parser/test_framework_parser.py new file mode 100644 index 0000000000..d37bd19dd9 --- /dev/null +++ b/tests/ut/python/profiler/parser/test_framework_parser.py @@ -0,0 +1,128 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +"""Test the framework parser module.""" +import csv +import os +import shutil +import tempfile +from unittest import mock + +import pytest + +from mindspore.profiler.common.exceptions.exceptions import \ + ProfilerFileNotFoundException +from mindspore.profiler.parser.framework_parser import FrameworkParser +from tests.ut.python.profiler import PROFILER_DIR, RAW_DATA_BASE + + +def get_framework_result(file_path): + """ + Get framework result from the framework file. + + Args: + file_path (str): The framework file path. + + Returns: + list[list], the parsed framework information. + """ + result = [] + with open(file_path, 'r') as file: + csv_reader = csv.reader(file) + for row in csv_reader: + result.append(row) + return result + + +class TestFrameworkParser: + """Test the class of `FrameworkParser`.""" + def setup_method(self): + """Initialization before test case execution.""" + with mock.patch.object(FrameworkParser, '_raw_data_dir', RAW_DATA_BASE): + self._output_path_1 = tempfile.mkdtemp(prefix='test_framework_parser_') + self._parser_1 = FrameworkParser('JOB1', '0', self._output_path_1) + self._output_path_2 = tempfile.mkdtemp(prefix='test_framework_parser_') + self._parser_2 = FrameworkParser('JOB2', '0', self._output_path_2) + self._output_path_4 = tempfile.mkdtemp(prefix='test_framework_parser_') + self._parser_4 = FrameworkParser('JOB4', '0', self._output_path_4) + + def teardown_method(self) -> None: + """Clear up after test case execution.""" + shutil.rmtree(self._output_path_1) + shutil.rmtree(self._output_path_2) + shutil.rmtree(self._output_path_4) + + def test_save_path(self): + """Test the querying save path function.""" + expect_result = os.path.join(self._output_path_1, 'framework_raw_0.csv') + assert expect_result == self._parser_1.save_path + + expect_result = os.path.join(self._output_path_2, 'framework_raw_0.csv') + assert expect_result == self._parser_2.save_path + + def test_point_info(self): + """Test the querying point info function.""" + expect_result = { + 1: 'Default/Cast-op6', + 2: 'Default/TransData-op7' + } + assert expect_result == self._parser_4.point_info + + def test_to_task_id_full_op_name_dict(self): + """Test the querying task id and full operator name dict function.""" + expect_result = { + '51517': 'Default/Cast-op6', + '51518': 'Default/TransData-op7', + '51519': 'Default/network-WithLossCell/_backbone-ResNet/conv1-Conv2d/Cast-op5', + '51522': 'Default/network-WithLossCell/_backbone-ResNet/' + 'layer1-SequentialCell/0-ResidualBlock/conv1-Conv2d/Cast-op28' + } + assert expect_result == self._parser_1.to_task_id_full_op_name_dict() + assert expect_result == self._parser_2.to_task_id_full_op_name_dict() + + expect_result = { + '0_1': 'Default/Cast-op6', + '0_2': 'Default/TransData-op7', + '0_3': 'Default/network-WithLossCell/_backbone-ResNet/conv1-Conv2d/Cast-op5', + '0_4': 'Default/network-WithLossCell/_backbone-ResNet/layer1-SequentialCell/' + '0-ResidualBlock/conv1-Conv2d/Cast-op28' + } + assert expect_result == self._parser_4.to_task_id_full_op_name_dict() + + def test_parse(self): + """Test the parse function.""" + expect_framework_file = os.path.join(PROFILER_DIR, 'framework_raw_0.csv') + expect_framework_file = os.path.realpath(expect_framework_file) + expect_result = get_framework_result(expect_framework_file) + + self._parser_1.parse() + framework_file = os.path.join(self._output_path_1, 'framework_raw_0.csv') + result = get_framework_result(framework_file) + assert expect_result == result + + self._parser_2.parse() + framework_file = os.path.join(self._output_path_2, 'framework_raw_0.csv') + result = get_framework_result(framework_file) + assert expect_result == result + + @mock.patch('os.listdir') + @mock.patch('os.path.isdir') + def test_create_framework_parser_fail_1(self, *args): + """Test the function of fail to create framework parser.""" + args[0].return_value = True + args[1].return_value = [] + with pytest.raises(ProfilerFileNotFoundException) as exc_info: + FrameworkParser('JOB1', '0') + assert exc_info.value.error_code == '50546084' + assert exc_info.value.message == 'The file not found.' diff --git a/tests/ut/python/profiler/parser/test_minddata_pipeline_parser.py b/tests/ut/python/profiler/parser/test_minddata_pipeline_parser.py new file mode 100644 index 0000000000..65ca7d7717 --- /dev/null +++ b/tests/ut/python/profiler/parser/test_minddata_pipeline_parser.py @@ -0,0 +1,93 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +"""Test the minddata pipeline parser module.""" +import csv +import os +import shutil +import tempfile + +from mindspore.profiler.parser.minddata_pipeline_parser import \ + MinddataPipelineParser +from tests.ut.python.profiler import PROFILER_DIR, RAW_DATA, RAW_DATA_JOB2 + + +def get_minddata_pipeline_result(file_path): + """ + Get minddata pipeline result from the minddata pipeline file. + + Args: + file_path (str): The minddata pipeline file path. + + Returns: + list[list], the parsed minddata pipeline information. + """ + result = [] + with open(file_path, 'r') as file: + csv_reader = csv.reader(file) + for row in csv_reader: + result.append(row) + return result + + +class TestMinddataPipelineParser: + """Test the class of `MinddataPipelineParser`.""" + def setup_method(self): + """Initialization before test case execution.""" + self._output_path_1 = tempfile.mkdtemp( + prefix='test_minddata_pipeline_parser_' + ) + self._parser_1 = MinddataPipelineParser( + RAW_DATA, '0', self._output_path_1 + ) + + self._output_path_2 = tempfile.mkdtemp( + prefix='test_minddata_pipeline_parser_' + ) + self._parser_2 = MinddataPipelineParser( + RAW_DATA_JOB2, '0', self._output_path_2 + ) + + def teardown_method(self) -> None: + """Clear up after test case execution.""" + shutil.rmtree(self._output_path_1) + shutil.rmtree(self._output_path_2) + + def test_save_path(self): + """Test the querying save path function.""" + expect_result = os.path.join( + self._output_path_1, 'minddata_pipeline_raw_0.csv' + ) + assert expect_result == self._parser_1.save_path + + def test_parse(self): + """Test the parse function.""" + expect_pipeline_file = os.path.join( + PROFILER_DIR, 'minddata_pipeline_raw_0.csv' + ) + expect_result = get_minddata_pipeline_result(expect_pipeline_file) + + self._parser_1.parse() + pipeline_file = os.path.join( + self._output_path_1, 'minddata_pipeline_raw_0.csv' + ) + result = get_minddata_pipeline_result(pipeline_file) + assert expect_result == result + + self._parser_2.parse() + pipeline_file = os.path.join( + self._output_path_2, 'minddata_pipeline_raw_0.csv' + ) + result = get_minddata_pipeline_result(pipeline_file) + assert expect_result == result