Skip to content

Commit 931af71

Browse files
committed
APP启动时间和CPU占用率数据获取分析的脚本
1 parent 4d2a73e commit 931af71

File tree

12 files changed

+369
-1
lines changed

12 files changed

+369
-1
lines changed

.idea/AndroidTestByPython.iml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,149 @@
11
# AndroidTestByPython
2-
Android使用Python进行自动化测试
2+
Android APP使用Python进行性能测试
3+
4+
### APP启动时间
5+
6+
#### 冷启动
7+
8+
- 启动APP命令
9+
10+
```shell
11+
adb shell am start -W -n package/activity
12+
```
13+
14+
- 停止App命令
15+
16+
```shell
17+
adb shell am force-stop package
18+
```
19+
20+
- 获取某个APP的packageName和ActivityName的命令
21+
22+
```shell
23+
adb logcat | grep START
24+
```
25+
26+
然后打开你想获取packageName和ActivityName的应用即可。
27+
28+
#### 热启动
29+
30+
- 启动APP命令和冷启动一样
31+
32+
- 停止App命令
33+
34+
```shell
35+
// 模拟点击back键
36+
adb shell input keyevent 3
37+
```
38+
39+
40+
41+
#### 自动化脚本的实现
42+
43+
两种方案:
44+
45+
- 获取命令执行时间,作为启动时间参考值
46+
47+
```python
48+
class App(object):
49+
def __init__(self):
50+
self.content = ""
51+
self.startTime = 0
52+
53+
# 启动APP
54+
def launch_app(self):
55+
cmd = 'adb shell am start -W -n org.chromium.webview_shell/.WebViewBrowserActivity' # type: str
56+
self.content = os.popen(cmd)
57+
58+
# 停止app
59+
@staticmethod
60+
def stop_app():
61+
# 冷启动停止的命令
62+
# cmd = 'adb shell am force-stop org.chromium.webview_shell'
63+
# 热启动停止的命令
64+
cmd = 'adb shell input keyevent 3'
65+
os.popen(cmd)
66+
67+
# 获取启动时间
68+
def get_launched_time(self):
69+
for line in self.content.readlines():
70+
if "ThisTime" in line:
71+
self.startTime = line.split(":")[1]
72+
break
73+
return self.startTime
74+
75+
# 单次测试过程
76+
def test_process(self):
77+
self.app.launch_app()
78+
time.sleep(5)
79+
elapsed_time = self.app.get_launched_time()
80+
self.app.stop_app()
81+
time.sleep(3)
82+
current_time = self.get_current_time()
83+
self.all_data.append((current_time, elapsed_time))
84+
85+
# 多次执行测试过程
86+
def run(self):
87+
while self.counter > 0:
88+
self.test_process()
89+
self.counter = self.counter - 1
90+
```
91+
92+
93+
94+
- 在命令前后加上时间戳,以差值作为参考值
95+
96+
```python
97+
# 单次测试过程
98+
def test_process(self):
99+
time_before_launch = int(time.time())
100+
self.app.launch_app()
101+
time_after_launch = int(time.time())
102+
time.sleep(5)
103+
# elapsed_time = self.app.get_launched_time()
104+
elapsed_time = time_after_launch - time_before_launch
105+
self.app.stop_app()
106+
time.sleep(3)
107+
current_time = self.get_current_time()
108+
self.all_data.append((current_time, str(elapsed_time)))
109+
110+
# 多次执行测试过程
111+
def run(self):
112+
while self.counter > 0:
113+
self.test_process()
114+
self.counter = self.counter - 1
115+
```
116+
117+
> 可以多次计算启动时间值,并保存到本地csv文件,进行统计分析,求平均值等,第一次获得的值最好弃用。
118+
119+
### APP的CPU占用情况
120+
121+
#### 获取数据
122+
123+
##### 命令
124+
125+
```shell
126+
# windows系统的cmd中,用findstr来过滤字符,不能用grep。
127+
adb shell dumpsys cpuinfo | grep packagename
128+
```
129+
130+
##### 脚本的代码实现
131+
132+
```python
133+
# 单次测试过程
134+
def test_process(self):
135+
# window 下用findstr,Mac下用grep
136+
cmd = "adb shell dumpsys cpuinfo | findstr org.chromium.webview_shell"
137+
self.result = os.popen(cmd)
138+
cpu_value = 0
139+
for line in self.result.readlines():
140+
cpu_value = line.split("%")[0]
141+
142+
current_time = self.get_current_time()
143+
self.all_data.append((current_time, cpu_value))
144+
```
145+
146+
147+
148+
149+

launchTime/__init__.py

Whitespace-only changes.

launchTime/cpuStatus.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# encoding:utf-8
2+
import csv
3+
import os
4+
import time
5+
6+
7+
# 控制类
8+
class Controller(object):
9+
def __init__(self, count):
10+
self.result = ""
11+
self.counter = count
12+
self.all_data = [("timestamp", "cpuStatus")]
13+
14+
# 单次测试过程
15+
def test_process(self):
16+
# window 下用findstr,Mac下用grep
17+
cmd = "adb shell dumpsys cpuinfo | findstr org.chromium.webview_shell"
18+
self.result = os.popen(cmd)
19+
cpu_value = 0
20+
for line in self.result.readlines():
21+
cpu_value = line.split("%")[0]
22+
23+
current_time = self.get_current_time()
24+
self.all_data.append((current_time, cpu_value))
25+
26+
# 多次执行测试过程
27+
def run(self):
28+
while self.counter > 0:
29+
self.test_process()
30+
self.counter = self.counter - 1
31+
time.sleep(3)
32+
33+
# 获取当前的时间戳
34+
def get_current_time(self):
35+
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
36+
return current_time
37+
38+
# 数据的存储
39+
def save_data_to_csv(self):
40+
csv_file = file('cpustatus.csv', 'wb')
41+
writer = csv.writer(csv_file)
42+
writer.writerows(self.all_data)
43+
csv_file.close()
44+
45+
46+
if __name__ == '__main__':
47+
controller = Controller(10)
48+
controller.run()
49+
controller.save_data_to_csv()

launchTime/cpustatus.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
timestamp,cpuStatus
2+
2020-03-12 18:06:29, 37
3+
2020-03-12 18:06:32, 37
4+
2020-03-12 18:06:35, 37
5+
2020-03-12 18:06:38, 37
6+
2020-03-12 18:06:41, 37
7+
2020-03-12 18:06:44, 37
8+
2020-03-12 18:06:47, 37
9+
2020-03-12 18:06:51, 37
10+
2020-03-12 18:06:54, 37
11+
2020-03-12 18:06:57, 37

launchTime/launchTime.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# encoding:utf-8
2+
import csv
3+
import os
4+
import time
5+
6+
# APP类
7+
class App(object):
8+
def __init__(self):
9+
self.content = ""
10+
self.startTime = 0
11+
12+
# 启动APP
13+
def launch_app(self):
14+
cmd = 'adb shell am start -W -n org.chromium.webview_shell/.WebViewBrowserActivity' # type: str
15+
self.content = os.popen(cmd)
16+
17+
# 停止app
18+
@staticmethod
19+
def stop_app():
20+
# 冷启动停止的命令
21+
# cmd = 'adb shell am force-stop org.chromium.webview_shell'
22+
# 热启动停止的命令
23+
cmd = 'adb shell input keyevent 3'
24+
os.popen(cmd)
25+
26+
# 获取启动时间
27+
def get_launched_time(self):
28+
for line in self.content.readlines():
29+
if "ThisTime" in line:
30+
self.startTime = line.split(":")[1]
31+
break
32+
return self.startTime
33+
34+
35+
# 控制类
36+
class Controller(object):
37+
def __init__(self, count):
38+
self.app = App()
39+
self.counter = count
40+
self.all_data = [("timestamp", "elapsedTime")]
41+
42+
# 单次测试过程
43+
def test_process(self):
44+
time_before_launch = int(time.time())
45+
self.app.launch_app()
46+
time_after_launch = int(time.time())
47+
time.sleep(5)
48+
# elapsed_time = self.app.get_launched_time()
49+
elapsed_time = time_after_launch - time_before_launch
50+
self.app.stop_app()
51+
time.sleep(3)
52+
current_time = self.get_current_time()
53+
self.all_data.append((current_time, str(elapsed_time)))
54+
55+
# 多次执行测试过程
56+
def run(self):
57+
while self.counter > 0:
58+
self.test_process()
59+
self.counter = self.counter - 1
60+
61+
# 获取当前的时间戳
62+
@staticmethod
63+
def get_current_time():
64+
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
65+
return current_time
66+
67+
# 数据的存储
68+
def save_data_to_csv(self):
69+
# 冷启动生成的文件
70+
# csv_file = file('startTime.csv', 'wb')
71+
# 热启动生成的文件
72+
csv_file = file('startTime2.csv', 'wb')
73+
writer = csv.writer(csv_file)
74+
writer.writerows(self.all_data)
75+
csv_file.close()
76+
77+
78+
if __name__ == '__main__':
79+
controller = Controller(10)
80+
controller.run()
81+
controller.save_data_to_csv()

launchTime/startTime.csv

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
timestamp,elapsedTime
2+
2020-03-11 11:58:56," 1794
3+
"
4+
2020-03-11 11:58:58," 1216
5+
"
6+
2020-03-11 11:59:01," 961
7+
"
8+
2020-03-11 11:59:03," 918
9+
"
10+
2020-03-11 11:59:05," 870
11+
"
12+
2020-03-11 11:59:08," 1176
13+
"
14+
2020-03-11 11:59:10," 902
15+
"
16+
2020-03-11 11:59:12," 1049
17+
"
18+
2020-03-11 11:59:14," 873
19+
"
20+
2020-03-11 11:59:16," 849
21+
"

launchTime/startTime2.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
timestamp,elapsedTime
2+
2020-03-12 11:38:32,0
3+
2020-03-12 11:38:41,0
4+
2020-03-12 11:38:50,0
5+
2020-03-12 11:38:59,0
6+
2020-03-12 11:39:08,0
7+
2020-03-12 11:39:17,0
8+
2020-03-12 11:39:26,0
9+
2020-03-12 11:39:35,0
10+
2020-03-12 11:39:44,0
11+
2020-03-12 11:39:53,0

0 commit comments

Comments
 (0)