You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_aicpu_parser.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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."""
  16. import os
  17. import tempfile
  18. import shutil
  19. from unittest import TestCase
  20. from mindinsight.profiler.parser.aicpu_data_parser import DataPreProcessParser
  21. def get_result(file_path):
  22. """
  23. Get result from the aicpu file.
  24. Args:
  25. file_path (str): The aicpu file path.
  26. Returns:
  27. list[list], the parsed aicpu information.
  28. """
  29. result = []
  30. try:
  31. file = open(file_path, 'r')
  32. result.append(file.read())
  33. return result
  34. finally:
  35. if file:
  36. file.close()
  37. class TestAicpuParser(TestCase):
  38. """Test the class of Aicpu Parser."""
  39. def setUp(self) -> None:
  40. """Initialization before test case execution."""
  41. self.profiling_dir = os.path.realpath(os.path.join(os.path.dirname(__file__),
  42. '../../../utils/resource/'
  43. 'JOB_AICPU/data'))
  44. self.expect_dir = os.path.realpath(os.path.join(os.path.dirname(__file__),
  45. '../../../utils/resource/'
  46. 'JOB_AICPU/expect'))
  47. self.output_path = tempfile.mkdtemp(prefix='output_data_preprocess_aicpu_')
  48. self.output_file = os.path.join(self.output_path, 'output_data_preprocess_aicpu_0.txt')
  49. self.expect_file = os.path.join(self.expect_dir, 'output_data_preprocess_aicpu_0.txt')
  50. def test_aicpu_parser(self):
  51. """Test the class of Aicpu Parser."""
  52. data = DataPreProcessParser(self.profiling_dir, self.output_file)
  53. data.execute()
  54. expect_result = get_result(self.expect_file)
  55. result = get_result(self.output_file)
  56. shutil.rmtree(self.output_path)
  57. assert expect_result == result
  58. def test_aicpu_parser_file_not_exist(self):
  59. """Test the class of Aicpu Parser."""
  60. profiling_dir = os.path.realpath(os.path.join(self.profiling_dir, 'data'))
  61. data = DataPreProcessParser(profiling_dir, self.output_file)
  62. data.execute()
  63. shutil.rmtree(self.output_path)