Android Graphics 显示系统 - 通过dumpsys SurfaceFlinger --latency计算FPS

本文给大家推荐一个网络上找到的shell脚本工具,利用dumpsys SurfaceFlinger --latency的数据计算FPS,目前我测试在Android 14上依然可以使用,可以满足某些场景下的测试需求,或者基于此做二次开发。

一、原文地址

https://testerhome.com/topics/4775

还有一个较新的版本,不过我并未测试,大家可以自行查阅

https://testerhome.com/topics/20187

自行复制到浏览器中查看吧!

二、使用方法

1. 设备端需存在/data/local/tmp/busybox (busybox可到官网对应cpu架构下载) ,https://busybox.net/downloads/binaries/

命令:

adb push busybox /data/local/tmpadb shell chmod 755 /data/local/tmp/busybox

2. 把shell脚本放到/data/local/tmp/fps_monitor.sh

3. dumpsys SurfaceFlinger找到要监测的layer name

4. 旧版本可以类似执行:​​​​​​​


                
### 如何通过 `adb shell dumpsys SurfaceFlinger --latency` 计算设备帧率 要计算 Android 设备的帧率(FPS),可以通过解析 `adb shell dumpsys SurfaceFlinger --latency` 输出的数据来实现。以下是具体方法: #### 数据采集 运行以下命令可以获取指定层(Layer)的时间戳数据: ```bash adb shell dumpsys SurfaceFlinger --latency "SurfaceView" ``` 对于 API 等级小于等于 23 的设备,可以直接使用 `"SurfaceView"`;而对于 API 等级大于 23 的设备,则需提供完整的 Layer 名称,例如 `"SurfaceView com.example.app/com.example.MainActivity"`[^1]。 #### 数据结构分析 输出的结果是一个三列表格,每行代表一次绘制操作的时间戳信息: - **第一列**:表示当前时间戳(单位为纳秒)。 - **第二列**:表示上一帧完成绘制的时间戳。 - **第三列**:表示 GPU 渲染完成的时间戳。 这些时间戳用于计算每一帧的渲染耗时以及最终的 FPS 值。 #### 计算逻辑 为了计算平均帧率(FPS),需要统计一段时间内的总帧数和总时间差。假设我们有如下伪代码实现这一过程: ```python import numpy as np def calculate_fps(latency_data, time_window=5): # 默认取最近5秒的数据 timestamps = [] for line in latency_data.splitlines(): parts = line.strip().split() if len(parts) != 3: continue current_time = int(parts[0]) / 1e9 # 转换为秒 previous_frame_time = int(parts[1]) / 1e9 # 转换为秒 frame_duration = current_time - previous_frame_time if frame_duration > 0: # 排除异常值 timestamps.append(current_time) start_time = max(timestamps[-1] - time_window, min(timestamps)) filtered_timestamps = [t for t in timestamps if t >= start_time] total_frames = len(filtered_timestamps) elapsed_seconds = (max(filtered_timestamps) - min(filtered_timestamps)) if filtered_timestamps else 0 fps = total_frames / elapsed_seconds if elapsed_seconds > 0 else 0 return fps # 示例调用 data = """ 1684732800000000000 1684732799000000000 1684732798000000000 ... """ print(calculate_fps(data)) ``` 上述脚本会读取 `--latency` 返回的日志并提取有效的时间戳,进而计算出目标时间段内的平均帧率。 #### 注意事项 - 如果硬件不支持 `--latency-clear` 功能,则可能无法完全清除历史记录,这可能导致重复计数问题[^2]。 - 对于不同版本的 Android 系统,Layer Name 的命名方式有所不同,请参照实际测试环境调整输入参数[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值