Skip to content

Instantly share code, notes, and snippets.

@vsoch vsoch/result.json
Last active May 11, 2019

Embed
What would you like to do?
An example of using the watchme psutils decorator to monitor resource usage of a function
[
{
"num_ctx_switches": {
"voluntary": 1,
"involuntary": 0
},
"ionice": {
"ioclass": "IOPRIO_CLASS_NONE",
"value": 4
},
"cpu_percent": 0.0,
"create_time": 1557594156.2,
"memory_percent": 0.05404730892787495,
"pid": 27398,
"num_fds": 8,
"connections": [],
"ppid": 27394,
"cpu_times": {
"user": 0.0,
"system": 0.0,
"children_user": 0.0,
"children_system": 0.0
},
"cpu_num": 3,
"memory_full_info": {
"rss": 11300864,
"vms": 84086784,
"shared": 3092480,
"text": 2875392,
"lib": 0,
"data": 8486912,
"dirty": 0,
"uss": 3637248,
"pss": 7037952,
"swap": 0
},
"status": "sleeping",
"cwd": "/home/vanessa/Documents/Dropbox/Code/Python/watchme/docs/_docs/examples",
"cpu_affinity": [
0,
1,
2,
3
],
"num_threads": 1,
"cmdline": [
"python",
"test_psutils_decorator_create.py"
],
"gids": {
"real": 1000,
"effetive": 1000,
"saved": 1000
},
"io_counters": {
"read_count": 1,
"write_count": 2,
"read_bytes": 0,
"write_bytes": 0,
"read_chars": 2496,
"write_chars": 77
},
"open_files": 0,
"uids": {
"real": 1000,
"effetive": 1000,
"saved": 1000
},
"terminal": "/dev/pts/19",
"nice": 0,
"exe": "/home/vanessa/anaconda3/bin/python3.6",
"name": "python",
"username": "vanessa"
},
{
"num_ctx_switches": {
"voluntary": 2,
"involuntary": 0
},
"ionice": {
"ioclass": "IOPRIO_CLASS_NONE",
"value": 4
},
"cpu_percent": 0.0,
"create_time": 1557594156.2,
"memory_percent": 0.05404730892787495,
"pid": 27398,
"num_fds": 8,
"connections": [],
"ppid": 27394,
"cpu_times": {
"user": 0.0,
"system": 0.0,
"children_user": 0.0,
"children_system": 0.0
},
"cpu_num": 3,
"memory_full_info": {
"rss": 11300864,
"vms": 84086784,
"shared": 3092480,
"text": 2875392,
"lib": 0,
"data": 8486912,
"dirty": 0,
"uss": 3772416,
"pss": 7105536,
"swap": 0
},
"status": "sleeping",
"cwd": "/home/vanessa/Documents/Dropbox/Code/Python/watchme/docs/_docs/examples",
"cpu_affinity": [
0,
1,
2,
3
],
"num_threads": 1,
"cmdline": [
"python",
"test_psutils_decorator_create.py"
],
"gids": {
"real": 1000,
"effetive": 1000,
"saved": 1000
},
"io_counters": {
"read_count": 1,
"write_count": 3,
"read_bytes": 0,
"write_bytes": 0,
"read_chars": 2496,
"write_chars": 104
},
"open_files": 0,
"uids": {
"real": 1000,
"effetive": 1000,
"saved": 1000
},
"terminal": "/dev/pts/19",
"nice": 0,
"exe": "/home/vanessa/anaconda3/bin/python3.6",
"name": "python",
"username": "vanessa"
}
]
#!/usr/bin/env python
from watchme.watchers.psutils.decorators import monitor_resources
from time import sleep
# Here we create a decorator to monitor "my func." Specifically, we:
# - want to use the watcher "decorator" that already exists. If we want to
# create on the fly, we would set creat=True
# - will record metrics every 3 seconds
# - to have somewhat of an impact on system resources we make a long list
# - we test to ensure that something is returned ("Hello!")
@monitor_resources('decorator', seconds=3)
def myfunc(iters, pause):
long_list = []
print("Generating a long list, pause is %s and iters is %s" % (pause, iters))
for i in range(iters):
long_list = long_list + (i*10)*['pancakes']
print("i is %s, sleeping %s seconds" % (i, pause))
sleep(pause)
return len(long_list)
# ensure the function runs when the file is called
if __name__ == '__main__':
print("Calling myfunc with 2 iters")
result = myfunc(2, 2)
print("Result list has length %s" % result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.