#!/usr/bin/env python3
# -*- coding: gbk -*- # 声明GBK编码
import subprocess
import re
import time
import csv
from datetime import datetime
class DeviceMonitor:
def __init__(self, interval=5, output_file="device_performance.csv"):
self.interval = interval # 监控间隔(秒)
self.output_file = output_file
self.header_written = False
def check_adb_connection(self):
"""验证ADB设备连接状态"""
try:
result = subprocess.run(
"adb devices",
shell=True,
capture_output=True,
text=True,
encoding='gbk'
)
if "device" not in result.stdout:
print("设备未连接或未授权")
return False
return True
except Exception as e:
print(f"ADB连接检查失败: {str(e)}")
return False
def get_foreground_package(self):
"""获取当前前台应用包名"""
try:
# 方法1: 使用dumpsys window命令
result = subprocess.run(
"adb shell dumpsys window windows",
shell=True,
capture_output=True,
text=True,
encoding='gbk',
errors='ignore'
)
output = result.stdout
# 尝试多种匹配模式
patterns = [
r'mCurrentFocus=Window\{.*?\s([a-zA-Z0-9._]+)/',
r'mFocusedApp=AppWindowToken\{.*?\s([a-zA-Z0-9._]+)/',
r'ResumedActivity: ActivityRecord\{.*?\s([a-zA-Z0-9._]+)/'
]
for pattern in patterns:
match = re.search(pattern, output)
if match:
return match.group(1)
# 方法2: 使用dumpsys activity命令(备用)
result = subprocess.run(
"adb shell dumpsys activity activities",
shell=True,
capture_output=True,
text=True,
encoding='gbk',
errors='ignore'
)
match = re.search(r'ResumedActivity: ActivityRecord\{.*?\s([a-zA-Z0-9._]+)/', result.stdout)
return match.group(1) if match else "unknown"
except Exception as e:
print(f"获取前台应用失败: {str(e)}")
return "unknown"
def get_cpu_usage(self):
"""获取CPU使用率(%)"""
try:
# 获取总CPU时间和空闲时间
result = subprocess.run(
"adb shell top -n 1 -d 0.5 | grep {match.group(1)}",
shell=True,
capture_output=True,
text=True
)
cpu_stats = result.stdout.split()[1:]
total_time = sum(map(int, cpu_stats))
idle_time = int(cpu_stats[3])
# 计算使用率
usage = 100 * (1 - idle_time / total_time)
return round(usage, 1)
except:
return 0.0
def get_memory_usage(self):
"""获取内存使用情况(MB)"""
try:
result = subprocess.run(
"adb shell cat /proc/meminfo",
shell=True,
capture_output=True,
text=True
)
meminfo = result.stdout.splitlines()
total = int(meminfo[0].split()[1]) // 1024
free = int(meminfo[1].split()[1]) // 1024
used = total - free
return total, used, free
except:
return 0, 0, 0
def get_battery_info(self):
"""获取电池信息和温度(℃)"""
try:
result = subprocess.run(
"adb shell dumpsys battery",
shell=True,
capture_output=True,
text=True
)
output = result.stdout
level = re.search(r'level: (\d+)', output)
status = re.search(r'status: (\d+)', output)
temp = re.search(r'temperature: (\d+)', output)
return (
int(level.group(1)) if level else 0,
int(status.group(1)) if status else 0,
int(temp.group(1)) / 10.0 if temp else 0.0 # 转换为摄氏度
)
except:
return 0, 0, 0.0
def get_cpu_temperature(self):
"""获取CPU温度(℃) - 需要root权限"""
try:
result = subprocess.run(
"adb shell cat /sys/class/thermal/thermal_zone0/temp",
shell=True,
capture_output=True,
text=True
)
return float(result.stdout.strip()) / 1000.0
except:
return 0.0
def monitor_performance(self):
"""主监控循环"""
if not self.check_adb_connection():
print("设备连接失败,请检查ADB连接")
return
print(f"开始设备监控,间隔: {self.interval}秒")
print("按Ctrl+C停止监控...")
try:
while True:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
package = self.get_foreground_package()
cpu_usage = self.get_cpu_usage()
mem_total, mem_used, mem_free = self.get_memory_usage()
batt_level, batt_status, batt_temp = self.get_battery_info()
cpu_temp = self.get_cpu_temperature()
# 输出到控制台
print(f"[{timestamp}] 应用: {package[:20]:<20} | "
f"CPU: {cpu_usage}% | "
f"内存: {mem_used}/{mem_total}MB | "
f"电池: {batt_level}% | "
f"温度: CPU:{cpu_temp:.1f}℃ 电池:{batt_temp:.1f}℃")
# 保存到CSV文件
self.save_to_csv(timestamp, package, cpu_usage, mem_total,
mem_used, mem_free, batt_level, batt_status,
batt_temp, cpu_temp)
time.sleep(self.interval)
except KeyboardInterrupt:
print("\n监控已停止")
def save_to_csv(self, timestamp, package, cpu_usage, mem_total,
mem_used, mem_free, batt_level, batt_status,
batt_temp, cpu_temp):
"""保存数据到CSV文件"""
data = [
timestamp, package, cpu_usage, mem_total, mem_used, mem_free,
batt_level, batt_status, batt_temp, cpu_temp
]
with open(self.output_file, 'a', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
if not self.header_written:
writer.writerow([
'时间戳', '前台应用', 'CPU使用率(%)', '内存总量(MB)',
'已用内存(MB)', '空闲内存(MB)', '电池电量(%)', '电池状态',
'电池温度(℃)', 'CPU温度(℃)'
])
self.header_written = True
writer.writerow(data)
if __name__ == "__main__":
# 创建监控实例并启动(每10秒监控一次)
monitor = DeviceMonitor(interval=10)
monitor.monitor_performance()
将此脚本加个应用刷新率和帧率