xref: /developtools/hiperf/script/make_report.py (revision 48f512ce)
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#   Copyright (c) 2021 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 os
17import sys
18import time
19import argparse
20from hiperf_utils import get_lib
21from hiperf_utils import dir_check
22from hiperf_utils import file_check
23
24
25def get_used_binaries(perf_data, report_file, local_lib_dir, html_template):
26    if local_lib_dir:
27        get_lib().ReportUnwindJson(perf_data.encode("utf-8"),
28                                   'json.txt'.encode("utf-8"),
29                                   local_lib_dir.encode("utf-8"))
30    else:
31        get_lib().ReportJson(perf_data.encode("utf-8"),
32                             'json.txt'.encode("utf-8"))
33    time.sleep(2)
34    with open('json.txt', 'r') as json_file:
35        all_json = json_file.read()
36        template = os.path.join(html_template, 'report.html')
37    with open(template, 'r', encoding='utf-8') as html_file:
38        html_str = html_file.read()
39    with open(report_file, 'w', encoding='utf-8') as report_html_file:
40        report_html_file.write(html_str + all_json + '</script>'
41                                      ' </body>'
42                                      ' </html>')
43    dirname, _ = os.path.split(os.path.abspath(sys.argv[0]))
44    abs_path = os.path.join(dirname, report_file)
45    print("save to %s success" % abs_path)
46    os.remove('json.txt')
47
48
49def main():
50    parser = argparse.ArgumentParser(description=""" To make a report, you
51    need to enter the data source and the path of the report.""")
52    parser.add_argument('-i', '--perf_data', default='perf.data',
53                        type=file_check, help=""" The path of profiling
54                        data.""")
55    parser.add_argument('-r', '--report_html', default='hiperf_report.html',
56                        help="""the path of the report.""")
57    parser.add_argument('-l', '--local_lib_dir', type=dir_check, default='./binary_cache',
58                        help="""Path to find symbol dir use to
59                         do offline unwind stack""")
60    parser.add_argument('-t', '--html_template', default='./',
61                        type=dir_check, help=""" The path of report html template
62                        """)
63    args = parser.parse_args()
64
65    get_used_binaries(args.perf_data, args.report_html, args.local_lib_dir, args.html_template)
66
67
68if __name__ == '__main__':
69    main()
70