Commit 75db40b8 authored by Mike Smith's avatar Mike Smith

Revert to old python code that I can get working

parent b910fad7
#!/usr/bin/python
'''
hpc_example.py -t <wait time> -l <list length>
'''
from getopt import getopt, GetoptError
from sys import argv, exit
from psutil import Process
from os import getpid
#!/usr/bin/env python
import resource
import platform
from argparse import ArgumentParser
from sys import stdout
from time import sleep
from platform import node
def parseCommand(wait_time=10, list_length=0):
'''
parse the command and set the wait time and list length accordingly
'''
usage = 'hpc_example.py -t <wait time> -l <list length>'
# handle the arguments
def occupy_memory(length):
'''Create a list of ones of length <length>, to occupy memory in the system.'''
try:
opts, args = getopt(argv,"ht:l:",["time=","length="])
except GetoptError as err:
print(err)
print(usage)
exit(2)
for opt, arg in opts:
if opt == '-h':
print(usage)
exit()
elif opt in ("-t", "--time"):
wait_time = max(wait_time, float(arg))
elif opt in ("-l", "--length"):
list_length = max(list_length, int(arg))
return (wait_time, list_length)
def use_memory(list_length):
'''
create a list of length list_length
and return the amount of memory being used by this process in MB
'''
memory_use_list = list(range(list_length))
process = Process(getpid())
memory_used_MB = process.memory_info().rss / 1_000_000
return memory_used_MB
def main(argv):
wait_time, list_length = parseCommand()
print(f'Current host is: {node()}')
print(f'Wait time is: {wait_time} seconds')
print(f'List length is: {list_length}')
mem_used = use_memory(list_length)
print(f'Memory usage: {mem_used} MB')
sleep(wait_time) # wait for the specified number of seconds
if __name__ == "__main__":
main(argv[1:])
size = int(length)
except TypeError:
raise "Expected an integer value for size parameter, got '{}''".format(length)
print(f'List length is: {format(length)}')
list_of_ones = []
for i in range(length):
list_of_ones.append(1)
def wait_for(seconds):
'''Suspend execution for <seconds> seconds, to control the length of time
that the script takes to run.'''
try:
seconds = int(seconds)
except TypeError:
raise "Expected an integer value for time parameter, got '{}'".format(seconds)
print(f'Wait time is: {format(seconds)} seconds')
sleep(seconds)
parser = ArgumentParser() # Use argparse.ArgumentParser to create a simple user interface
parser.add_argument("--time", "-t", default=10, type=int,
help="Time (in whole seconds) that this script should take to run.")
parser.add_argument("--length", "-l", default=0, type=int,
help="Length of list to generate. This approximates the amount of memory that the script will take up when it runs. Must be an integer.")
# parse arguments from the command line
args = parser.parse_args()
secs = args.time
list_length = args.length
print(f'Current host is: {format(platform.node())}')
occupy_memory(list_length)
wait_for(secs)
# fetch the memory being used by the script and print it out
mem_used = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1000.0
print(f'Memory usage: {format(mem_used)}MB')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment