51Testing软件测试论坛

标题: 移动端自动化性能测试之性能分析 [打印本页]

作者: Mario洁    时间: 2018-6-12 15:54
标题: 移动端自动化性能测试之性能分析
  1. # -*- coding: utf-8 -*-

  2. import os

  3. import shutil

  4. import platform

  5. import re

  6. import subprocess

  7. import time

  8. stop=True

  9. runNum=None

  10. # 判断系统类型,windows使用findstr,linux使用grep

  11. system=platform.system()

  12. ifsystemis"Windows":

  13. find_util="findstr"

  14. else:

  15. find_util="grep"

  16. # 判断是否设置环境变量ANDROID_HOME

  17. if"ANDROID_HOME"inos.environ:

  18. ifsystem=="Windows":

  19. command=os.path.join(os.environ["ANDROID_HOME"],"platform-tools","adb.exe")

  20. else:

  21. command=os.path.join(os.environ["ANDROID_HOME"],"platform-tools","adb")

  22. else:

  23. raiseEnvironmentError(

  24. "Adb not found in $ANDROID_HOME path: %s."%os.environ["ANDROID_HOME"])

  25. # adb命令

  26. defadb(args):

  27. cmd="%s %s"%(command,str(args))

  28. returnsubprocess.Popen(cmd,shell=False,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

  29. # adb shell命令

  30. defshell(args):

  31. cmd="%s shell %s"%(command,str(args))

  32. returnsubprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

  33. # 获取设备状态

  34. defget_state():

  35. returnos.popen("adb get-state").read().strip()

  36. # 获取对应包名的pid

  37. defget_app_pid(pkg_name):

  38. ifsystemis"Windows":

  39. string=shell("ps | findstr %s$"%pkg_name).stdout.read()

  40. else:

  41. string=shell("ps | grep -w %s"%pkg_name).stdout.read()

  42. ifstring=='':

  43. return"the process doesn't exist."

  44. pattern=re.compile(r"\d+")

  45. result=string.split()

  46. result.remove(result[0])

  47. returnpattern.findall(" ".join(result))[0]

  48. # 杀掉对应包名的进程

  49. defkill_process(pkg_name):

  50. pid=get_app_pid(pkg_name)

  51. result=shell("su -c 'kill %s'"%str(pid)).stdout.read().split(": ")[-1]

  52. ifresult!="":

  53. raiseexception.SriptException("Operation not permitted or No such process or Permission denied")

  54. # 杀掉应用程序

  55. defkill_application(pkg_name):

  56. shell("am force-stop %s"%pkg_name)

  57. # 获取设备上当前应用的包名与activity

  58. defget_focused_package_and_activity():

  59. pattern=re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")

  60. out=shell("dumpsys window w | %s \/ | %s name="%(find_util, find_util)).stdout.read()

  61. returnpattern.findall(out)[0]

  62. # 获取当前应用的包名

  63. defget_current_package_name():

  64. returnget_focused_package_and_activity().split("/")[0]

  65. # 获取当前设备的activity

  66. defget_current_activity():

  67. returnget_focused_package_and_activity().split("/")[-1]

  68. # 时间戳

  69. deftimestamp():

  70. returntime.strftime('%Y-%m-%d_%H.%M.%S', time.localtime(time.time()))

  71. # 检查设备状态

  72. ifget_state()!="device":

  73. adb("kill-server").wait()

  74. adb("start-server").wait()

  75. ifget_state()!="device":

  76. raiseexception.SriptException("Device not run")

  77. # 获取设备名称

  78. defget_device_name():

  79. brandName=shell("cat /system/build.prop | findstr ro.product.brand").stdout.readline().split("=")[1].strip()

  80. deviceName=shell("cat /system/build.prop | findstr ro.product.model").stdout.readline().split("=")[1].strip()

  81. result="{brand}_{device}".format(brand=brandName,device=deviceName)

  82. returnresult

  83. # 获取设备序列号

  84. defget_serialno():

  85. returnadb("get-serialno").stdout.readline()

  86. # 获取APP的uid

  87. defget_app_uid(pkg_name):

  88. pid=get_app_pid(pkg_name)

  89. uid=shell("cat /proc/%s/status|findstr Uid"%pid).stdout.readline().split()[1]

  90. returnuid

  91. # 获取CPU型号

  92. defget_cpu_model():

  93. returnshell("cat /proc/cpuinfo|findstr Hardware").stdout.readline().strip().split(":")[1]

  94. # 获取设备总内存

  95. defget_total_memory():

  96. result=shell('cat /proc/meminfo|findstr "MemTotal"').stdout.readline().split(":")[1].strip()

  97. MemTotal="{val}MB".format(val=int(result.split()[0])/1024)

  98. returnMemTotal

  99. # 获取设备分辨率

  100. defget_DisplayDeviceInfo():

  101. result=shell("dumpsys display | findstr DisplayDeviceInfo").stdout.readline().split(",")[1].strip()

  102. returnresult

  103. # 获取系统版本号

  104. defget_system_version():

  105. returnshell("cat /system/build.prop | findstr ro.build.version.release").stdout.readline().split("=")[1]

  106. # 获取SDK版本号

  107. defget_sdk_version():

  108. returnshell("cat /system/build.prop | findstr ro.build.version.sdk").stdout.readline().split("=")[1]

  109. # 获取设备CPU最大频率

  110. defget_cpu_max_frequency():

  111. max_frequency=shell("su -c 'cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq'").stdout.readline().strip()

  112. if"not found"inmax_frequency:

  113. max_frequency="Permission denied"

  114. returnmax_frequency

  115. else:

  116. return"{val}MHz".format(val=int(max_frequency)/1000)

  117. # 获取设备CPU最小频率

  118. defget_cpu_min_frequency():

  119. min_frequency=shell("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq").stdout.readline().strip()

  120. return"{val}MHz".format(val=int(min_frequency)/1000)

  121. # 截图保存至sd卡adbscreenshot目录并同步电脑

  122. defscreenshot():

  123. if"directory"inshell("ls /sdcard/adbScreenShot").stdout.readline():

  124. shell("mkdir /sdcard/adbScreenShot")

  125. shell("/system/bin/screencap -p /sdcard/adbScreenShot/%s.png"%timestamp()).wait()

  126. ifos.path.exists("存储地址"):

  127. shutil.rmtree("存储地址")

  128. adb("pull /sdcard/adbscreenshot 存储地址")

  129. if__name__=="__main__":

  130. screenshot()
复制代码



作者: qqq911    时间: 2018-6-26 11:23
感谢分享·~
作者: Miss_love    时间: 2020-12-29 10:57
感谢分享




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2