148f512ceSopenharmony_ci#!/usr/bin/env python
248f512ceSopenharmony_ci# -*- coding: utf-8 -*-
348f512ceSopenharmony_ci#   Copyright (c) 2021 Huawei Device Co., Ltd.
448f512ceSopenharmony_ci#   Licensed under the Apache License, Version 2.0 (the "License");
548f512ceSopenharmony_ci#   you may not use this file except in compliance with the License.
648f512ceSopenharmony_ci#   You may obtain a copy of the License at
748f512ceSopenharmony_ci#
848f512ceSopenharmony_ci#       http://www.apache.org/licenses/LICENSE-2.0
948f512ceSopenharmony_ci#
1048f512ceSopenharmony_ci#   Unless required by applicable law or agreed to in writing, software
1148f512ceSopenharmony_ci#   distributed under the License is distributed on an "AS IS" BASIS,
1248f512ceSopenharmony_ci#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1348f512ceSopenharmony_ci#   See the License for the specific language governing permissions and
1448f512ceSopenharmony_ci#   limitations under the License.
1548f512ceSopenharmony_ci
1648f512ceSopenharmony_ciimport os
1748f512ceSopenharmony_ciimport sys
1848f512ceSopenharmony_ciimport time
1948f512ceSopenharmony_ciimport argparse
2048f512ceSopenharmony_cifrom hiperf_utils import get_lib
2148f512ceSopenharmony_cifrom hiperf_utils import dir_check
2248f512ceSopenharmony_cifrom hiperf_utils import file_check
2348f512ceSopenharmony_ci
2448f512ceSopenharmony_ci
2548f512ceSopenharmony_cidef get_used_binaries(perf_data_base, perf_data_target, report_file,
2648f512ceSopenharmony_ci                      local_lib_base, local_lib_target):
2748f512ceSopenharmony_ci    if local_lib_base:
2848f512ceSopenharmony_ci        get_lib().ReportUnwindJson(perf_data_base.encode("utf-8"),
2948f512ceSopenharmony_ci                                   'json_base.txt'.encode("utf-8"),
3048f512ceSopenharmony_ci                                   local_lib_base.encode("utf-8"))
3148f512ceSopenharmony_ci    else:
3248f512ceSopenharmony_ci        get_lib().ReportJson(perf_data_base.encode("utf-8"),
3348f512ceSopenharmony_ci                             'json_base.txt'.encode("utf-8"))
3448f512ceSopenharmony_ci
3548f512ceSopenharmony_ci    if local_lib_target:
3648f512ceSopenharmony_ci        get_lib().ReportUnwindJson(perf_data_target.encode("utf-8"),
3748f512ceSopenharmony_ci                                   'json_target.txt'.encode("utf-8"),
3848f512ceSopenharmony_ci                                   local_lib_target.encode("utf-8"))
3948f512ceSopenharmony_ci    else:
4048f512ceSopenharmony_ci        get_lib().ReportJson(perf_data_target.encode("utf-8"),
4148f512ceSopenharmony_ci                             'json_target.txt'.encode("utf-8"))
4248f512ceSopenharmony_ci    time.sleep(2)
4348f512ceSopenharmony_ci
4448f512ceSopenharmony_ci    with open('json_base.txt', 'r') as json_file:
4548f512ceSopenharmony_ci        all_json_base = json_file.read()
4648f512ceSopenharmony_ci    with open('json_target.txt', 'r') as json_file:
4748f512ceSopenharmony_ci        all_json_target = json_file.read()
4848f512ceSopenharmony_ci
4948f512ceSopenharmony_ci    with open('report-diff.html', 'r', encoding='utf-8') as html_file:
5048f512ceSopenharmony_ci        html_str = html_file.read()
5148f512ceSopenharmony_ci
5248f512ceSopenharmony_ci    with open(report_file, 'w', encoding='utf-8') as report_html_file:
5348f512ceSopenharmony_ci        combine_html(all_json_base, all_json_target)
5448f512ceSopenharmony_ci        report_html_file.write(html_str + combine_html(all_json_base, all_json_target))
5548f512ceSopenharmony_ci
5648f512ceSopenharmony_ci    dirname, _ = os.path.split(os.path.abspath(sys.argv[0]))
5748f512ceSopenharmony_ci    abs_path = os.path.join(dirname, report_file)
5848f512ceSopenharmony_ci    print("save to %s success" % abs_path)
5948f512ceSopenharmony_ci    os.remove("json_base.txt")
6048f512ceSopenharmony_ci    os.remove("json_target.txt")
6148f512ceSopenharmony_ci
6248f512ceSopenharmony_ci
6348f512ceSopenharmony_ci
6448f512ceSopenharmony_cidef combine_html(json_1, json_2):
6548f512ceSopenharmony_ci    return '\n<script id = "record_data_diff_1" type = "application/json">' + \
6648f512ceSopenharmony_ci           json_1 + '</script>\n' \
6748f512ceSopenharmony_ci                    '<script id = "record_data_diff_2" type = "application/json">' + \
6848f512ceSopenharmony_ci           json_2 + '</script> </body> </html>'
6948f512ceSopenharmony_ci
7048f512ceSopenharmony_ci
7148f512ceSopenharmony_cidef main():
7248f512ceSopenharmony_ci    parser = argparse.ArgumentParser(description=""" To make a report, you
7348f512ceSopenharmony_ci    need to enter the data source and the path of the report.""")
7448f512ceSopenharmony_ci    parser.add_argument('-b', '--base', default='perf.data',
7548f512ceSopenharmony_ci                        type=file_check, help=""" The path of base profiling
7648f512ceSopenharmony_ci                        data.""")
7748f512ceSopenharmony_ci    parser.add_argument('-t', '--target', default='perf.data',
7848f512ceSopenharmony_ci                        type=file_check, help=""" The path of target profiling
7948f512ceSopenharmony_ci                            data.""")
8048f512ceSopenharmony_ci    parser.add_argument('-r', '--report_html', default='hiperf_report.html',
8148f512ceSopenharmony_ci                        help="""the path of the report.""")
8248f512ceSopenharmony_ci    parser.add_argument('-lb', '--lib_base_dir', type=dir_check,
8348f512ceSopenharmony_ci                        help="""Path to find symbol dir use to
8448f512ceSopenharmony_ci                         base data offline unwind stack""")
8548f512ceSopenharmony_ci    parser.add_argument('-lt', '--lib_target_dir', type=dir_check,
8648f512ceSopenharmony_ci                        help="""Path to find symbol dir use to
8748f512ceSopenharmony_ci                            target data offline unwind stack""")
8848f512ceSopenharmony_ci    args = parser.parse_args()
8948f512ceSopenharmony_ci
9048f512ceSopenharmony_ci    get_used_binaries(args.base,args.target, args.report_html,
9148f512ceSopenharmony_ci                      args.lib_base_dir, args.lib_target_dir)
9248f512ceSopenharmony_ci
9348f512ceSopenharmony_ci
9448f512ceSopenharmony_ciif __name__ == '__main__':
9548f512ceSopenharmony_ci    main()
96