You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python解析Chrome Trace文件并筛选指定名称的跟踪记录

Chrome Trace文件筛选实现方案

前置说明

你下载的Chrome Trace跟踪文件通常为JSON结构,你提供的样例属于标准的Chrome线程元数据记录格式,可直接通过Python的json模块解析筛选。

完整实现代码

import json

def filter_trace_records(file_path, target_names):
    # 读取并解析Trace文件
    with open(file_path, 'r', encoding='utf-8') as f:
        trace_data = []
        # 兼容两种常见Trace存储格式
        try:
            # 优先尝试按完整JSON数组解析(大部分Chrome Trace为此格式)
            trace_data = json.load(f)
        except json.JSONDecodeError:
            # 解析失败则按行解析JSON Lines格式,处理行尾逗号
            f.seek(0)
            for line in f:
                cleaned_line = line.strip().rstrip(',')
                if not cleaned_line:
                    continue
                try:
                    trace_data.append(json.loads(cleaned_line))
                except json.JSONDecodeError:
                    # 跳过格式非法的行
                    continue

    # 按指定名称筛选记录,匹配args下的name字段
    filtered_records = [
        item for item in trace_data
        if item.get('args', {}).get('name') in target_names
    ]
    return filtered_records

# 调用示例
if __name__ == "__main__":
    # 替换为你本地的Trace文件路径
    trace_file_path = "./your_trace_file.json"
    # 可自定义要筛选的名称列表,支持同时筛选多个值
    target_list = ["Chrome_ChildIOThread"]
    result = filter_trace_records(trace_file_path, target_list)

    # 输出结果
    print(f"筛选到符合条件的记录共{len(result)}条:")
    for record in result:
        print(record)

使用说明

  • 只需将trace_file_path替换为你下载的Trace文件的本地路径即可运行
  • 如需同时筛选多个值,直接修改target_list即可,例如["Chrome_ChildIOThread", "Compositor"]
  • 返回的result为Python字典组成的列表,可直接用于后续数据处理

内容的提问来源于stack exchange,提问作者kne1234

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 21:45:02