Skip to content

Instantly share code, notes, and snippets.

@remh
Created January 12, 2015 19:19
Show Gist options
  • Select an option

  • Save remh/c750ee64a6713f3ad222 to your computer and use it in GitHub Desktop.

Select an option

Save remh/c750ee64a6713f3ad222 to your computer and use it in GitHub Desktop.
from hashlib import sha512
import time
import random
import string
import sys
import os
import multiprocessing
chars = string.letters
for i in range(10):
chars += str(i)
TIMINGS = {
"EASY": 20,
"MEDIUM": 50,
"HARD": 100,
"CRAZY": 200,
}
SCENARIOS = {
"CPU-EASY": [15, 1000, False],
"CPU-MEDIUM": [50, 1000, False],
"CPU-HARD": [90, 3000, False],
"CPU-CRAZY": [101, 10000, False],
"CPU-MEM-EASY": [15, 1000, True],
"CPU-MEM-MEDIUM": [50, 1000, True],
"CPU-MEM-HARD": [70, 4000, True],
"CPU-MEM-CRAZY": [95, 10000, True]
}
def clog_cpu(seconds, percentage, N, keep_in_memory):
memory = []
start = time.time()
print "Clogging CPU for {0} seconds".format(seconds)
if keep_in_memory:
print "Keeping in memory"
while True:
if time.time() - start > seconds:
break
if random.randint(0, 100) > percentage:
time.sleep(0.001)
continue
st = ""
for i in range(N):
st += chars[random.randint(0, len(chars) -1)]
_hash = sha512(st).hexdigest()
if keep_in_memory:
memory.append(_hash * N)
print "Clogged CPU for {0} seconds".format(seconds)
del memory
def main():
while True:
timing = TIMINGS.keys()[random.randint(0, len(TIMINGS) -1)]
scenario = SCENARIOS.keys()[random.randint(0, len(SCENARIOS) -1)]
print "Running scenario {0} with a {1} timing".format(scenario, timing)
args = [TIMINGS[timing]]
args.extend(SCENARIOS[scenario])
clog_cpu(*args)
sleep_time = random.randint(0, 30)
print "Sleeping for {0}s".format(sleep_time)
time.sleep(sleep_time)
if __name__ == "__main__":
print "pid is:"
print os.getpid()
for num in range(multiprocessing.cpu_count()):
multiprocessing.Process(target=main, args=[]).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment