Last active
April 6, 2018 19:37
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Author
juancarlospaco
commented
Jun 19, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment