• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#   Copyright (c) 2025 Huawei Device Co., Ltd.
4#   Licensed under the Apache License, Version 2.0 (the "License");
5#   you may not use this file except in compliance with the License.
6#   You may obtain a copy of the License at
7#
8#       http://www.apache.org/licenses/LICENSE-2.0
9#
10#   Unless required by applicable law or agreed to in writing, software
11#   distributed under the License is distributed on an "AS IS" BASIS,
12#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#   See the License for the specific language governing permissions and
14#   limitations under the License.
15
16import pytest
17import os
18import argparse
19from hiperf_utils import PerformanceProfile
20from hiperf_utils import dir_check
21
22
23def check_args(args):
24    if (not args.package_name) and args.ability:
25        raise Exception('-a can only be used when profiling an OHOS '
26                        'package_name.')
27    return True
28
29
30def parser_add_argument():
31    description = "Collect performance sampling information of" \
32                  " running [command]."
33    parser = argparse.ArgumentParser(description=description)
34    target_group = parser.add_argument_group(title='Select profiling target') \
35        .add_mutually_exclusive_group(required=False)
36    target_group.add_argument('-app', '--package_name',
37                              help="""Collect profile info for an OHOS app""")
38
39    target_group.add_argument('-lp', '--local_program',
40                              help="""Collect profile info
41                               for an local program.""")
42
43    target_group.add_argument('-cmd',
44                              help="""Running a command on the OHOS device.
45                              like as : -cmd "'ps -ef'".
46                              the ps will open as child process of hiperf
47                              and sample this process.""")
48
49    target_group.add_argument('-p', '--pid', nargs='*',
50                              help="""Limit the process id of the collection
51                         target.""")
52
53    target_group.add_argument('-t', '--tid', nargs='*',
54                              help="""Limit the thread id of the collection
55                        target.""")
56
57    target_group.add_argument('-sw', '--system_wide', action='store_true',
58                              help="""Collect system-wide information.
59                        This requires CAP_PERFMON (since Linux 5.8) or
60                        CAP_SYS_ADMIN capability or a
61                        /proc/sys/kernel/perf_event_paranoid
62                        value of less than 1.""")
63    record_group = parser.add_argument_group('Select recording options')
64    record_group.add_argument('-a', '--ability',
65                              help="""Used with -p. Profile the launch time of
66                        an ability in an OHOS app. The app will be started or
67                        restarted to run the ability.
68                        Like : -a .MainAbility """)
69
70    record_group.add_argument('-r', '--record_options',
71                              default='-f 1000 -d 2 -s dwarf',
72                              help="""Set recording options for `hiperf record`
73                         command. Default is "'-f 1000 -d 2 -s dwarf'".""")
74
75    record_group.add_argument('-lib', '--local_lib_dir', type=dir_check,
76                              help="""When profiling an OHOS app containing
77                         local thelocal libraries are usually stripped and lake
78                         of symbols and debug information to provide good
79                         profiling result. By using -lib, you tell
80                         command_script.py the path storing unstripped local
81                         libraries, and script will search all shared libraries
82                         with suffix .so in the directory. Then the local
83                         libraries will be downloaded on device and collected
84                         in build_cache.""")
85
86    record_group.add_argument('-o', '--output_perf_data', default='perf.data',
87                              help='The path to store profiling data. '
88                                   'Default is perf.data.')
89
90    other_group = parser.add_argument_group('Other options')
91    other_group.add_argument('--not_hdc_root', action='store_true',
92                             help="""Force hdc to run in non root mode. """)
93    args = parser.parse_args()
94    args.package_name = 'hiview'
95    return args
96
97
98class TestRecord:
99    def setup(self):
100        print("TestRecord setup")
101
102    @pytest.mark.L0
103    def test_package_name(self):
104        if os.path.exists('./perf.data') == True:
105            os.remove('./perf.data')
106        args = parser_add_argument()
107        check_args(args)
108        profiler = PerformanceProfile(args)
109        profiler.profile()
110        assert os.path.exists('./perf.data') == True
111