以下是python源码实现,可直接使用~
import uiautomator2 as u2
import time
import threading
import os
import matplotlib.pyplot as plt
import numpy as np
import random
#serial="***"
#d=u2.connect(serial)
sn="*"
initstr="python -m uiautomator2 init >"
snfilename="./initdevice.txt"
activityfilename="./activityfile.txt"
packagefilename="./packagefile.txt"
sysmonkeylog="./sysmonkey.txt"
activityfile=open(activityfilename,mode="w+")
packagefile=open(packagefilename,mode="w+")
activitylist=[]
packagenamelist=[]
totalRAM=[]
freeRAM=[]
usedRAM=[]
lostRAM=[]
sysUserCPU=[]
sysKernelCPU=[]
sysTotalCPU=[]
packagefilterstr="******" #用于过滤公司内部的package
drawtime=3600*5 #every 5H draw one time picture for RAM and CPU Analysis
appswitchtimes=1000 #run 1000times circle switch to get packagename from the device
monkeyseed=int(random.random()*10000+1) #each run for one seed
monkeytimes=1200 #each time 0.3s monkey time long for 0.3*monkeytimes
silencetime=0.3*monkeytimes #keep silence as long as monkey time
getinfotime=5 #every 5s get one time info data
def initconnect():
global sn
global d
cmdstr=initstr+snfilename
print(cmdstr)
os.system(cmdstr)
sn=open(snfilename,mode="r+").readline().split("=")[1].replace(")","").strip()
print(len(sn))
d=u2.connect(sn)
def runAppswitch():
os.system("adb shell monkey -s "+str(monkeyseed)+" --pct-touch 10 --pct-appswitch 90 --throttle 300 -v "+str(monkeytimes))
def getPackageinfo():
count=0
global alist
global plist
while count<monkeytimes:
cmdstr="dumpsys window | grep mCurrentFocus | grep "+packagefilterstr
out=d.shell(cmdstr)
if(str(out).find("output=''")==-1):
if(str(out).find("mCurrentFocus=null")==-1):
if(str(out).split("u0")[1].find("}")!=-1):
activity