Skip to content

Instantly share code, notes, and snippets.

@wayerr
Last active October 12, 2017 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wayerr/3023edb55e91eef989c69df21d6d717a to your computer and use it in GitHub Desktop.
Save wayerr/3023edb55e91eef989c69df21d6d717a to your computer and use it in GitHub Desktop.
load cpu by specified schedule
#!/usr/bin/python3
import argparse
import time
import os
import math
# data, each byte of it used as cpu load value: (byte/255.0)
data = "0020305060708090A0B0C0D0E0F0FFA050"
def load_cpu(interval: int, fract: float):
start = time.perf_counter()
old_curr = start
sleep_fract = (1.0 - fract)
sleep_time = interval
delta = 0.01 # 1ms
decr = delta * sleep_fract
while True:
curr = time.perf_counter()
if (curr - start) >= interval:
break
if (curr - old_curr) > delta:
sleep_time -= decr
time.sleep(decr)
old_curr = curr
else:
for i in range(1000): # it make CPU load
i = math.sqrt(math.pi)
print("sleep time: %s to %s, by decr: %s " % (interval, sleep_time, decr))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Load CPU by specified schedule.')
parser.add_argument('--interval', dest='interval', default=60, type=int,
help='Scheduled interval in seconds.')
parser.add_argument('--schedule', dest='schedule', default=data,
help='Schedule, HEX sequence like "00A0B0FF", each byte mean load at interval.')
args = parser.parse_args()
interval = args.interval
schedule = args.schedule
print("Run CPU Load with: interval=%s, schedule=%s" % (interval, schedule))
data_bytes = bytearray.fromhex(schedule)
os.sched_setaffinity(0, {0})
while True:
for byte in data_bytes:
fract = byte / 255.0
print("load_cpu %s, %s " % (interval, fract))
load_cpu(interval, fract)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment