forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperftest.py
50 lines (45 loc) · 1.26 KB
/
perftest.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import subprocess
import time
import psutil
def get_process_info(p):
try:
cpu = int(p.get_cpu_percent(interval=0)) #0: non-blocking
rss, vms = p.get_memory_info()
name = p.name
pid = p.pid
except psutil.error.NoSuchProcess, e:
name = "Invalid process"
pid = 0
rss = 0
vms = 0
cpu = 0
return {'cpu': cpu, 'rss': rss, 'vms': vms}
def main():
conf_file = open('perftest.json')
conf = json.load(conf_file)
conf_file.close()
cmd = conf['cmd']
# shell == True: process is shell process
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
p = psutil.Process(proc.pid)
if not p.is_running():
return
name = p.name
pid = p.pid
record = []
print 'recoding...'
while True:
if not p.is_running():
break
record.append(get_process_info(p))
time.sleep(1)
proc_history = { 'name': name, 'pid': pid, 'history': record }
print proc_history
with open('perftest_result.json', 'wb') as fp:
json.dump(proc_history, fp)
print 'result is saved in perftest_result.json'
if __name__ == '__main__':
main()