51Testing软件测试论坛
标题:
移动端自动化性能测试之性能分析
[打印本页]
作者:
Mario洁
时间:
2018-6-12 15:54
标题:
移动端自动化性能测试之性能分析
# -*- coding: utf-8 -*-
import os
import shutil
import platform
import re
import subprocess
import time
stop=True
runNum=None
# 判断系统类型,windows使用findstr,linux使用grep
system=platform.system()
ifsystemis"Windows":
find_util="findstr"
else:
find_util="grep"
# 判断是否设置环境变量ANDROID_HOME
if"ANDROID_HOME"inos.environ:
ifsystem=="Windows":
command=os.path.join(os.environ["ANDROID_HOME"],"platform-tools","adb.exe")
else:
command=os.path.join(os.environ["ANDROID_HOME"],"platform-tools","adb")
else:
raiseEnvironmentError(
"Adb not found in $ANDROID_HOME path: %s."%os.environ["ANDROID_HOME"])
# adb命令
defadb(args):
cmd="%s %s"%(command,str(args))
returnsubprocess.Popen(cmd,shell=False,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# adb shell命令
defshell(args):
cmd="%s shell %s"%(command,str(args))
returnsubprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# 获取设备状态
defget_state():
returnos.popen("adb get-state").read().strip()
# 获取对应包名的pid
defget_app_pid(pkg_name):
ifsystemis"Windows":
string=shell("ps | findstr %s$"%pkg_name).stdout.read()
else:
string=shell("ps | grep -w %s"%pkg_name).stdout.read()
ifstring=='':
return"the process doesn't exist."
pattern=re.compile(r"\d+")
result=string.split()
result.remove(result[0])
returnpattern.findall(" ".join(result))[0]
# 杀掉对应包名的进程
defkill_process(pkg_name):
pid=get_app_pid(pkg_name)
result=shell("su -c 'kill %s'"%str(pid)).stdout.read().split(": ")[-1]
ifresult!="":
raiseexception.SriptException("Operation not permitted or No such process or Permission denied")
# 杀掉应用程序
defkill_application(pkg_name):
shell("am force-stop %s"%pkg_name)
# 获取设备上当前应用的包名与activity
defget_focused_package_and_activity():
pattern=re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")
out=shell("dumpsys window w | %s \/ | %s name="%(find_util, find_util)).stdout.read()
returnpattern.findall(out)[0]
# 获取当前应用的包名
defget_current_package_name():
returnget_focused_package_and_activity().split("/")[0]
# 获取当前设备的activity
defget_current_activity():
returnget_focused_package_and_activity().split("/")[-1]
# 时间戳
deftimestamp():
returntime.strftime('%Y-%m-%d_%H.%M.%S', time.localtime(time.time()))
# 检查设备状态
ifget_state()!="device":
adb("kill-server").wait()
adb("start-server").wait()
ifget_state()!="device":
raiseexception.SriptException("Device not run")
# 获取设备名称
defget_device_name():
brandName=shell("cat /system/build.prop | findstr ro.product.brand").stdout.readline().split("=")[1].strip()
deviceName=shell("cat /system/build.prop | findstr ro.product.model").stdout.readline().split("=")[1].strip()
result="{brand}_{device}".format(brand=brandName,device=deviceName)
returnresult
# 获取设备序列号
defget_serialno():
returnadb("get-serialno").stdout.readline()
# 获取APP的uid
defget_app_uid(pkg_name):
pid=get_app_pid(pkg_name)
uid=shell("cat /proc/%s/status|findstr Uid"%pid).stdout.readline().split()[1]
returnuid
# 获取CPU型号
defget_cpu_model():
returnshell("cat /proc/cpuinfo|findstr Hardware").stdout.readline().strip().split(":")[1]
# 获取设备总内存
defget_total_memory():
result=shell('cat /proc/meminfo|findstr "MemTotal"').stdout.readline().split(":")[1].strip()
MemTotal="{val}MB".format(val=int(result.split()[0])/1024)
returnMemTotal
# 获取设备分辨率
defget_DisplayDeviceInfo():
result=shell("dumpsys display | findstr DisplayDeviceInfo").stdout.readline().split(",")[1].strip()
returnresult
# 获取系统版本号
defget_system_version():
returnshell("cat /system/build.prop | findstr ro.build.version.release").stdout.readline().split("=")[1]
# 获取SDK版本号
defget_sdk_version():
returnshell("cat /system/build.prop | findstr ro.build.version.sdk").stdout.readline().split("=")[1]
# 获取设备CPU最大频率
defget_cpu_max_frequency():
max_frequency=shell("su -c 'cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq'").stdout.readline().strip()
if"not found"inmax_frequency:
max_frequency="Permission denied"
returnmax_frequency
else:
return"{val}MHz".format(val=int(max_frequency)/1000)
# 获取设备CPU最小频率
defget_cpu_min_frequency():
min_frequency=shell("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq").stdout.readline().strip()
return"{val}MHz".format(val=int(min_frequency)/1000)
# 截图保存至sd卡adbscreenshot目录并同步电脑
defscreenshot():
if"directory"inshell("ls /sdcard/adbScreenShot").stdout.readline():
shell("mkdir /sdcard/adbScreenShot")
shell("/system/bin/screencap -p /sdcard/adbScreenShot/%s.png"%timestamp()).wait()
ifos.path.exists("存储地址"):
shutil.rmtree("存储地址")
adb("pull /sdcard/adbscreenshot 存储地址")
if__name__=="__main__":
screenshot()
复制代码
作者:
qqq911
时间:
2018-6-26 11:23
感谢分享·~
作者:
Miss_love
时间:
2020-12-29 10:57
感谢分享
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2