移动性能测试 测试 monkey 的同时,怎么收集 CPU 和内存的信息呢,使用多线程,发现不行,大家可以帮忙看一下吗

cczhang · 2019年08月16日 · 最后由 陌路微凉 回复于 2020年05月26日 · 1799 次阅读

import datetime
import subprocess
import time
import os
import re
import threading

si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW

dev_list =[]

def get_devices():
devices = []
result = subprocess.Popen("adb devices", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()
# print(result[1].decode())
#print(len(result))
if len(result)-2 == 1:
for line in result[1:]:
devices.append(line.strip().decode())
#print(devices[0].split()[0])
return devices[0].split()[0]
else:
print('No device')
return 'No device found'

def get_monkey():
os.popen("adb shell monkey -p com.kugou.android.ringtone --throttle 500 -s 1000 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --ignore-native-crashes --monitor-native-crashes -v -v -v 1000 >D:\monkey_log.txt").readlines() # 逐行读取

def getBerforeTotalPss():
lines = os.popen("adb shell dumpsys meminfo com.kugou.android.ringtone ").readlines() # 逐行读取
total = "TOTAL"
for line in lines:
if re.findall(total, line): # 找到 TOTAL 这一行
lis = line.split(" ") # 将这一行,按空格分割成一个 list
while '' in lis: # 将 list 中的空元素删除
lis.remove('')
#return lis[1] # 返回总共内存使用
print('前端应用的内存是:',int(lis[1])/1024,'M')
break

def getPss():
print('z 来到这里吗')
t=time.time()
i=5
t1 = time.time()

while t1 - t <= 5:
t1 = time.time()
print('看看这里')
getBerforeTotalPss()
print('----------------------------------------------------------------------------------------------------')
time.sleep(1)

if name == "main":
print('测试开始啦。。。。{}'.format(time.ctime()))
threadl = []
t1 = threading.Thread(target=get_monkey())
t2 = threading.Thread(target=getBerforeTotalPss())
t1.start()
t2.start()
t1.join()
t2.join()
print('测试结束啦。。。。{}'.format(time.ctime()))

共收到 5 条回复 时间 点赞

你这种要用多进程执行下那 2 个函数吧。你目前做出来效果是,monkey 跑完了,再去记录内存,不是并行

兄 dei,咱敢不敢用 markdown 啊,看的很累啊。
首先你的多线程装载有问题:
t1 = threading.Thread(target=get_monkey())
把 get_monkey 后面的括号去掉,应该是 t1 = threading.Thread(target=get_monkey)
线程的参数是函数,而不是函数的返回值,要不当程序执行到你的那句话时,他要把 get_monkey 函数执行结束,得到 None 返回,把 None 作为参数传给 threading.Thread 类。

黑山老妖 回复

也没有用,最终只能同时跑两个工具,才能一边进行 Monkey 的同时采集性能数据

请问下,同时跑两个工具是什么意思,最近也在做一个 monkey 工具,打算跑 monkey 的同时记录 cpu 或者内存

我个人在做的一个工具,可以参考一下,里面有 monkey 的执行和 cpu 内存的采集。不建议直接拿来用,而是分析一下实现方式https://github.com/sailenxu/PerformaceMonitor

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册