X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/blobdiff_plain/0e82b3c21c1e2a5c18149bf5834b46eed877898e..dbee4a4b26e10e4aac686ed7e01c8f8165e851e7:/unitproc/python/sleepRand.py diff --git a/unitproc/python/sleepRand.py b/unitproc/python/sleepRand.py index 3454d24..3917506 100755 --- a/unitproc/python/sleepRand.py +++ b/unitproc/python/sleepRand.py @@ -1,24 +1,58 @@ #!/usr/bin/env python3 # Desc: Pauses a random amount of time. Random distribution is inverse gaussian. -# Version: 0.0.3 +# Version: 0.0.4 # Depends: python 3.7.3 -# Usage: ./sleepRand.py arg1 -# Input: arg1: float seconds (mean of inverse gaussian distribution) -# Example: python3 sleepRand.py 4.0 +# Usage: ./sleepRand.py [-v] [-p L] SECONDS +# Input: SECONDS: float seconds (mean of inverse gaussian distribution) +# L: precision (lambda of inverse gaussian distribution) +# Example: python3 sleepRand.py -vv -p 8.0 60.0 -import math, time, random, sys +import argparse; +import math, time, random, sys; +import logging; -# Adjustments -lambdaFactor = 4; # spread factor; inversely proportional to variance +# Set up argument parser (see https://docs.python.org/3.7/library/argparse.html ) +parser = argparse.ArgumentParser( + description='Delay activity for a random number of seconds. Delays sampled from an inverse gaussian distribution.', + epilog="Author: Steven Baltakatei Sandoval. License: GPLv3+"); +parser.add_argument('-v','--verbose', + action='count', + dest='verbosity', + default=0, + help='Verbose output. (repeat for increased verbosity)'); +parser.add_argument('mean', + action='store', + metavar='SECONDS', + nargs=1, + default=1, + type=float, + help='Mean seconds of delay. Is the mean of the inverse gaussian distribution.'); +parser.add_argument('--precision','-p', + action='store', + metavar='P', + nargs=1, + default=[4.0], + type=float, + help='How concentrated delays are around the mean (default: 4.0). Must be a positive integer or floating point value. Is the lambda factor in the inverse gaussian distribution. High values (e.g. > 10.0) cause random delays to rarely stray far from MEAN. Small values (e.g. < 0.10) result in many small delays plus occasional long delays.'); +args = parser.parse_args(); # Define functions +def setup_logging(verbosity): + # Depends: module: argparse + # Ref/Attrib: Haas, Florian; Configure logging with argparse; https://xahteiwi.eu/resources/hints-and-kinks/python-cli-logging-options/ + base_loglevel = 30; + verbosity = min(verbosity, 2); + loglevel = base_loglevel - (verbosity * 10); + logging.basicConfig(level=loglevel, + format='%(message)s'); + def randInvGau(mu, lam): """Returns random variate of inverse gaussian distribution""" # input: mu: mean of inverse gaussian distribution # lam: shape parameter # output: float sampled from inv. gaus. with range 0 to infinity, mean mu # example: sample = float(randInvGau(1.0,4.0)); - # Ref/Attrib: doi:10.1080/00031305.1976.10479147 + # Ref/Attrib: Michael, John R. "Generating Random Variates Using Transformations with Multiple Roots" https://doi.org/10.2307/2683801 nu = random.gauss(0,1); y = nu ** 2; xTerm1 = mu; @@ -31,13 +65,32 @@ def randInvGau(mu, lam): else: return (mu ** 2 / x); -# Check input (TODO) -arg1 = float(sys.argv[1]); # first argument -desMean = arg1; +# Process input +## Start up logger +setup_logging(args.verbosity); +logging.debug('DEBUG:Debug logging output enabled.'); +logging.debug('DEBUG:args.verbosity:' + str(args.verbosity)); +logging.debug('DEBUG:args:' + str(args)); +## Reject negative floats. +try: + ### Get desired mean + desMean = args.mean[0]; + logging.debug('DEBUG:Desired mean:' + str(desMean)); + ### Get lambda precision factor + lambdaFactor = args.precision[0]; + logging.debug('DEBUG:Lambda precision factor:' + str(lambdaFactor)); + if desMean < 0: + logging.error('ERROR:Desired mean is negative:' + str(desMean)); + raise ValueError('Negative number error.'); + if lambdaFactor < 0: + logging.error('ERROR:Lambda precision factor is negative:' + str(lambdaFactor)); + raise ValueError('Negative number error.'); +except ValueError: + sys.exit(1); # Calculate delay delay = randInvGau(desMean, desMean * lambdaFactor); -#print('DEBUG:delay:' + str(float(delay))); +logging.debug('delay:' + str(delay)); # Sleep time.sleep(float(delay));