From 70857132f7dac1a7f07892a7e967d8c00c8e3167 Mon Sep 17 00:00:00 2001 From: l00816237 Date: Fri, 22 Dec 2023 11:22:53 +0800 Subject: [PATCH] add ut of utils and common_func --- profiler/test/resource/event_list.json | 26 +++++ profiler/test/resource/test.csv | 9 ++ .../common_func/test_path_manager.py | 107 ++++++++++++++++++ .../prof_bean/test_step_trace_time_bean.py | 13 +++ .../compare_tools/utils/test_file_reader.py | 18 +++ .../compare_tools/utils/test_name_function.py | 42 +++++++ .../compare_tools/utils/test_tree_builder.py | 41 +++++++ 7 files changed, 256 insertions(+) create mode 100644 profiler/test/resource/event_list.json create mode 100644 profiler/test/resource/test.csv create mode 100644 profiler/test/ut/cluster_analyse/common_func/test_path_manager.py create mode 100644 profiler/test/ut/cluster_analyse/prof_bean/test_step_trace_time_bean.py create mode 100644 profiler/test/ut/compare_tools/utils/test_file_reader.py create mode 100644 profiler/test/ut/compare_tools/utils/test_name_function.py create mode 100644 profiler/test/ut/compare_tools/utils/test_tree_builder.py diff --git a/profiler/test/resource/event_list.json b/profiler/test/resource/event_list.json new file mode 100644 index 000000000..653c847f1 --- /dev/null +++ b/profiler/test/resource/event_list.json @@ -0,0 +1,26 @@ +[ + { + "pid": 0, + "tid": 0, + "args": { + "Input Dims": [[1,1], [1,1]], + "name": 0 + }, + "ts": 0, + "dur": 1, + "ph": "M", + "name": "process_name" + }, + { + "pid": 1, + "tid": 1, + "args": { + "Input Dims": [[1,1], [1,1]], + "name": 1 + }, + "ts": 1, + "dur": 1, + "ph": "M", + "name": "process_name" + } +] diff --git a/profiler/test/resource/test.csv b/profiler/test/resource/test.csv new file mode 100644 index 000000000..33a231187 --- /dev/null +++ b/profiler/test/resource/test.csv @@ -0,0 +1,9 @@ +Name,Type,Accelerator Core,Start Time(us),Duration(us),Wait Time(us),Block Dim +Cast16,N/A,N/A,1702866902796675,932.0498,0,0 +trans_Cast_19,N/A,N/A,1702866902996859,5.86,199251.9502,0 +trans_TransData_20,N/A,N/A,1702866902996917,7.77,52.14,0 +trans_Cast_21,N/A,N/A,1702866902996929,4.35,4.23,0 +Conv2D17,N/A,N/A,1702866902996956.8,24.26,23.4,0 +trans_TransData_22,N/A,N/A,1702866902996981,23.98,0,0 +Add18,N/A,N/A,1702866903139579,1.22,142574.02,0 +atomic_memset-1_18_186609_1,N/A,N/A,1702866903316761.5,1.36,177181.28,0 diff --git a/profiler/test/ut/cluster_analyse/common_func/test_path_manager.py b/profiler/test/ut/cluster_analyse/common_func/test_path_manager.py new file mode 100644 index 000000000..8693ed33d --- /dev/null +++ b/profiler/test/ut/cluster_analyse/common_func/test_path_manager.py @@ -0,0 +1,107 @@ +import unittest +import os +import time +import pytest + +from common_func.path_manager import PathManager + + +PATH_DIR = "resource" +PATH_FILE = "resource/test.csv" +PATH_TEMP = "temp" + + +class TestPathManager(unittest.TestCase): + + def test_check_input_directory_path(self): + with pytest.raises(RuntimeError) as error: + PathManager.check_input_directory_path(PATH_FILE) + PathManager.check_input_directory_path(PATH_DIR) + + def test_check_input_file_path(self): + with pytest.raises(RuntimeError) as error: + PathManager.check_input_file_path(PATH_DIR) + PathManager.check_input_file_path(PATH_FILE) + + def test_check_path_length(self): + path_max = "a" * 4097 + name_max = "a" * 257 + path_with_name_max = "a/" + name_max + with pytest.raises(RuntimeError) as error: + PathManager.check_input_directory_path(path_max) + with pytest.raises(RuntimeError) as error: + PathManager.check_input_directory_path(path_with_name_max) + PathManager.check_path_length(PATH_FILE) + + def test_input_path_common_check(self): + path_max = "a" * 4097 + name_max = "a" * 257 + path_with_name_max = "a/" + name_max + with pytest.raises(RuntimeError) as error: + PathManager.input_path_common_check(path_max) + with pytest.raises(RuntimeError) as error: + PathManager.input_path_common_check(path_with_name_max) + with pytest.raises(RuntimeError) as error: + PathManager.input_path_common_check(PATH_DIR + "!@~#$%") + PathManager.input_path_common_check(PATH_FILE) + + def test_check_path_owner_consistent(self): + PathManager.check_path_owner_consistent(PATH_DIR) + + def test_check_path_writeable(self): + link_name = "test_link" + str(time.time()) + os.symlink(PATH_FILE, link_name) + with pytest.raises(RuntimeError) as error: + PathManager.check_path_writeable(link_name) + PathManager.check_path_writeable(PATH_DIR) + os.unlink(link_name) + + def test_check_path_readable(self): + link_name = "test_link" + str(time.time()) + os.symlink(PATH_FILE, link_name) + with pytest.raises(RuntimeError) as error: + PathManager.check_path_readable(link_name) + PathManager.check_path_readable(PATH_DIR) + os.unlink(link_name) + + def test_remove_path_safety(self): + path = PATH_TEMP + str(time.time()) + os.makedirs(path) + link_name = "test_link" + str(time.time()) + os.symlink(PATH_FILE, link_name) + with pytest.raises(RuntimeError) as error: + PathManager.remove_path_safety(link_name) + PathManager.remove_path_safety(path + "not_exist") + PathManager.remove_path_safety(path) + os.unlink(link_name) + + def test_make_dir_safety(self): + path = PATH_TEMP + str(time.time()) + link_name = "test_link" + str(time.time()) + os.symlink(PATH_FILE, link_name) + with pytest.raises(RuntimeError) as error: + PathManager.make_dir_safety(link_name) + PathManager.make_dir_safety(path) + os.removedirs(path) + os.unlink(link_name) + + def test_create_file_safety(self): + path = PATH_TEMP + str(time.time()) + link_name = "test_link" + str(time.time()) + os.symlink(PATH_FILE, link_name) + with pytest.raises(RuntimeError) as error: + PathManager.create_file_safety(link_name) + PathManager.create_file_safety(path) + os.remove(path) + os.unlink(link_name) + + def test_get_realpath(self): + path = PATH_TEMP + str(time.time()) + real_path = PathManager.get_realpath(path) + link_name = "test_link" + str(time.time()) + os.symlink(PATH_FILE, link_name) + with pytest.raises(RuntimeError) as error: + PathManager.get_realpath(link_name) + self.assertTrue(real_path.endswith(path)) + os.unlink(link_name) + diff --git a/profiler/test/ut/cluster_analyse/prof_bean/test_step_trace_time_bean.py b/profiler/test/ut/cluster_analyse/prof_bean/test_step_trace_time_bean.py new file mode 100644 index 000000000..e369df484 --- /dev/null +++ b/profiler/test/ut/cluster_analyse/prof_bean/test_step_trace_time_bean.py @@ -0,0 +1,13 @@ +import unittest + +from prof_bean.step_trace_time_bean import StepTraceTimeBean + + +class TestStepTraceTimeBean(unittest.TestCase): + + def test(self): + data = {"Step": 0, "Attr1": 1, "Attr2": 2} + bean = StepTraceTimeBean(data) + self.assertEqual(bean.row, [1.0, 2.0]) + self.assertEqual(bean.step, 0) + self.assertEqual(bean.all_headers, ['Step', 'Type', 'Index', 'Attr1', 'Attr2']) diff --git a/profiler/test/ut/compare_tools/utils/test_file_reader.py b/profiler/test/ut/compare_tools/utils/test_file_reader.py new file mode 100644 index 000000000..7e755a69c --- /dev/null +++ b/profiler/test/ut/compare_tools/utils/test_file_reader.py @@ -0,0 +1,18 @@ +import unittest + +from utils.file_reader import FileReader + + +class TestFileReader(unittest.TestCase): + + def test_read_trace_file(self): + json_data = FileReader.read_trace_file("resource/event_list.json") + self.assertEqual(len(json_data), 2) + + def test_read_csv_file(self): + csv = FileReader.read_csv_file("resource/test.csv") + self.assertEqual(len(csv), 8) + + def test_check_json_type(self): + t = FileReader.check_json_type("resource/event_list.json") + self.assertEqual(t, 1) diff --git a/profiler/test/ut/compare_tools/utils/test_name_function.py b/profiler/test/ut/compare_tools/utils/test_name_function.py new file mode 100644 index 000000000..4775c49b2 --- /dev/null +++ b/profiler/test/ut/compare_tools/utils/test_name_function.py @@ -0,0 +1,42 @@ +import unittest +import json + +from utils.name_function import NameFunction +from utils.tree_builder import TreeBuilder +from utils.torch_op_node import TorchOpNode + + +class Args: + def __init__(self, **kwargs): + for key, value in kwargs.items(): + setattr(self, key, value) + + +args = {"op_name_map": {}, "use_input_shape": True} +args = Args(**args) +func = NameFunction(args) + + +class TestNameFunction(unittest.TestCase): + node = None + + @classmethod + def setUpClass(cls) -> None: + super().setUpClass() + cls.node = TorchOpNode(event={"pid": 0, "tid": 0, "args": {"Input Dims": [[1, 1], [1, 1]], "name": 0}, + "ts": 0, "dur": 1, "ph": "M", "name": "process_name"}) + + def test_get_name(self): + self.assertEqual(NameFunction.get_name(self.node), "process_name") + + def test_get_full_name(self): + self.assertEqual(NameFunction.get_full_name(self.node), "process_name1,1;\r\n1,1") + + def test_get_name_function(self): + self.assertEqual(func.get_name_func(), func.get_full_map_name) + + def test_get_map_name(self): + self.assertEqual(func.get_map_name(self.node), "process_name") + + def test_get_full_map_name(self): + self.assertEqual(func.get_full_map_name(self.node), "process_name1,1;\r\n1,1") diff --git a/profiler/test/ut/compare_tools/utils/test_tree_builder.py b/profiler/test/ut/compare_tools/utils/test_tree_builder.py new file mode 100644 index 000000000..9d422046b --- /dev/null +++ b/profiler/test/ut/compare_tools/utils/test_tree_builder.py @@ -0,0 +1,41 @@ +import unittest +import json + +from utils.torch_op_node import TorchOpNode +from utils.tree_builder import TreeBuilder + + +class TestUtils(unittest.TestCase): + tree = None + + @classmethod + def setUpClass(cls) -> None: + super().setUpClass() + cls.tree = TorchOpNode() + child_1 = TorchOpNode(event={"pid": 0, "tid": 0, "args": {"Input Dims": [[1, 1], [1, 1]], "name": 0}, + "ts": 0, "dur": 1, "ph": "M", "name": "process_name"}, parent_node=cls.tree) + child_2 = TorchOpNode(event={"pid": 1, "tid": 1, "args": {"Input Dims": [[1, 1], [1, 1]], "name": 1}, + "ts": 1, "dur": 1, "ph": "M", "name": "process_name"}, parent_node=cls.tree) + cls.tree.add_child_node(child_1) + cls.tree.add_child_node(child_2) + + def test_build_tree(self): + with open('resource/event_list.json', 'r') as f: + event_list = json.load(f) + tree = TreeBuilder.build_tree(event_list) + child_nodes = tree.child_nodes + self.assertEqual(len(tree._child_nodes), 2) + self.assertEqual(child_nodes[0].parent, tree) + self.assertEqual(child_nodes[0].start_time, 0) + self.assertEqual(child_nodes[0].end_time, 1) + + def test_update_tree(self): + flow_kernel_dict = {0: [0, 1], 1: [0, 1]} + memory_allocated_list = [ + {"ts": 0, "Allocation Time(us)": 1, "Release Time(us)": 3, "Name": "test", "Size(KB)": 1}] + TreeBuilder.update_tree_node(self.tree, flow_kernel_dict, memory_allocated_list) + child_nodes = self.tree.child_nodes + self.assertEqual(child_nodes[0].kernel_num, 2) + self.assertEqual(child_nodes[1].kernel_num, 2) + self.assertEqual(len(TreeBuilder.get_total_kernels(self.tree)), 4) + self.assertEqual(TreeBuilder.get_total_memory(self.tree)[0].size, 1) -- Gitee