monkey命令性能测试-创新互联

new一下调试成功,可以直接使用

import time, os, random
import subprocess
import re

PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p))

#注意apk包的存放路径

class adbmokeybast(object):
     filename = ""
    packagename = ""
    versionCode = ""
    versionName = ""

    def __init__(self, adbdeves, locatpath):
        self.adbdeves = adbdeves
        self.locatpath = locatpath
        self.clsriz()

    def getApkBaseInfo(self):
        getcmd = 'aapt d badging  %s  |findstr "package launchable-activity"' % self.locatpath
        result = subprocess.Popen(getcmd, stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE,
                                  stdin=subprocess.PIPE, shell=True)
        (output, err) = result.communicate()
        match = re.compile("package: name='(\S+)' versionCode='(\d+)' versionName='(\S+)'").match(output.decode())
        if not match:
            raise Exception("can't get packageinfo")
        self.packagename = match.group(1)
        self.versionCode = match.group(2)
        self.versionName = match.group(3)

        print('packagename:' + self.packagename)
        print('versionCode:' + self.versionCode)
        print('versionName:' + self.versionName)
        return self.packagename, self.versionName, self.versionCode

    def clsriz(self):
        os.system("cls")  # 清楚日志
        print("adb 启动中...")
        # 链接模拟器 进行测试
        os.popen("adb connect 127.0.0.1:62001")
        os.popen("adb devices")  # 执行命令
        os.system("cls")

    def adbmokdeyaut(self):
        result = os.popen(self.adbdeves).readlines()  # os.popen()执行系统命令并返回执行后的信息对象
        devices = []
        devicesdec = {}
        # 保存选择设备号
        dev = ""
        alen = len(result)
        new_a = result[1:alen - 1]
        for i in range(len(new_a)):
            newadvices = new_a[i].split("\t")
            if newadvices[1] == "device\n":
                devices.append(newadvices[0])
                devicesdec[i] = newadvices[0]
            else:
                return "没有可以用的设备号"
        print(devices)
        print(devicesdec)
        try:
           while True:
                nSel = int(input("选择输入设备按序号就行: "))
                for k, v in devicesdec.items():
                    if nSel == k:
                        print("您输入的序号为:%d,设备号为: %s" % (int(nSel), devicesdec[nSel]))
                        dev = devicesdec[nSel]
                        break
                else:
                    print("你输入的有误!!!")
                    continue
                break
        except:
            print("输入有误,程序停止")
            return 0

        self.filename = input("输入保存的结果文件名:")
        fd = open(self.filename + '.cmd', 'w')
        monkeypath = "e:\\monkey\\" + self.filename
        if not os.path.isfile(monkeypath):
            os.mkdir(monkeypath)
        print(monkeypath)
        fd.write(":loop\n")
        fd.write("adb -s ")  # 选择设备
        fd.write(dev)  # 设备号
        # -s %random%的意思是防止产品相同的时间避免重复执行同一种动作  320000
        cmmmdgetapck = " shell monkey  -p ${packes} --monitor-native-crashes  --pct-touch 80 --pct-motion 15 --pct-nav 5 -s %random% -v  --throttle 500 320000".replace(
            "${packes}", self.packagename)
        fd.write(cmmmdgetapck + "  >" + monkeypath)
        fd.write("\\%random%.log\n")
        # ping自己15次用来延迟
        fd.write("@ping -n 15 127.1 >nul\n")
        fd.write("adb -s ")
        fd.write(dev)
        fd.write(" reboot\n")
        fd.write("@ping -n 120 127.1 >nul\n")
        fd.write("@goto loop")
        fd.close()
        print("批处理生成完毕")

    def cmdrun(self):
        path = os.getcwd()
        print(path)
        self.adbmokdeyaut()
        print("============" + self.filename)
        os.system(self.filename)

if __name__ == '__main__':
    locatpath = PATH(r'apk\XXXX.apk')
    am = adbmokeybast("adb devices", locatpath)
        #获取包名
    am.getApkBaseInfo()
        #启动全部程序
    # am.cmdrun()

old

#! /usr/bin/env python
# -*- coding:utf-8 -*-
# Author: liwen
'''
@time: 2018/8/6 9:19
'''
import os
import os.path

def clsriz():
    os.system("cls")  # 清楚日志
    print "adb 启动中..."
    os.popen("adb devices")  # 执行命令
    os.system("cls")

def operation():
    result = os.popen('adb devices').readlines()  # os.popen()执行系统命令并返回执行后的信息对象
    n = len(result) - 2
    print result
    print "检测到" + str(n) + "台设备:"
    devices = range(n)
    for i in range(n):
        nPos = result[i + 1].index("\t")
        devices[i] = result[i + 1][:nPos]
        print str(i + 1) + " - " + devices[i]
    print " "
    nSel = 1
    if n != 1:
        nSel = input("选择设备:")
    if nSel <= n:
        dev = devices[nSel - 1]

    fn = raw_input("输入文件名:")
    fd = open(fn + '.cmd', 'w')

    if not os.path.isdir("D:\\" + fn):
        os.mkdir("D:\\" + fn)

    fd.write(":loop\n")
    fd.write("adb -s ")  # 选择设备
    fd.write(dev)  # 设备号
    fd.write(
        " shell monkey  -p com.android.browser --monitor-native-crashes  --pct-touch 80 --pct-motion 15 --pct-nav 5 -s %random% -v  --throttle 500 320000 >d:\\")  # -s %random%的意思是防止产品相同的时间避免重复执行同一种动作
    fd.write(fn)
    fd.write("\\%random%.log\n")
    fd.write("@ping -n 15 127.1 >nul\n")  # ping自己15次用来延迟
    fd.write("adb -s ")
    fd.write(dev)
    fd.write(" reboot\n")
    fd.write("@ping -n 120 127.1 >nul\n")
    fd.write("@goto loop")

    fd.close()

    print "批处理生成完毕"

if __name__ == '__main__':
    # clsriz()
    operation()

修改版本

result = os.popen(self.adbdeves).readlines()  # os.popen()执行系统命令并返回执行后的信息对象
        devices = []
        devicesdec = {}
        # 保存选择设备号
        dev = ""
        alen = len(result)
        new_a = result[1:alen - 1]
        for i in range(len(new_a)):
            newadvices = new_a[i].split("\t")
            if newadvices[1] == "device\n":
                devices.append(newadvices[0])
                devicesdec[i] = newadvices[0]
            else:
                return "没有可以用的设备号"
        print(devices)
        print(devicesdec)
        try:
           while True:
                nSel = int(input("选择输入设备按序号就行: "))
                for k, v in devicesdec.items():
                    if nSel == k:
                        print("您输入的序号为:%d,设备号为: %s" % (int(nSel), devicesdec[nSel]))
                        dev = devicesdec[nSel]
                        break
                else:
                    print("你输入的有误!!!")
                    continue
                break
        except:
            print("输入有误,程序停止")
            return 0

#第二种方法:
 devices = []
    import subprocess
    result = subprocess.Popen("adb devices", shell=True, stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE).stdout.readlines()
    for item in result:
        t = item.decode().split("\tdevice")
        if len(t) >= 2:
            devices.append(t[0])
    return devices

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

目前创新互联已为数千家的企业提供了网站建设、域名、虚拟主机、网站托管维护、企业网站设计、樊城网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
当前名称:monkey命令性能测试-创新互联
文章网址:http://myzitong.com/article/dddgji.html