summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
a967b28)
- note: option added in case user wishes to limit maximum possible
delay generated.
#!/usr/bin/env python3
# Desc: Pauses a random amount of time. Random distribution is inverse gaussian.
#!/usr/bin/env python3
# Desc: Pauses a random amount of time. Random distribution is inverse gaussian.
# Depends: python 3.7.3
# Usage: ./sleepRand.py [-v] [-p P] SECONDS
# Input: SECONDS: float seconds (mean of inverse gaussian distribution)
# Depends: python 3.7.3
# Usage: ./sleepRand.py [-v] [-p P] SECONDS
# Input: SECONDS: float seconds (mean of inverse gaussian distribution)
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.');
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.');
+parser.add_argument('--upper','-u',
+ action='store',
+ metavar='U',
+ nargs=1,
+ default=[None],
+ type=float,
+ help='Upper bound for possible delays (default: no bound). Without bound, extremely high delays are unlikely but possible.');
args = parser.parse_args();
# Define functions
args = parser.parse_args();
# Define functions
logging.debug('DEBUG:args.verbosity:' + str(args.verbosity));
logging.debug('DEBUG:args:' + str(args));
logging.debug('DEBUG:args.verbosity:' + str(args.verbosity));
logging.debug('DEBUG:args:' + str(args));
-## Reject negative floats.
+## Receive input arguments
try:
### Get desired mean
desMean = args.mean[0];
logging.debug('DEBUG:Desired mean:' + str(desMean));
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));
### Get lambda precision factor
lambdaFactor = args.precision[0];
logging.debug('DEBUG:Lambda precision factor:' + str(lambdaFactor));
+
+ ### Get upper bound
+ if isinstance(args.upper[0], float):
+ logging.debug('DEBUG:args.upper[0] is float:' + str(args.upper[0]));
+ upperBound = args.upper[0];
+ elif args.upper[0] is None:
+ logging.debug('DEBUG:args.upper[0] is None:' + str(args.upper[0]));
+ upperBound = None;
+ else:
+ raise TypeError('Upper bound not set correctly.');
+ logging.debug('DEBUG:Upper bound:' + str(upperBound));
+
+ ### Reject negative floats.
if desMean < 0:
logging.error('ERROR:Desired mean is negative:' + str(desMean));
raise ValueError('Negative number error.');
if desMean < 0:
logging.error('ERROR:Desired mean is negative:' + str(desMean));
raise ValueError('Negative number error.');
sys.exit(1);
# Calculate delay
sys.exit(1);
# Calculate delay
-delay = randInvGau(desMean, desMean * lambdaFactor);
-logging.debug('delay:' + str(delay));
+rawDelay = randInvGau(desMean, desMean * lambdaFactor);
+logging.debug('DEBUG:rawDelay(seconds):' + str(rawDelay));
+if isinstance(upperBound,float):
+ delay = min(upperBound, rawDelay);
+elif upperBound is None:
+ delay = rawDelay;
+logging.debug('DEBUG:delay(seconds) :' + str(delay));
# Sleep
time.sleep(float(delay));
# Sleep
time.sleep(float(delay));