From 5779f4dc624fd7f4ac71e5a961a13e2d81096767 Mon Sep 17 00:00:00 2001 From: caolili123 Date: Mon, 24 Feb 2025 10:30:18 +0800 Subject: [PATCH] add run_count Signed-off-by: caolili123 --- run_sendable.py | 358 ++++++++++++++++++------------------ run_sendable_win.py | 429 +++++++++++++++++++++++--------------------- 2 files changed, 394 insertions(+), 393 deletions(-) diff --git a/run_sendable.py b/run_sendable.py index 4511bcb..2b50668 100644 --- a/run_sendable.py +++ b/run_sendable.py @@ -1,32 +1,13 @@ +import argparse import os import subprocess import platform +import re from datetime import datetime -from openpyxl import load_workbook,Workbook +from openpyxl import load_workbook, Workbook +from collections import defaultdict +import pandas as pd -def git_clone(repository_url: str, destination_path: str): - command = ['git', 'clone', repository_url, destination_path] - if platform.system() == "Windows": - subprocess.run(command, check=True, shell=False) - else: - subprocess.run(command, check=True) - - -def git_checkout(commit_hash: str, destination_path: str): - command = ['git', 'checkout', commit_hash] - subprocess.run(command, cwd=destination_path) - - -def git_pull(check_out_dir=os.getcwd()): - cmds = ['git', 'pull', '--rebase'] - with subprocess.Popen(cmds, cwd=check_out_dir) as proc: - proc.wait() - - -def git_clean(clean_dir=os.getcwd()): - cmds = ['git', 'checkout', '--', '.'] - with subprocess.Popen(cmds, cwd=clean_dir) as proc: - proc.wait() def run_cmd(command,cwd=None,command_env=None): log=f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] : {' && '.join(command)}" @@ -36,7 +17,7 @@ def run_cmd(command,cwd=None,command_env=None): process = subprocess.Popen(command, cwd=cwd, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,env={**command_env, **os.environ}) else: process = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,env={**command_env, **os.environ}) - stdout, stderr = process.communicate() # 等待命令执行完毕并获取输出结果 + stdout, stderr = process.communicate() if stdout: print(f"stdout : {stdout}") writing_log(file_name=f"{out_path}/{log_file}",show_date=False,log=stdout) @@ -109,179 +90,176 @@ def writing_log(show_date=False,file_name=None,log="",init=False): def runCmd(cmd): print(cmd) result = os.popen(cmd).read() - print(result+"\n\n") with open(log_file, "a") as file: file.writelines(cmd+"\n") with open(log_file, "a") as file: file.writelines(result+"\n") return result -def is_excel_file_exists(file_path): - try: - workbook = load_workbook(filename=file_path) - return True - except FileNotFoundError: - return False - -def os_path_convert(path): - return os.path.abspath(os.path.expanduser(path)) - -def process_sheet(line, sheet, header, data_keywords): - parts = line.split("--") - sub_parts = parts[1].split(",") - values = [None] * len(data_keywords) - case = None - for sub_part in sub_parts: - for i, (keyword, value_type) in enumerate(data_keywords): - if keyword in sub_part: - value_str = sub_part.split(":")[1].strip() - values[i] = value_type(value_str.replace(" ms", "")) - case = parts[0].split(" ")[1].strip() - ratios = [] - for i in range(0, len(values), 2): - try: - if values[i] and values[i + 1]: - ratio = round(float(values[i + 1]) / float(values[i]), 3) - else: - ratio = 0 - except ZeroDivisionError: - ratio = 0 - ratios.append(ratio) - sheet.append([case] + values + ratios) - -def is_word_boundary(char): - return not char.isalnum() -def whole_word_match(line, target_word): - length = len(target_word) - index = 0 - while index < len(line): - found_index = line.find(target_word, index) - if found_index == -1: - break - start_boundary = found_index == 0 or is_word_boundary(line[found_index - 1]) - end_boundary = found_index + length == len(line) or is_word_boundary(line[found_index + length]) - if start_boundary and end_boundary: - return True - index = found_index + 1 - return False - -def run_array(case_name=None): +def run_array(case_name=None, run_count=1): os.chdir(out_path) - if not is_excel_file_exists(f'{case_name}_data_{date_strftime}.xlsx'): + excel_file = f'{case_name}_data_{date_strftime}.xlsx' + + # 初始化或加载Excel文件 + if not os.path.exists(excel_file): workbook = Workbook() - array_sheet = workbook.active - array_sheet.title = "Array" - array_sheet.append(["Case", "Array", "SendableArray", "SendableArray/Array"]) - workbook.save(f'{case_name}_data_{date_strftime}.xlsx') - workbook = load_workbook(f'{case_name}_data_{date_strftime}.xlsx') - - array_sheet = workbook["Array"] - array_sheet.delete_rows(2, array_sheet.max_row) - # 新增 Int8Array sheet 相关逻辑 - int8array_sheet = workbook.create_sheet("Int8Array") if "Int8Array" not in workbook.sheetnames else workbook["Int8Array"] - int8array_sheet.append(["Case", "Int8Array", "SendableInt8Array", "SendableInt8Array/Int8Array"]) - int8array_sheet.delete_rows(2, int8array_sheet.max_row) - - # 新增 Uint8ClampedArray sheet 相关逻辑 - uint8ClampedArray_sheet = workbook.create_sheet("Uint8ClampedArray") if "Uint8ClampedArray" not in workbook.sheetnames else workbook["Uint8ClampedArray"] - uint8ClampedArray_sheet.append(["Case", "Uint8ClampedArray", "SendableUint8ClampedArray", "SendableUint8ClampedArray/Uint8ClampedArray"]) - uint8ClampedArray_sheet.delete_rows(2, uint8ClampedArray_sheet.max_row) - - # 新增 Uint8Array sheet 相关逻辑 - uint8Array_sheet = workbook.create_sheet("Uint8Array") if "Uint8Array" not in workbook.sheetnames else workbook["Uint8Array"] - uint8Array_sheet.append(["Case", "Uint8Array", "SendableUint8Array", "SendableUint8Array/Uint8Array"]) - uint8Array_sheet.delete_rows(2, uint8Array_sheet.max_row) - - # 新增 Int16Array sheet 相关逻辑 - int16Array_sheet = workbook.create_sheet("Int16Array") if "Int16Array" not in workbook.sheetnames else workbook["Int16Array"] - int16Array_sheet.append(["Case", "Int16Array", "SendableInt16Array", "SendableInt16Array/Int16Array"]) - int16Array_sheet.delete_rows(2, int16Array_sheet.max_row) - - # 新增 Uint16Array sheet 相关逻辑 - uint16Array_sheet = workbook.create_sheet("Uint16Array") if "Uint16Array" not in workbook.sheetnames else workbook["Uint16Array"] - uint16Array_sheet.append(["Case", "Uint16Array", "SendableUint16Array", "SendableUint16Array/Uint16Array"]) - uint16Array_sheet.delete_rows(2, uint16Array_sheet.max_row) - - # 新增 Int32Array sheet 相关逻辑 - int32Array_sheet = workbook.create_sheet("Int32Array") if "Int32Array" not in workbook.sheetnames else workbook["Int32Array"] - int32Array_sheet.append(["Case", "Int32Array", "SendableInt32Array", "SendableInt32Array/Int32Array"]) - int32Array_sheet.delete_rows(2, int32Array_sheet.max_row) - - # 新增 Uint32Array sheet 相关逻辑 - uint32Array_sheet = workbook.create_sheet("Uint32Array") if "Uint32Array" not in workbook.sheetnames else workbook["Uint32Array"] - uint32Array_sheet.append(["Case", "Uint32Array", "SendableUint32Array", "SendableUint32Array/Uint32Array"]) - uint32Array_sheet.delete_rows(2, uint32Array_sheet.max_row) - - # 新增 Float32Array sheet 相关逻辑 - float32Array_sheet = workbook.create_sheet("Float32Array") if "Float32Array" not in workbook.sheetnames else workbook["Float32Array"] - float32Array_sheet.append(["Case", "Float32Array", "SendableFloat32Array", "SendableFloat32Array/Float32Array"]) - float32Array_sheet.delete_rows(2, float32Array_sheet.max_row) + workbook.remove(workbook.active) + else: + workbook = load_workbook(excel_file) - # 新增 Map 相关逻辑 - map_sheet = workbook.create_sheet("Map") if "Map" not in workbook.sheetnames else workbook["Map"] - map_sheet.append(["Case", "Map", "SendableMap", "SendableMap/Map"]) - map_sheet.delete_rows(2, map_sheet.max_row) + sheets_config = { + "Array": { + "pattern": r"Array", + "cols": ["Array", "SendableArray"] + }, + "Int8Array": { + "pattern": r"Int8Array", + "cols": ["Int8Array", "SendableInt8Array"] + }, + "Uint8ClampedArray": { + "pattern": r"Uint8ClampedArray", + "cols": ["Uint8ClampedArray", "SendableUint8ClampedArray"] + }, + "Uint8Array": { + "pattern": r"Uint8Array", + "cols": ["Uint8Array", "SendableUint8Array"] + }, + "Int16Array": { + "pattern": r"Int16Array", + "cols": ["Int16Array", "SendableInt16Array"] + }, + "Uint16Array": { + "pattern": r"Uint16Array", + "cols": ["Uint16Array", "SendableUint16Array"] + }, + "Int32Array": { + "pattern": r"Int32Array", + "cols": ["Int32Array", "SendableInt32Array"] + }, + "Uint32Array": { + "pattern": r"Uint32Array", + "cols": ["Uint32Array", "SendableUint32Array"] + }, + "Float32Array": { + "pattern": r"Float32Array", + "cols": ["Float32Array", "SendableFloat32Array"] + }, + "Map": { + "pattern": r"Map", + "cols": ["Map", "SendableMap"] + }, + "Set": { + "pattern": r"Set", + "cols": ["Set", "SendableSet"] + } + } - # 新增 Set 相关逻辑 - set_sheet = workbook.create_sheet("Set") if "Set" not in workbook.sheetnames else workbook["Set"] - set_sheet.append(["Case", "Set", "SendableSet", "SendableSet/Set"]) - set_sheet.delete_rows(2, set_sheet.max_row) + for run_idx in range(1, run_count+1): + # 执行命令 + cmd = f"cd {standalone_path} && {ark_js_vm} --icu-data-path={icu_data_path} --entry-point={case_name} --aot-file=./{case_name} {abc_path}/{case_name}.abc" + result = runCmd(cmd) + + if "error" in result.lower() or "signal" in result.lower(): + print(f"第{_+1}次运行失败") + continue + + data = defaultdict(dict) + for line in result.split('\n'): + line = line.strip() + if not line.startswith('collections'): + continue + + # 使用正则 + match = re.match( + r'collections (\w+)--normal (\w+): (\d+) ms, sendable (\w+): (\d+) ms', + line + ) + if not match: + continue + + op, normal_type, normal_time, sendable_type, sendable_time = match.groups() + if normal_type != sendable_type: + continue + + # 按类型存储 + data[normal_type][op] = { + "normal": int(normal_time), + "sendable": int(sendable_time) + } + + # 更新每个Sheet + for sheet_name, config in sheets_config.items(): + # 创建或获取Sheet + if sheet_name not in workbook.sheetnames: + sheet = workbook.create_sheet(sheet_name) + sheet.append(["Case"]) # 第一行表头 + else: + sheet = workbook[sheet_name] + + # 更新列头 + current_col = sheet.max_column + if current_col == 1: # 只有Case列 + sheet.cell(1, 2, f"{config['cols'][0]}{run_idx}") + sheet.cell(1, 3, f"{config['cols'][1]}{run_idx}") + else: + sheet.cell(1, current_col+1, f"{config['cols'][0]}{run_idx}") + sheet.cell(1, current_col+2, f"{config['cols'][1]}{run_idx}") + + # 填充数据 + sheet_data = data.get(sheet_name, {}) + for row_idx in range(2, sheet.max_row+1): + case = sheet.cell(row_idx, 1).value + if case in sheet_data: + sheet.cell(row_idx, current_col+1, sheet_data[case]["normal"]) + sheet.cell(row_idx, current_col+2, sheet_data[case]["sendable"]) + + # 处理Case + for case in sheet_data: + if case not in [sheet.cell(r, 1).value for r in range(2, sheet.max_row+1)]: + new_row = [case] + [0]*(2*(run_idx-1)) + [ + sheet_data[case]["normal"], + sheet_data[case]["sendable"] + ] + sheet.append(new_row) + + workbook.save(excel_file) + generate_summary_excel(excel_file, f'{case_name}_summary_{date_strftime}.xlsx') - cmd = f"cd {standalone_path} && {ark_js_vm} --icu-data-path={icu_data_path} --entry-point={case_name} --aot-file=./{case_name} {abc_path}/{case_name}.abc" - result = runCmd(cmd) - if "error" in result or "Signal" in result or "TypeError" in result or "RangeError" in result: - print(f"result error: {result}") - return - for line in result.split("\n"): - line = line.strip() - if "--" in line and "ms" in line: - if whole_word_match(line, "Array") : - array_data_keywords = [("normal Array:", float), ("sendable Array:", float)] - process_sheet(line, array_sheet, ["Case", "Array", "SendableArray", "SendableArray/Array"], array_data_keywords) - elif whole_word_match(line, "Int8Array") : - int8array_data_keywords = [("normal Int8Array:", float), ("sendable Int8Array:", float)] - process_sheet(line, int8array_sheet, ["Case", "Int8Array", "SendableInt8Array", "SendableInt8Array/Int8Array"], int8array_data_keywords) - elif whole_word_match(line, "Uint8ClampedArray") : - uint8ClampedArray_data_keywords = [("normal Uint8ClampedArray:", float), ("sendable Uint8ClampedArray:", float)] - process_sheet(line, uint8ClampedArray_sheet, ["Case", "Uint8ClampedArray", "SendableUint8ClampedArray", "SendableUint8ClampedArray/Uint8ClampedArray"], uint8ClampedArray_data_keywords) - elif whole_word_match(line, "Uint8Array") : - Uint8Array_data_keywords = [("normal Uint8Array:", float), ("sendable Uint8Array:", float)] - process_sheet(line, uint8Array_sheet, ["Case", "Uint8Array", "SendableUint8Array", "SendableUint8Array/Uint8Array"], Uint8Array_data_keywords) - elif whole_word_match(line, "Int16Array") : - Int16Array_data_keywords = [("normal Int16Array:", float), ("sendable Int16Array:", float)] - process_sheet(line, int16Array_sheet, ["Case", "Int16Array", "SendableInt16Array", "SendableInt16Array/Int16Array"], Int16Array_data_keywords) - elif whole_word_match(line, "Uint16Array") : - Uint16Array_data_keywords = [("normal Uint16Array:", float), ("sendable Uint16Array:", float)] - process_sheet(line, uint16Array_sheet, ["Case", "Uint16Array", "SendableUint16Array", "SendableUint16Array/Uint16Array"], Uint16Array_data_keywords) - elif whole_word_match(line, "Int32Array") : - Int32Array_data_keywords = [("normal Int32Array:", float), ("sendable Int32Array:", float)] - process_sheet(line, int32Array_sheet, ["Case", "Int32Array", "SendableInt32Array", "SendableInt32Array/Int32Array"], Int32Array_data_keywords) - elif whole_word_match(line, "Uint32Array") : - Uint32Array_data_keywords = [("normal Uint32Array:", float), ("sendable Uint32Array:", float)] - process_sheet(line, uint32Array_sheet, ["Case", "Uint32Array", "SendableUint32Array", "SendableUint32Array/Uint32Array"], Uint32Array_data_keywords) - elif whole_word_match(line, "Float32Array") : - Float32Array_data_keywords = [("normal Float32Array:", float), ("sendable Float32Array:", float)] - process_sheet(line, float32Array_sheet, ["Case", "Float32Array", "SendableFloat32Array", "SendableFloat32Array/Float32Array"], Float32Array_data_keywords) - elif whole_word_match(line, "Map") : - map_data_keywords = [("normal Map:", float), ("sendable Map:", float)] - process_sheet(line, map_sheet, ["Case", "Map", "SendableMap", "SendableMap/Map"], map_data_keywords) - elif whole_word_match(line, "Set") : - set_data_keywords = [("normal Set:", float), ("sendable Set:", float)] - process_sheet(line, set_sheet, ["Case", "Set", "SendableSet", "SendableSet/Set"], set_data_keywords) - workbook.save(f'{case_name}_data_{date_strftime}.xlsx') +def generate_summary_excel(input_file, output_file): + # 读取原始Excel文件 + xls = pd.ExcelFile(input_file) + writer = pd.ExcelWriter(output_file, engine='openpyxl') + + # 遍历每个Sheet + for sheet_name in xls.sheet_names: + df = xls.parse(sheet_name) + + # 提取Case列 + case_col = df['Case'] + + # 分离普通列和Sendable列 + normal_cols = [col for col in df.columns if 'Sendable' not in col and col != 'Case'] + sendable_cols = [col for col in df.columns if 'Sendable' in col] + + # 按序号分组计算平均值(例如Array1, Array2...) + avg_normal = df[normal_cols].mean(axis=1).round(3) + avg_sendable = df[sendable_cols].mean(axis=1).round(3) + + # 构建结果DataFrame + result_df = pd.DataFrame({ + 'Case': case_col, + sheet_name: avg_normal, + f'Sendable{sheet_name}': avg_sendable + }) + + # 写入新Sheet + result_df.to_excel(writer, sheet_name=sheet_name, index=False) + + writer.close() def main(): - global es2abc - global ts_path - global log_file - global date_strftime - global standalone_path - global ark_js_vm - global out_path - global current_dir - global abc_path - global icu_data_path - + global es2abc, ts_path, log_file, date_strftime, standalone_path, ark_js_vm, out_path, current_dir, abc_path, icu_data_path + current_dir = os.getcwd() standalone_path = f"{current_dir}/standalone" es2abc = f"{standalone_path}/out/x64.release/arkcompiler/ets_frontend/es2abc" @@ -291,16 +269,20 @@ def main(): out_path = f"{current_dir}/arkts/out" log_file = f'arkts_build_{date_strftime}.log' abc_path=f"{current_dir}/arkts/abc/{date_strftime}" - icu_data_path=os_path_convert(f"{standalone_path}/third_party/icu/ohos_icu4j/data") + icu_data_path=os.path.abspath(os.path.join(standalone_path, "third_party/icu/ohos_icu4j/data")) + + parser = argparse.ArgumentParser() + parser.add_argument('--run_count', type=int, default=1, help='执行次数') + args = parser.parse_args() prepare_standalon_code() - case_name_ts = None + for root, dirs, files in os.walk(ts_path): for file in files: if file.endswith('.ts'): file_path = os.path.join(root, file) case_name = os.path.splitext(file)[0] ts2abc(case_name, file_path) - run_array(case_name) + run_array(case_name, run_count=args.run_count) if __name__ == "__main__": main() \ No newline at end of file diff --git a/run_sendable_win.py b/run_sendable_win.py index 986fe73..ff65184 100644 --- a/run_sendable_win.py +++ b/run_sendable_win.py @@ -1,231 +1,250 @@ +import argparse import subprocess import os +import re from datetime import datetime -from openpyxl import load_workbook,Workbook +from openpyxl import load_workbook, Workbook +from collections import defaultdict +import pandas as pd +# 发送可执行文件 def send_files_to_device(): + # 创建目录 + subprocess.run(f"hdc shell mkdir -p {device_dir}", shell=True, check=True) for file in files: if os.path.exists(file): - try: - # 使用 hdc shell 命令将文件推送到设备 - command = f'hdc file send {file} {device_dir}' - subprocess.run(command, shell=True, check=True) - print(f"成功将 {file} 发送到设备的 {device_dir} 目录") - except subprocess.CalledProcessError as e: - print(f"发送 {file} 到设备时出错: {e}") + subprocess.run( + f"hdc file send {file} {device_dir}", + shell=True, + check=True + ) + print(f"[OK] 已发送 {file} 到 {device_dir}") else: - print(f"{file} 文件不存在,请检查。") + print(f"[ERROR] {file} 发送失败,请检查当前目前下是否存在{file}") + exit(1) +# 设置可执行权限 def set_executable_permission(): - for file in files: - try: - # 使用 hdc shell 命令设置文件的可执行权限 - command = f'hdc shell chmod +x {device_dir}/{file}' - subprocess.run(command, shell=True, check=True) - print(f"成功为 {device_dir}/{file} 设置可执行权限") - except subprocess.CalledProcessError as e: - print(f"为 {device_dir}/{file} 设置可执行权限时出错: {e}") - -def execute_command_on_device(): - # 要执行的命令 - command_to_execute = f'hdc shell "cd {device_dir} && ./ark_js_vm collections.abc > {device_log_file}"' try: - # 执行命令并将结果输出到 result.log 文件 - subprocess.run(command_to_execute, shell=True, check=True) - print(f"成功在设备上执行命令并将结果保存到 {device_log_file}") + subprocess.run( + f"hdc shell chmod +x {device_dir}/ark_js_vm", + shell=True, + check=True + ) + print(f"[OK] 已设置 {device_dir}/ark_js_vm 可执行权限") except subprocess.CalledProcessError as e: - print(f"在设备上执行命令时出错: {e}") + print(f"[ERROR] 权限设置失败: {e}") + exit(1) + +# 执行多次测试并保存独立日志 +def execute_command_on_device(run_count): + # 创建日志目录 + subprocess.run(f"hdc shell mkdir -p {device_log_dir}/{current_time}", shell=True, check=True) + for i in range(1, run_count+1): + cmd = f'hdc shell "cd {device_dir} && ./ark_js_vm collections.abc > {device_log_dir}/{current_time}/"collections_data_{i}.log""' + try: + subprocess.run(cmd, shell=True, check=True) + print(f"[OK] 第{i}次测试执行完成") + except subprocess.CalledProcessError as e: + print(f"[ERROR] 第{i}次执行失败: {e}") -def pull_log_file(): +# 拉取所有日志 +def pull_log_files(): try: - # 使用 hdc shell 命令将日志文件从设备拉取到本地 - command = f'hdc file recv {device_log_file} {local_log_file}' - subprocess.run(command, shell=True, check=True) - print(f"成功将 {device_log_file} 回传到本地的 {local_log_file}") + subprocess.run( + f"hdc file recv {device_log_dir}/{current_time} {current_log_dir}", + shell=True, + check=True + ) + print(f"[OK] 已拉取日志到 {current_log_dir}") except subprocess.CalledProcessError as e: - print(f"回传 {device_log_file} 到本地时出错: {e}") - -def is_excel_file_exists(file_path): - try: - workbook = load_workbook(filename=file_path) - return True - except FileNotFoundError: - return False - -def process_sheet(line, sheet, header, data_keywords): - parts = line.split("--") - sub_parts = parts[1].split(",") - values = [None] * len(data_keywords) - case = None - for sub_part in sub_parts: - for i, (keyword, value_type) in enumerate(data_keywords): - if keyword in sub_part: - value_str = sub_part.split(":")[1].strip() - values[i] = value_type(value_str.replace(" ms", "")) - case = parts[0].split(" ")[1].strip() - ratios = [] - for i in range(0, len(values), 2): + print(f"[ERROR] 日志拉取失败: {e}") + +# 解析所有日志到Excel +def parse_logs_to_excel(run_count): + workbook = Workbook() + workbook.remove(workbook.active) + + # 配置所有sheet + sheets_config = { + "Array": { + "pattern": r"Array", + "cols": ["Array", "SendableArray"] + }, + "Int8Array": { + "pattern": r"Int8Array", + "cols": ["Int8Array", "SendableInt8Array"] + }, + "Uint8ClampedArray": { + "pattern": r"Uint8ClampedArray", + "cols": ["Uint8ClampedArray", "SendableUint8ClampedArray"] + }, + "Uint8Array": { + "pattern": r"Uint8Array", + "cols": ["Uint8Array", "SendableUint8Array"] + }, + "Int16Array": { + "pattern": r"Int16Array", + "cols": ["Int16Array", "SendableInt16Array"] + }, + "Uint16Array": { + "pattern": r"Uint16Array", + "cols": ["Uint16Array", "SendableUint16Array"] + }, + "Int32Array": { + "pattern": r"Int32Array", + "cols": ["Int32Array", "SendableInt32Array"] + }, + "Uint32Array": { + "pattern": r"Uint32Array", + "cols": ["Uint32Array", "SendableUint32Array"] + }, + "Float32Array": { + "pattern": r"Float32Array", + "cols": ["Float32Array", "SendableFloat32Array"] + }, + "Map": { + "pattern": r"Map", + "cols": ["Map", "SendableMap"] + }, + "Set": { + "pattern": r"Set", + "cols": ["Set", "SendableSet"] + } + } + + # 遍历所有日志文件 + for log_idx in range(1, run_count+1): + log_file = f"{current_log_dir}/{current_time}/collections_data_{log_idx}.log" try: - if values[i] and values[i + 1]: - ratio = round(float(values[i + 1]) / float(values[i]), 3) - else: - ratio = 0 - except ZeroDivisionError: - ratio = 0 - ratios.append(ratio) - sheet.append([case] + values + ratios) - -def is_word_boundary(char): - return not char.isalnum() -def whole_word_match(line, target_word): - length = len(target_word) - index = 0 - while index < len(line): - found_index = line.find(target_word, index) - if found_index == -1: - break - start_boundary = found_index == 0 or is_word_boundary(line[found_index - 1]) - end_boundary = found_index + length == len(line) or is_word_boundary(line[found_index + length]) - if start_boundary and end_boundary: - return True - index = found_index + 1 - return False - -def parse_log(): - if not is_excel_file_exists(excel_file): - workbook = Workbook() - array_sheet = workbook.active - array_sheet.title = "Array" - array_sheet.append(["Case", "Array", "SendableArray", "SendableArray/Array"]) - workbook.save(excel_file) - workbook = load_workbook(excel_file) - - array_sheet = workbook["Array"] - array_sheet.delete_rows(2, array_sheet.max_row) - # 新增 Int8Array sheet 相关逻辑 - int8array_sheet = workbook.create_sheet("Int8Array") if "Int8Array" not in workbook.sheetnames else workbook["Int8Array"] - int8array_sheet.append(["Case", "Int8Array", "SendableInt8Array", "SendableInt8Array/Int8Array"]) - int8array_sheet.delete_rows(2, int8array_sheet.max_row) - - # 新增 Uint8ClampedArray sheet 相关逻辑 - uint8ClampedArray_sheet = workbook.create_sheet("Uint8ClampedArray") if "Uint8ClampedArray" not in workbook.sheetnames else workbook["Uint8ClampedArray"] - uint8ClampedArray_sheet.append(["Case", "Uint8ClampedArray", "SendableUint8ClampedArray", "SendableUint8ClampedArray/Uint8ClampedArray"]) - uint8ClampedArray_sheet.delete_rows(2, uint8ClampedArray_sheet.max_row) - - # 新增 Uint8Array sheet 相关逻辑 - uint8Array_sheet = workbook.create_sheet("Uint8Array") if "Uint8Array" not in workbook.sheetnames else workbook["Uint8Array"] - uint8Array_sheet.append(["Case", "Uint8Array", "SendableUint8Array", "SendableUint8Array/Uint8Array"]) - uint8Array_sheet.delete_rows(2, uint8Array_sheet.max_row) - - # 新增 Int16Array sheet 相关逻辑 - int16Array_sheet = workbook.create_sheet("Int16Array") if "Int16Array" not in workbook.sheetnames else workbook["Int16Array"] - int16Array_sheet.append(["Case", "Int16Array", "SendableInt16Array", "SendableInt16Array/Int16Array"]) - int16Array_sheet.delete_rows(2, int16Array_sheet.max_row) - - # 新增 Uint16Array sheet 相关逻辑 - uint16Array_sheet = workbook.create_sheet("Uint16Array") if "Uint16Array" not in workbook.sheetnames else workbook["Uint16Array"] - uint16Array_sheet.append(["Case", "Uint16Array", "SendableUint16Array", "SendableUint16Array/Uint16Array"]) - uint16Array_sheet.delete_rows(2, uint16Array_sheet.max_row) - - # 新增 Int32Array sheet 相关逻辑 - int32Array_sheet = workbook.create_sheet("Int32Array") if "Int32Array" not in workbook.sheetnames else workbook["Int32Array"] - int32Array_sheet.append(["Case", "Int32Array", "SendableInt32Array", "SendableInt32Array/Int32Array"]) - int32Array_sheet.delete_rows(2, int32Array_sheet.max_row) - - # 新增 Uint32Array sheet 相关逻辑 - uint32Array_sheet = workbook.create_sheet("Uint32Array") if "Uint32Array" not in workbook.sheetnames else workbook["Uint32Array"] - uint32Array_sheet.append(["Case", "Uint32Array", "SendableUint32Array", "SendableUint32Array/Uint32Array"]) - uint32Array_sheet.delete_rows(2, uint32Array_sheet.max_row) - - # 新增 Float32Array sheet 相关逻辑 - float32Array_sheet = workbook.create_sheet("Float32Array") if "Float32Array" not in workbook.sheetnames else workbook["Float32Array"] - float32Array_sheet.append(["Case", "Float32Array", "SendableFloat32Array", "SendableFloat32Array/Float32Array"]) - float32Array_sheet.delete_rows(2, float32Array_sheet.max_row) - - # 新增 Map 相关逻辑 - map_sheet = workbook.create_sheet("Map") if "Map" not in workbook.sheetnames else workbook["Map"] - map_sheet.append(["Case", "Map", "SendableMap", "SendableMap/Map"]) - map_sheet.delete_rows(2, map_sheet.max_row) - - # 新增 Set 相关逻辑 - set_sheet = workbook.create_sheet("Set") if "Set" not in workbook.sheetnames else workbook["Set"] - set_sheet.append(["Case", "Set", "SendableSet", "SendableSet/Set"]) - set_sheet.delete_rows(2, set_sheet.max_row) - - try: - with open(local_log_file, 'r') as file: - log_content = file.read() - for line in log_content.split("\n"): + with open(log_file) as f: + log_content = f.read() + except FileNotFoundError: + print(f"[WARN] 日志文件 {log_file} 不存在,跳过") + continue + + # 解析日志数据 + data = defaultdict(dict) + for line in log_content.split('\n'): line = line.strip() - if "--" in line and "ms" in line: - if whole_word_match(line, "Array") : - array_data_keywords = [("normal Array:", float), ("sendable Array:", float)] - process_sheet(line, array_sheet, ["Case", "Array", "SendableArray", "SendableArray/Array"], array_data_keywords) - elif whole_word_match(line, "Int8Array") : - int8array_data_keywords = [("normal Int8Array:", float), ("sendable Int8Array:", float)] - process_sheet(line, int8array_sheet, ["Case", "Int8Array", "SendableInt8Array", "SendableInt8Array/Int8Array"], int8array_data_keywords) - elif whole_word_match(line, "Uint8ClampedArray") : - uint8ClampedArray_data_keywords = [("normal Uint8ClampedArray:", float), ("sendable Uint8ClampedArray:", float)] - process_sheet(line, uint8ClampedArray_sheet, ["Case", "Uint8ClampedArray", "SendableUint8ClampedArray", "SendableUint8ClampedArray/Uint8ClampedArray"], uint8ClampedArray_data_keywords) - elif whole_word_match(line, "Uint8Array") : - Uint8Array_data_keywords = [("normal Uint8Array:", float), ("sendable Uint8Array:", float)] - process_sheet(line, uint8Array_sheet, ["Case", "Uint8Array", "SendableUint8Array", "SendableUint8Array/Uint8Array"], Uint8Array_data_keywords) - elif whole_word_match(line, "Int16Array") : - Int16Array_data_keywords = [("normal Int16Array:", float), ("sendable Int16Array:", float)] - process_sheet(line, int16Array_sheet, ["Case", "Int16Array", "SendableInt16Array", "SendableInt16Array/Int16Array"], Int16Array_data_keywords) - elif whole_word_match(line, "Uint16Array") : - Uint16Array_data_keywords = [("normal Uint16Array:", float), ("sendable Uint16Array:", float)] - process_sheet(line, uint16Array_sheet, ["Case", "Uint16Array", "SendableUint16Array", "SendableUint16Array/Uint16Array"], Uint16Array_data_keywords) - elif whole_word_match(line, "Int32Array") : - Int32Array_data_keywords = [("normal Int32Array:", float), ("sendable Int32Array:", float)] - process_sheet(line, int32Array_sheet, ["Case", "Int32Array", "SendableInt32Array", "SendableInt32Array/Int32Array"], Int32Array_data_keywords) - elif whole_word_match(line, "Uint32Array") : - Uint32Array_data_keywords = [("normal Uint32Array:", float), ("sendable Uint32Array:", float)] - process_sheet(line, uint32Array_sheet, ["Case", "Uint32Array", "SendableUint32Array", "SendableUint32Array/Uint32Array"], Uint32Array_data_keywords) - elif whole_word_match(line, "Float32Array") : - Float32Array_data_keywords = [("normal Float32Array:", float), ("sendable Float32Array:", float)] - process_sheet(line, float32Array_sheet, ["Case", "Float32Array", "SendableFloat32Array", "SendableFloat32Array/Float32Array"], Float32Array_data_keywords) - elif whole_word_match(line, "Map") : - map_data_keywords = [("normal Map:", float), ("sendable Map:", float)] - process_sheet(line, map_sheet, ["Case", "Map", "SendableMap", "SendableMap/Map"], map_data_keywords) - elif whole_word_match(line, "Set") : - set_data_keywords = [("normal Set:", float), ("sendable Set:", float)] - process_sheet(line, set_sheet, ["Case", "Set", "SendableSet", "SendableSet/Set"], set_data_keywords) - workbook.save(excel_file) - print(f"成功将结果保存在 {excel_file}") - except FileNotFoundError: - print(f"日志文件 {log_file} 未找到,请检查。") + if not line.startswith('collections'): + continue + + if match := re.match( + r'collections (\w+)--normal (\w+): (\d+) ms, sendable (\w+): (\d+) ms', + line + ): + op, normal_type, normal_time, sendable_type, sendable_time = match.groups() + if normal_type != sendable_type: + continue + + data[normal_type][op] = { + "normal": int(normal_time), + "sendable": int(sendable_time) + } + + # 更新每个sheet + for sheet_name, config in sheets_config.items(): + # 创建或获取Sheet + if sheet_name not in workbook.sheetnames: + sheet = workbook.create_sheet(sheet_name) + sheet.append(["Case"]) + else: + sheet = workbook[sheet_name] + + # 动态添加列头 + current_col = sheet.max_column + col_prefix = config["cols"][0].replace("Sendable", "") + sheet.cell(1, current_col+1, f"{col_prefix}{log_idx}") + sheet.cell(1, current_col+2, f"Sendable{col_prefix}{log_idx}") + + # 填充数据 + sheet_data = data.get(sheet_name, {}) + for row_idx in range(2, sheet.max_row+1): + case = sheet.cell(row_idx, 1).value + if case in sheet_data: + sheet.cell(row_idx, current_col+1, sheet_data[case]["normal"]) + sheet.cell(row_idx, current_col+2, sheet_data[case]["sendable"]) + + # 处理新Case + for case in sheet_data: + existing_cases = [sheet.cell(r, 1).value for r in range(2, sheet.max_row+1)] + if case not in existing_cases: + new_row = [case] + [0]*(2*(log_idx-1)) + [ + sheet_data[case]["normal"], + sheet_data[case]["sendable"] + ] + sheet.append(new_row) + + # 保存原始数据 + raw_excel = f"collections_data_{current_time}.xlsx" + workbook.save(raw_excel) + print(f"[OK] 原始数据已保存至 {raw_excel}") + + # 生成汇总表 + generate_summary_excel(raw_excel, f"collections_summary_{current_time}.xlsx") + +# 生成平均值汇总表 +def generate_summary_excel(input_file, output_file): + xls = pd.ExcelFile(input_file) + writer = pd.ExcelWriter(output_file, engine='openpyxl') + + for sheet_name in xls.sheet_names: + df = xls.parse(sheet_name) + case_col = df['Case'] + + # 分离普通列和Sendable列 + normal_cols = [col for col in df.columns if 'Sendable' not in col and col != 'Case'] + sendable_cols = [col for col in df.columns if 'Sendable' in col] + + # 计算平均值 + avg_normal = df[normal_cols].mean(axis=1).round(3) + avg_sendable = df[sendable_cols].mean(axis=1).round(3) + + # 构建结果 + result_df = pd.DataFrame({ + 'Case': case_col, + sheet_name: avg_normal, + f'Sendable{sheet_name}': avg_sendable + }) + + # 写入sheet + result_df.to_excel(writer, sheet_name=sheet_name, index=False) + + writer.close() + print(f"[OK] 汇总表已生成至 {output_file}") def main(): - global file_name - global log_file - global current_dir - global device_dir - global local_log_file - global device_log_file - global files - global excel_file + parser = argparse.ArgumentParser(description='Windows平台性能测试脚本') + parser.add_argument('--run_count', type=int, default=1, help='测试执行次数') + args = parser.parse_args() + global device_dir, files, current_dir, current_time, device_log_dir, current_log_dir current_dir = os.getcwd() - current_time = datetime.now().strftime('%Y%m%d_%H%M%S') + current_log_dir = f"{current_dir}/log" + device_dir = "/data/collections_test" + device_log_dir = "/data/collections_test/log" files = ["collections.abc", "ark_js_vm"] - device_dir = "/data" - log_file = f"collections_data_{current_time}.log" - device_log_file = f"{device_dir}/{log_file}" - local_log_file = f"{current_dir}/{log_file}" - excel_file = f"collections_data_{current_time}.xlsx" - local_excel_file = f"{current_dir}/{excel_file}" - # 发送文件到设备 - send_files_to_device() - # 设置文件的可执行权限 - set_executable_permission() - # 在设备上执行命令并将结果保存到日志文件 - execute_command_on_device() - # 将日志文件从设备回传到本地 - pull_log_file() - # 解析日志文件并生成excel - parse_log() + current_time = datetime.now().strftime('%Y%m%d_%H%M%S') + + try: + # 清理环境 + subprocess.run(f"hdc shell rm -rf {device_log_dir}", shell=True, check=True) + print("[OK] 已清理设备环境") + + # 执行测试流程 + send_files_to_device() + set_executable_permission() + execute_command_on_device(args.run_count) + pull_log_files() + + # 生成报告 + parse_logs_to_excel(args.run_count) + + except subprocess.CalledProcessError as e: + print(f"[FATAL] 主流程执行失败: {e}") + exit(1) if __name__ == "__main__": main() \ No newline at end of file -- Gitee