-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark.py
60 lines (52 loc) · 1.67 KB
/
benchmark.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
51
52
53
54
55
56
57
58
59
import time
import pickle
import os
import subprocess
import sys
import signal
def gather_stats(filename):
try:
stats = {}
_gather_stats(stats)
except:
f = open(filename,"w")
pickle.dump(stats, f)
f.close()
def _gather_stats(stats):
mem_stat={}
stats["mem"] = mem_stat
mem_infos = [line.split() for line in open("/proc/meminfo").read().replace(":","").split("\n") if line]
for mem_info in mem_infos: mem_stat[mem_info[0]] = []
cpu_stat={}
stats["cpu"] = cpu_stat
cpu_infos = [line.split() for line in open("/proc/stat").read().split("\n") if line]
for cpu_info in cpu_infos:
i = 1
for info in cpu_info[1:]:
cpu_stat[cpu_info[0] + "_" + str(i)] = []
i = i + 1
while 1:
mem_infos = [line.split() for line in open("/proc/meminfo").read().replace(":","").split("\n") if line]
for mem_info in mem_infos: mem_stat[mem_info[0]].append(mem_info[1])
cpu_infos = [line.split() for line in open("/proc/stat").read().split("\n") if line]
for cpu_info in cpu_infos:
i = 1
for info in cpu_info[1:]:
cpu_stat[cpu_info[0] + "_" + str(i)].append(info)
i = i + 1
time.sleep(1)
def benchmark(filename, cmd, margin, delay):
time.sleep(delay)
collector_pid = os.fork()
if not collector_pid:
gather_stats(filename)
sys.exit(0)
time.sleep(margin)
exec_pid = os.fork()
if not exec_pid:
os.execvp(cmd[0], cmd)
sys.exit(0)
os.waitpid(exec_pid, 0)
time.sleep(margin)
os.kill(collector_pid, signal.SIGINT)
os.waitpid(collector_pid, 0)