【app性能测试】Python脚本监控app指标

1. adb shell top
top命令提供了实时的对系统处理器的状态监视.它将显示系统中CPU最“敏感”的任务列表.该命令可以按CPU使用.内存使用和执行时间对任务进行排序.
top 用法

>adb shell top -h
Usage: top [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [-t ] [ -h ]
    -m num  Maximum number of processes to display. 最多显示多少个进程
    -n num  Updates to show before exiting.  刷新次数 
    -d num  Seconds to wait between updates. 刷新间隔时间(默认5秒)
    -s col  Column to sort by (cpu,vss,rss,thr). 按哪列排序 
    -t      Show threads instead of processes. 显示线程信息而不是进程
    -h      Display this help screen.  显示帮助文档

adb shell top

User 13%, System 5%, IOW 0%, IRQ 0%
User 85 + Nice 0 + Sys 37 + Idle 509 + IOW 0 + IRQ 0 + SIRQ 0 = 631

PID PR CPU% S #THR VSS RSS PCY UID Name
0 13% S 56 423416K 88160K fg u0_a92 com.tmall.wireless
1 2% R 1 1232K 536K root top
0 1% S 46 341712K 40872K fg u0_a90 com.wandoujia.phoenix2.usbproxy
1 1% S 31 319976K 33284K fg u0_a74 com.android.Chinpower
0 1% S 32 67320K 20552K fg system /system/bin/surfaceflinger
0 1% S 112 445876K 80304K fg system system_server

0 0% S 1 0K 0K root watchdog/0
1 0% S 1 0K 0K root khelper
1 0% S 1 0K 0K root suspend_sys_syn
1 0% S 1 0K 0K root suspend

第一组数据的含义:

User 处于用户态的运行时间,不包含优先值为负进程
Nice 优先值为负的进程所占用的CPU时间
Sys 处于核心态的运行时间
Idle 除IO等待时间以外的其它等待时间
IOW IO等待时间
IRQ 硬中断时间
SIRQ 软中断时间

第二组数据的含义:

PID 进程id
PR 优先级
CPU% 当前瞬时CPU占用率
S 进程状态:D=不可中断的睡眠状态, R=运行, S=睡眠, T=跟踪/停止, Z=僵尸进程
#THR 程序当前所用的线程数
VSS Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PCY 调度策略优先级,SP_BACKGROUND/SP_FOREGROUND
UID 进程所有者的用户id
Name 进程的名称

监控CPU的命令使用实战
C:\Users\THINK>adb shell top -m 5 -n 2 -d 3 -s cpu | findstr phone >d:\app\phone.txt
打印 top命令,并查找包含 phone 的命令行,输出至 d:\phone.txt
【app性能测试】Python脚本监控app指标_第1张图片
2. adb logcat
常用命令

1.adb logcat -v time
2.adb logcat -v time > D:\log.txt
3.adb logcat -c     #查看日志前,最好先清除以前的日志
查看app上正在运行的进程
C:\Users\THINK>adb shell "ps | grep qqmusic"
u0_a41    4817  1625  1536108 177820 ffffffff b765f07b S com.tencent.qqmusic
u0_a41    4876  1625  1444484 126760 ffffffff b765f07b S com.tencent.qqmusic:QQPlayerService

3. 查看第三方包:adb shell pm list packages -3
查看启动activity.

C:\Users\THINK>adb shell monkey -p com.tencent.qqmusic -vvv 1

在日志中查找:cmp=com.tencent.qqmusic/.activity.AppStarterActivity
【app性能测试】Python脚本监控app指标_第2张图片
4. 启动app:adb shell am start -W -n package/.activity

C:\Users\THINK>adb shell am start -W -n com.tencent.qqmusic/.activity.AppStarterActivity
Starting: Intent { cmp=com.tencent.qqmusic/.activity.AppStarterActivity }
Status: ok
Activity: com.tencent.qqmusic/.activity.AppStarterActivity
ThisTime: 1178
TotalTime: 1178
Complete

5.关闭app:adb shell am force-stop package

C:\Users\THINK>adb shell am force-stop com.tencent.qqmusic

C:\Users\THINK>

6.实战
1):编写python 脚本统计vwill app的启动时间。
代码如下:

import csv
import os
import time

class AppStartTime(object):
    def __init__(self, count):
        self.count = count
        self.alldata = [("timestamp", "elapsetime")]
    # 单次启动过程
    def monitoring(self):
        #result1 = os.popen("adb shell am start -W -n com.tencent.qqmusic/.activity.AppStarterActivity")
        result = os.popen("adb shell am start -W -n com.xyy.vwill/.MainActivity")
        qqtime=-1
        for line in result.readlines():
            #注意:有可能出现启动超时,引发异常
            try:
                if 'TotalTime' in line:
                    qqtime=line.split(":")[1].strip()
            except:
                print('Status: timeout')
            print(line)
        currenttime = self.getCurrentTime()
        print("current time is:"+currenttime)
        self.alldata.append([currenttime, qqtime])
        time.sleep(1)
        #关闭app
        os.popen("adb shell am force-stop com.xyy.vwill")
        #后台运行
        #os.popen("adb shell input keyevent 3")
        print(self.alldata)
    # 多次执行监控过程
    def run(self):
        while self.count > 0:
            self.monitoring()
            self.count = self.count - 1
            time.sleep(5)
    # 获取当前的时间戳
    def getCurrentTime(self):
        currentTime = time.strftime("%H:%M:%S", time.localtime())
        return currentTime
    # 数据的存储
    def SaveDataToCSV(self):
        csvfile = open('appstarttime.csv', 'w',encoding='utf8',newline='')
        writer = csv.writer(csvfile)
        writer.writerows(self.alldata)
        csvfile.close()

if __name__ == "__main__":
    appstart=AppStartTime(10)
    appstart.run()
    appstart.SaveDataToCSV()

运行结果及分析:
【app性能测试】Python脚本监控app指标_第3张图片
2)编写python脚本收集QQ音乐 app cup使用情况。
代码如下:

import csv
import os
import time

# 监控CPU资源信息
class MonitoringCPUResources(object):
    def __init__(self, count):
        self.count = count
        self.alldata = [("timestamp", "cpustatus")]
    # 单次执行监控过程
    def monitoring(self):
        result = os.popen("adb shell dumpsys cpuinfo | findstr com.tencent.qqmusic")
        cpuvalue = result.readline().split("%")[0].strip()
        currenttime = self.getCurrentTime()
        print("current time is:"+currenttime)
        print("cpu used is:" + cpuvalue)
        self.alldata.append([currenttime, cpuvalue])
    # 多次执行监控过程
    def run(self):
        while self.count > 0:
            self.monitoring()
            self.count = self.count - 1
            time.sleep(3)
    # 获取当前的时间戳
    def getCurrentTime(self):
        currentTime = time.strftime("%H:%M:%S", time.localtime())
        return currentTime
    # 数据的存储
    def SaveDataToCSV(self):
        csvfile = open('cpustatus.csv', 'w',encoding='utf8',newline='')
        writer = csv.writer(csvfile)
        writer.writerows(self.alldata)
        csvfile.close()

def count_cpu():
    monitoringCPUResources = MonitoringCPUResources(30)
    monitoringCPUResources.run()
    monitoringCPUResources.SaveDataToCSV()
if __name__ == "__main__":
    count_cpu()

运行结果:
【app性能测试】Python脚本监控app指标_第4张图片

你可能感兴趣的:(软件测试,#,性能测试)