Skip to content

Instantly share code, notes, and snippets.

@adhorn
Created July 11, 2019 12:12
Show Gist options
  • Save adhorn/68d06863f15a8ca54263dff9fb71d1a9 to your computer and use it in GitHub Desktop.
Save adhorn/68d06863f15a8ca54263dff9fb71d1a9 to your computer and use it in GitHub Desktop.
@corrupt_delay decorator
def corrupt_delay(func):
def latency(*args, **kw):
delay, rate = get_config('delay')
if not delay:
return func(*args, **kw)
print("delay: {0}, rate: {1}".format(delay, rate))
# if delay and rate exist, delaying with that value at that rate
start = time.time()
if delay > 0 and rate >= 0:
# add latency approx rate% of the time
if random.random() <= rate:
time.sleep(delay / 1000.0)
result = func(*args, **kw)
end = time.time()
print('Added {1:.2f}ms to {0:s}'.format(
func.__name__,
(end - start) * 1000
))
return result
return latency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment