| 1 |
|
| 2 |
from __future__ import annotations |
| 3 |
import argparse |
| 4 |
import sys |
| 5 |
import time |
| 6 |
|
| 7 |
|
| 8 |
def consume_cpu(duration: int, load: int) -> None: |
| 9 |
"""Function to consume CPU proportional to 'load' for 'duration' seconds""" |
| 10 |
end_time = time.time() + duration |
| 11 |
while time.time() < end_time: |
| 12 |
for _ in range(load): |
| 13 |
pass |
| 14 |
|
| 15 |
|
| 16 |
def consume_memory(size: int) -> bytearray: |
| 17 |
"""Function to consume amount of memory specified by 'size' in megabytes""" |
| 18 |
|
| 19 |
bytes_in_mb = 1024 * 1024 |
| 20 |
return bytearray(size * bytes_in_mb) |
| 21 |
|
| 22 |
|
| 23 |
def main(duration: int, cpu_load: int, memory_size: int) -> None: |
| 24 |
print("this is of test of STDOUT") |
| 25 |
print("this is of test of STDERR", file=sys.stderr) |
| 26 |
_mem_hold = consume_memory(memory_size) |
| 27 |
consume_cpu(duration, cpu_load) |
| 28 |
print( |
| 29 |
f"Test completed. Consumed {memory_size} MB for {duration} seconds with CPU load factor {cpu_load}." |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
if __name__ == "__main__": |
| 34 |
parser = argparse.ArgumentParser( |
| 35 |
description="Test script to consume CPU and memory." |
| 36 |
) |
| 37 |
parser.add_argument( |
| 38 |
"--duration", type=int, default=60, help="Duration to run the test in seconds." |
| 39 |
) |
| 40 |
parser.add_argument( |
| 41 |
"--cpu-load", type=int, default=10000, help="Load factor to simulate CPU usage." |
| 42 |
) |
| 43 |
parser.add_argument( |
| 44 |
"--memory-size", |
| 45 |
type=int, |
| 46 |
default=10, |
| 47 |
help="Amount of memory to allocate in MB.", |
| 48 |
) |
| 49 |
args = parser.parse_args() |
| 50 |
main(args.duration, args.cpu_load, args.memory_size) |