python--安卓端APP:性能测试(cpu使用)

一、获取应用包名,ex:shell

adb shell dumpsys window | findstr mCurrentFocus
mCurrentFocus=Window{b1af8e1 u0 com.xw.samlpe.bubbleseekbar/com.xw.samlpe.bubbleseekbar.MainAbilityShellActivity}

二、获取应用的pid,全部线程的tid值,得到具体线程的状态----得到线程运行时间processCPUTime(即utime,stime,cutime,cstime的和)ide

 1 adb shell ps -ef |findstr com.xw.samlpe.bubbleseekbar   #获取pid线程

 2 adb shell ps -T -p pid值                               #全部线程的tid进程

 3 adb shell cat /proc/pid值/task/tid值/stat                  #具体线程的状态  it

备注:proc/pid/task/tid/stat文件介绍以下:io

1 C:\Users\dWX1068106>adb shell cat /proc/2275/task/2471/stat
2 2471 (Thread-3) S 626 972 0 0 -1 1077952576 11015 0 0 0 364 207 0 0 10 -10 39 0 534300 6705520640 40954 18446744073709551615 1 1 0 0 0 0 4612 4097 1073775864 0 0 0 -1 3 0 0 0 0 0 0 0 0 0 0 0 0 0
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

每一个参数意思为:
参数                                                       解释
pid=2741                                             进程(包括轻量级进程,即线程)号
com=(Thread-3)          应用程序或命令的名class

utime=364                                       该任务在用户态运行的时间,单位为jiffies(通常地等于10ms),下标为13
stime=207                                       该任务在核心态运行的时间,单位为jiffies,下标为14
cutime=0                                         累计的该任务的全部的waited-for进程曾经在用户态运行的时间,单位为jiffies
cstime=0                                         累计的该任务的全部的waited-for进程曾经在核心态运行的时间,单位为jiffies程序

三、得到/proc/stat中 cpu运行时间  totalCpuTime即(CPU指标:user,nice, system, idle, iowait, irq, softirq的和)im

四、totalCpuTime,processCPUTime均取0.3s的差值,来判断cpu_rate(咱们须要的cpu使用率),cpu

cpu = 100 * (processCpuTime差值) / (totalCpuTime差值)
       if cpu < 0:
           cpu = 0
       elif cpu > 100:
           cpu = 100
       return cpu
相关文章
相关标签/搜索