Skip to content

Instantly share code, notes, and snippets.

@juancarlospaco
Last active April 6, 2018 19:37
Show Gist options
  • Save juancarlospaco/cefd6713a2f0392c55c4 to your computer and use it in GitHub Desktop.
Save juancarlospaco/cefd6713a2f0392c55c4 to your computer and use it in GitHub Desktop.
Simple Post-Execution Message for Apps, print used Resources and Running Time (A.K.A. Uptime)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, os
import logging as log
from datetime import datetime
try:
import resource # windows dont have resource
except ImportError:
resource = None
start_time = datetime.now()
def make_post_execution_message(app=__doc__.splitlines()[0].strip()):
"""Simple Post-Execution Message with information about RAM and Time.
>>> make_post_execution_message() >= 0
True
"""
ram_use = int(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss *
resource.getpagesize() / 1024 / 1024 if resource else 0)
ram_all = int(os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
/ 1024 / 1024 if sys.platform.startswith("linux") else 0)
msg = "Total Maximum RAM Memory used: ~{0} of {1} MegaBytes.".format(
ram_use, ram_all)
log.info(msg)
if start_time and datetime:
log.info("Total Working Time: {0}.".format(
datetime.now() - start_time))
return msg
if __name__ in '__main__':
log.basicConfig(level=-1) # basic logger
make_post_execution_message()
__import__("doctest").testmod(verbose=True, report=True, exclude_empty=1)
@juancarlospaco
Copy link
Author

juan@z:~$ python2 temp.py 

INFO:root:Total Maximum RAM Memory used: ~20 MegaBytes.
INFO:root:Total Working Time: 0:00:00.000205.
Trying:
    make_post_execution_message() >= 0
Expecting:
    True
INFO:root:Total Maximum RAM Memory used: ~26 MegaBytes.
INFO:root:Total Working Time: 0:00:00.036104.
ok
1 items passed all tests:
   1 tests in __main__.make_post_execution_message
1 tests in 1 items.
1 passed and 0 failed.
Test passed.

juan@z:~$ python3 temp.py

INFO:root:Total Maximum RAM Memory used: ~20 MegaBytes.
INFO:root:Total Working Time: 0:00:00.000225.
Trying:
    make_post_execution_message() >= 0
Expecting:
    True
INFO:root:Total Maximum RAM Memory used: ~25 MegaBytes.
INFO:root:Total Working Time: 0:00:00.094710.
ok
1 items passed all tests:
   1 tests in __main__.make_post_execution_message
1 tests in 1 items.
1 passed and 0 failed.
Test passed.

juan@z:~$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment