forked from hydcoder/AndroidTestByPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpuStatus.py
More file actions
51 lines (42 loc) · 1.35 KB
/
cpuStatus.py
File metadata and controls
51 lines (42 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# encoding:utf-8
import csv
import os
import time
# 控制类
class Controller(object):
def __init__(self, count):
self.result = ""
self.counter = count
self.all_data = [("timestamp", "cpuStatus")]
# 单次测试过程
def test_process(self):
# window 下用findstr,Mac下用grep
cmd = "adb shell dumpsys cpuinfo | findstr org.chromium.webview_shell"
self.result = os.popen(cmd)
cpu_value = 0
for line in self.result.readlines():
cpu_value = line.split("%")[0]
current_time = self.get_current_time()
self.all_data.append((current_time, cpu_value))
# 多次执行测试过程
def run(self):
while self.counter > 0:
self.test_process()
self.counter = self.counter - 1
# 每3秒采集一次数据
time.sleep(3)
# 获取当前的时间戳
@staticmethod
def get_current_time():
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return current_time
# 数据的存储
def save_data_to_csv(self):
csv_file = file('cpustatus.csv', 'wb')
writer = csv.writer(csv_file)
writer.writerows(self.all_data)
csv_file.close()
if __name__ == '__main__':
controller = Controller(10)
controller.run()
controller.save_data_to_csv()