一烤咧、獲取APP啟動時間
1绝页、adb命令行獲取
adb shell am start -S -W com.xxxx.xxxx/com.xxxx.biz.main.ui.activity.SplashPageActivity
-S:表示每次啟動前先強行停止
2畜挨、python執(zhí)行adb命令
import subprocess
res = subprocess.Popen('adb shell am start -S -W com.xxxx.xxxx/com.xxxx.biz.main.ui.activity.SplashPageActivity', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
result = res.stdout.readlines()
3徊都、拆解結(jié)果中所需要的數(shù)據(jù)
# 系統(tǒng)啟動APP耗時
WaitTime = str(result[-2]).split(':')[-1].replace("\\n'", '')
# APP啟動耗時
TotalTime = str(result[-3]).split(':')[-1].replace("\\n'", '')
二徐紧、數(shù)據(jù)寫入Excel表格
1谭确、引入openpyxl處理表格內(nèi)容
from openpyxl import Workbook
# 創(chuàng)建一個新的工作簿
wb = Workbook()
# 獲取默認(rèn)sheet
ws = wb.active
ws.title = "DefaultSheet"
2士复、需要獲取每次啟動時間和平均值
ws['A1'] = '序號'
ws['B1'] = 'WaitTime'
ws['C1'] = 'TotalTime'
ws['D1'] = 'WaitTime平均值'
ws['E1'] = 'TotalTime平均值'
#保存累加結(jié)果图谷,計算平均值
launchlist = []
3、寫入數(shù)據(jù)阱洪,假設(shè)共啟動了10次
for index in range(0, 10):
# 寫入每次啟動時間
ws['B' + str(index + 2)] = WaitTime
ws['C' + str(index + 2)] = TotalTime
averWaitTime += WaitTime
averTotalTime += TotalTime
# 計算平均值
ws['D2'] = averWaitTime / 10
ws['E2'] = averTotalTime / 10
#寫完后便贵,一定要保存文件
wb.save('/Users/username/Desktop/comment.xlsx')
三、創(chuàng)建折線圖
1冗荸、引入折線圖包
from openpyxl.chart import (
LineChart,
Reference,
)
lineimage = LineChart()
2承璃、折線圖屬性
#折線圖標(biāo)題
lineimage.title = "啟動時間"
#Y軸標(biāo)題
c2.y_axis.title = "耗時(ms)"
#X軸標(biāo)題
c2.x_axis.title = "Date"
3、添加數(shù)據(jù)
# 折線圖原始數(shù)據(jù)獲取蚌本,min_col:起始列盔粹;min_row:起始行;max_col:結(jié)束列程癌;max_row:結(jié)束行
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=count)
lineimage.add_data(data, titles_from_data=True)
# X軸數(shù)據(jù)顯示舷嗡,min_col:起始列;min_row:起始行嵌莉;max_row:結(jié)束行
dates = Reference(ws, min_col=1, min_row=2, max_row=count)
lineimage.set_categories(dates)
四咬崔、結(jié)果示例
五、完整代碼
import subprocess
from openpyxl import Workbook
from openpyxl.chart import (
LineChart,
Reference,
)
# 創(chuàng)建一個新的工作簿
wb = Workbook()
# 獲取默認(rèn)sheet
ws = wb.active
ws.title = "DefaultSheet"
ws['A1'] = '序號'
ws['B1'] = 'WaitTime'
ws['C1'] = 'TotalTime'
ws['D1'] = 'WaitTime平均值'
ws['E1'] = 'TotalTime平均值'
launchlist = []
def adbrun(cmd):
res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
result = res.stdout.readlines()
return result
def launch():
# -S:表示每次啟動前先強行停止
cmd_start = 'adb shell am start -S -W com.xxxx.xxxx/com.xxxx.biz.main.ui.activity.SplashPageActivity'
result = adbrun(cmd_start)
# 系統(tǒng)啟動APP耗時
WaitTime = str(result[-2]).split(':')[-1].replace("\\n'", '')
# APP啟動耗時
TotalTime = str(result[-3]).split(':')[-1].replace("\\n'", '')
return {'WaitTime': int(WaitTime), 'TotalTime': int(TotalTime)}
def writeexcel(launchlist):
averWaitTime = 0
averTotalTime = 0
for index in range(0, len(launchlist)):
# 為X軸添加序列號
ws['A' + str(index + 2)] = index + 1
# 寫入每次啟動時間
WaitTime = launchlist[index]['WaitTime']
TotalTime = launchlist[index]['TotalTime']
ws['B' + str(index + 2)] = WaitTime
ws['C' + str(index + 2)] = TotalTime
averWaitTime += WaitTime
averTotalTime += TotalTime
# 計算平均值
ws['D2'] = averWaitTime / len(launchlist)
ws['E2'] = averTotalTime / len(launchlist)
wb.save('/Users/username/Desktop/comment.xlsx')
linechart(len(launchlist))
def linechart(count):
lineimage = LineChart()
lineimage.title = "啟動時間"
# 折線圖原始數(shù)據(jù)獲取烦秩,min_col:起始列垮斯;min_row:起始行;max_col:結(jié)束列只祠;max_row:結(jié)束行
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=count+1)
lineimage.add_data(data, titles_from_data=True)
# X軸數(shù)據(jù)顯示兜蠕,min_col:起始列;min_row:起始行抛寝;max_row:結(jié)束行
dates = Reference(ws, min_col=1, min_row=2, max_row=count+1)
lineimage.set_categories(dates)
# 折線圖添加的位置
ws.add_chart(lineimage, "G2")
wb.save('/Users/username/Desktop/comment.xlsx')
if __name__ == '__main__':
for index in range(0, 10):
launchlist.append(launch())
writeexcel(launchlist)