Skip to content

Instantly share code, notes, and snippets.

@paulgessinger
Last active July 31, 2019 12:17
Show Gist options
  • Save paulgessinger/5ef2a493e2de7b4c5f6c4bc3599641b7 to your computer and use it in GitHub Desktop.
Save paulgessinger/5ef2a493e2de7b4c5f6c4bc3599641b7 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/bin/env python
from __future__ import print_function
import subprocess as sp
import sys
import time
from datetime import datetime
import re
import csv
import psutil
def get_mem_total():
return int(sp.check_output(["free -m | awk '/Mem:/ { print $2 }'"], shell=True).strip())
def get_mem():
return int(sp.check_output(["free -m | awk '/Mem:/ { print $3 }'"], shell=True).strip())
def main():
assert len(sys.argv) >= 2
cmd = sys.argv[1]
print(cmd)
# sp.check_call(cmd, shell=True)
p = psutil.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.STDOUT)
samples = [(datetime.now(), 0, 0)]
with open('timing.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(["datetime", "memory", "cpu"])
while p.poll() is None:
# print(samples)
t = (samples[-1][0] - samples[0][0])
t = t.seconds + t.microseconds/1e6
print("{1:%H:%M:%S} - {0:6>.2f}s - {2:>8.2f}MB - {3:>6.2f}% CPU".format(t, *samples[-1]))
writer.writerow(samples[-1])
# mem = get_mem()
now = datetime.now()
mem = 0
cpu = 0
# print(p.memory_info())
for subp in p.children(recursive=True):
# print(subp.name(), subp.pid)
try:
mem += subp.memory_info().rss
# cpu += subp.cpu_percent(interval=None)
except psutil.NoSuchProcess:
# process not available anymore, ok
pass
# print(subp.memory_info())
cpu = psutil.cpu_percent()
# in KB, go to MB
mem = mem / 1e6
# print(mem)
samples.append((now, mem, cpu))
time.sleep(0.5)
samples.append((datetime.now(), get_mem()))
writer.writerow(samples[-1])
delta = datetime.now() - samples[0][0]
print("TOTAL TIME: ", delta)
if "__main__" == __name__:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment