Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/scripts/build_tests/pretty_diff_size.py
Views: 1799
#!/usr/bin/env python12'''3This script intend to provide a pretty size diff between two binaries.45AP_FLAKE8_CLEAN6'''78import os9from argparse import ArgumentParser10from tabulate import tabulate1112parser = ArgumentParser(description="Print binary size difference with master.")13parser.add_argument("-m", "--master", dest='master', type=str, help="Master Binaries Path", required=True)14parser.add_argument("-s", "--second", dest='second', type=str, help="Second Binaries Path", required=True)1516args = parser.parse_args()171819def sizes_for_file(filepath):20"""Get binary size information with size."""21print("Get binary size of %s" % filepath)22cmd = "size %s" % (filepath,)23stuff = os.popen(cmd).read()24lines = stuff.splitlines()[1:]25size_list = []26for line in lines:27row = line.strip().split()28size_list.append(dict(29text=int(row[0]),30data=int(row[1]),31bss=int(row[2]),32total=int(row[3]),33))3435# Get the size of .crash_log to remove it from .bss reporting36cmd = "size -A %s" % (filepath,)37output = os.popen(cmd).read()38lines = output.splitlines()[1:]39for line in lines:40if ".crash_log" in line:41row = line.strip().split()42size_list[0]["crash_log"] = int(row[1])43break4445# check if crash_log wasn't found and set to 0B if not found46# FIX ME : so it doesn't report Flash_Free 0B for non-ARM boards47if size_list[0].get("crash_log") is None:48size_list[0]["crash_log"] = 04950return size_list515253def print_table(summary_data_list_second, summary_data_list_master):54"""Print the binaries size diff on a table."""55print_data = []56print("")57# print(summary_data_list_second)58# print(summary_data_list_master)59for name in summary_data_list_second[0]:60for master_name in summary_data_list_master[0]:61if name == master_name:62col_data = [name]63for key in ["text", "data", "bss", "total"]:64bvalue = summary_data_list_second[0][name].get(key)65mvalue = summary_data_list_master[0][name].get(key)6667# BSS: remove the portion occupied by crash_log as the command `size binary.elf`68# reports BSS with crash_log included69if key == "bss":70mvalue = (summary_data_list_master[0][name].get("bss") -71summary_data_list_master[0][name].get("crash_log"))72bvalue = (summary_data_list_second[0][name].get("bss") -73summary_data_list_second[0][name].get("crash_log"))7475# Total Flash Cost = Data + Text76if key == "total":77mvalue = (summary_data_list_master[0][name].get("text") +78summary_data_list_master[0][name].get("data"))79bvalue = (summary_data_list_second[0][name].get("text") +80summary_data_list_second[0][name].get("data"))81diff = (bvalue - mvalue) * 100.0 / mvalue82signum = "+" if diff > 0.0 else ""83print_diff = str(bvalue - mvalue)84print_diff += " (" + signum + "%0.4f%%" % diff + ")"85col_data.append(print_diff)8687# Append free flash space which is equivalent to crash_log's size88col_data.append(str(summary_data_list_second[0][name].get("crash_log")))89print_data.append(col_data)90print(tabulate(print_data, headers=["Binary Name", "Text [B]", "Data [B]", "BSS (B)",91"Total Flash Change [B] (%)", "Flash Free After PR (B)"]))92# Get the GitHub Actions summary file path93summary_file = os.getenv('GITHUB_STEP_SUMMARY')94if summary_file:95# Append the output to the summary file96with open(summary_file, 'a') as f:97f.write("### Diff summary\n")98f.write(tabulate(print_data, headers=[99"Binary Name",100"Text [B]",101"Data [B]",102"BSS (B)",103"Total Flash Change [B] (%)",104"Flash Free After PR (B)"105], tablefmt="github"))106f.write("\n")107108109def extract_binaries_size(path):110"""Search and extract binary size for each binary in the given path."""111print("Extracting binaries size on %s" % path)112binaries_list = []113for file in os.listdir(args.master):114fileNoExt = os.path.splitext(file)[0]115binaries_list.append(fileNoExt)116binaries_list = list(dict.fromkeys(binaries_list))117if len(binaries_list) == 0:118print("Failed to get binaries")119120size_dict = None121for binaries in binaries_list:122binary_path = os.path.join(path, binaries)123parsed = sizes_for_file(binary_path)124if size_dict is None:125size_dict = [{binaries.lower(): parsed[0]}]126else:127size_dict[0].update({binaries.lower(): parsed[0]})128print("Success !")129return size_dict130131132master_dict = extract_binaries_size(args.master)133second_dict = extract_binaries_size(args.second)134135print_table(second_dict, master_dict)136137138