-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcpu_using.py
More file actions
71 lines (62 loc) · 2.11 KB
/
Copy pathcpu_using.py
File metadata and controls
71 lines (62 loc) · 2.11 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
from time import sleep
from utils import androiddebug, excel
import threading
import xlsxwriter as wx
import matplotlib.pyplot as plt
class CpuData(threading.Thread):
def __init__(self, pkg):
super(CpuData, self).__init__()
self.total_before = None
self.total_after = None
self.process_before = None
self.process_after = None
self.ls = []
self.pkg = pkg
# 获取APP的PID
def get_pid(self):
pid = androiddebug.get_app_pid(self.pkg)
return pid
# 获取总CPU时间
@staticmethod
def get_total_cpu_time():
time = androiddebug.shell("cat /proc/stat|gawk '{print $2,$3,$4,$5,$6,$7,$8,$9,$10,$11}'").stdout.readline().split()
total_time = 0
for i in time:
total_time += int(i)
return total_time
# 获取进程cpu时间
def get_process_cpu_time(self):
time = androiddebug.shell(
"cat /proc/%s/stat|gawk '{print $14,$15,$16,$17}'" % self.get_pid()).stdout.readline().split()
pro_time = 0
for i in time:
pro_time += int(i)
return pro_time
# 计算cpu占用率
def run(self):
self.total_before = self.get_total_cpu_time()
self.process_before = self.get_process_cpu_time()
while True:
if androiddebug.stop != True:
break
y = []
sleep(1)
tm = androiddebug.timestamp()
self.total_after = self.get_total_cpu_time()
self.process_after = self.get_process_cpu_time()
usage = str(
round(float(self.process_after - self.process_before) / float(self.total_after - self.total_before),
4) * 100)
y.append(tm)
y.append(usage)
# print "CPU占用率:%s\n" % usage
self.total_before = self.total_after
self.process_before = self.process_after
self.ls.append(y)
def get_cpu(self):
return self.ls
if __name__ == '__main__':
cpu = CpuData("com.longtu.weifuhua")
cpu.start()
cpu.join()