andoird app性能测试详解

1、启动时间

冷启动:进程首次创建,并首次加载资源

热启动:非首次启动,在后台运行后,再进行启动

2、获取运行的APP的包名

adb shell logcat | grep "START"

注意:在linux下面,用grep,在windows下面用户findstr

3、冷启动APP

adb shell  am start -W -n activity

 

获取到TotalTime

注意-W,W是大写的

4、冷启动停止app命令

adb shell am force-stop packagenames

5、热启动,停止APP

adb shell input keyevent 3

6、利用python脚本获取启动时间

#/usr/bin/python
# -*- coding=utf-8 -*-
import os
import time
import csv

class App(object):
    def __init__(self):
        self.content=""
        self.startTime=0
    #启动APP
    def LaunchApp(self):
        cmd="adb shell  am start -W -n activitys"
        self.content=os.popen(cmd)
    #启动APP
    def StopApp(self):
        cmd='adb shell am force-stop packagenames'
        os.popen(cmd)
    #获取启动时间
    def GetLaunchedTime(self):
        for line in  self.content.readlines():
            if "ThisTime" in line:
                self.startTime=line.split(":")[1]
                break
        return self.startTime

#控制类
class Cotroller(object):
    def __init__(self,count):
        self.app=App()
        self.counter=count
        self.alldata=[("timestamp","elapsedtime")]
      #单次测试过程
    def testprocess(self):
        self.app.LaunchApp()
        elapsedtime=self.app.GetLaunchedTime()
        self.app.StopApp()
        currentime=self.getCurrentTime()
        self.alldata.append((currentime,elapsedtime))

    #多次执行测试过程
    def  run(self):
        while self.counter>0:
            self.testprocess()
            self.counter=self.counter-1
#获取当前时间戳
    def getCurrentTime(self):
        currenTime=time.strftime("%Y-%m-%d %H-%M-%S",time.localtime())
        return currenTime
#数据的存储
    def SaveDataToCSV(self):
        csvfile=file("starttime.csv","wb")
        writer=csv.writer(csvfile)
        writer.writerows(self.alldata)
        csvfile.close()

if __name__=="__main__":
    controller=Cotroller(50)
    controller.run()
    controller.SaveDataToCSV()

 

7、启动时间的均值,和曲线图

8、热启动

#/usr/bin/python
# -*- coding=utf-8 -*-
import os
import time
import csv

class App(object):
    def __init__(self):
        self.content=""
        self.startTime=0
    #启动APP
    def LaunchApp(self):
        cmd="adb shell  am start -W -n activity"
        self.content=os.popen(cmd)
    #启动APP
    def StopApp(self):
        cmd='adb shell input keyevent 3'
        os.popen(cmd)
    #获取启动时间
    def GetLaunchedTime(self):
        for line in  self.content.readlines():
            if "ThisTime" in line:
                self.startTime=line.split(":")[1]
                break
        return self.startTime

#控制类
class Cotroller(object):
    def __init__(self,count):
        self.app=App()
        self.counter=count
        self.alldata=[("timestamp","elapsedtime")]
      #单次测试过程
    def testprocess(self):
        self.app.LaunchApp()
        elapsedtime=self.app.GetLaunchedTime()
        self.app.StopApp()
        currentime=self.getCurrentTime()
        self.alldata.append((currentime,elapsedtime))

    #多次执行测试过程
    def  run(self):
        while self.counter>0:
            self.testprocess()
            self.counter=self.counter-1
#获取当前时间戳
    def getCurrentTime(self):
        currenTime=time.strftime("%Y-%m-%d %H-%M-%S",time.localtime())
        return currenTime
#数据的存储
    def SaveDataToCSV(self):
        csvfile=file("starttime1.csv","wb")
        writer=csv.writer(csvfile)
        writer.writerows(self.alldata)
        csvfile.close()

if __name__=="__main__":
    controller=Cotroller(50)
    controller.run()
    controller.SaveDataToCSV()

二、cpu的获取方法,脚本实现和数据分析

adb shell dumpsys cpuinfo |grep packagename

在windows下用findstr

#/usr/bin/python
# -*- coding=utf-8 -*-
import os
import time
import csv

#获取cpu的占比
#控制类
class Cotroller(object):
    def __init__(self,count):
        #self.app=App()
        self.content=""
        self.counter=count
        self.alldata=[("timestamp","cpustatus")]
      #单次测试过程
    def testprocess(self):
        cmd='adb shell  dumpsys cpuinfo | grep -e packagenames -e minor'
        self.content=os.popen(cmd)
        print "11"
        print self.content
        print self.content.readlines()
        for line in self.content.readlines():
            cpuvalue=line.split("%")[0]
            print cpuvalue

            currentime=self.getCurrentTime()
            #print currentime
            self.alldata.append((currentime,cpuvalue))

    #多次执行测试过程
    def  run(self):
        while self.counter>0:
            self.testprocess()
            self.counter=self.counter-1
            time.sleep(5)
#获取当前时间戳
    def getCurrentTime(self):
        currenTime=time.strftime("%Y-%m-%d %H-%M-%S",time.localtime())

        return currenTime
#数据的存储
    def SaveDataToCSV(self):
        csvfile=file("cpuinfo.csv","wb")
        writer=csv.writer(csvfile)
        writer.writerows(self.alldata)
        csvfile.close()

if __name__=="__main__":
    controller=Cotroller(1)
    controller.run()
    controller.SaveDataToCSV()

转载于:https://my.oschina.net/u/3950843/blog/1932975

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值