|
| 1 | +"""CPU and memory stress task for cloud_poc. |
| 2 | +
|
| 3 | +Submits a task that burns CPU for a given duration while repeatedly |
| 4 | +allocating and releasing chunks of memory. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + BROKER_URL="http://localhost:8000/api/v1/broker?api_key=<key>" \ |
| 8 | + python stress_task.py [cpu_seconds] [mem_mib_per_iter] [mem_iters] |
| 9 | +
|
| 10 | +Arguments (positional, all optional): |
| 11 | + cpu_seconds Seconds to keep the CPU busy (default: 10) |
| 12 | + mem_mib_per_iter MiB to allocate per iteration (default: 64) |
| 13 | + mem_iters Number of allocation iterations (default: 8) |
| 14 | +""" |
| 15 | + |
| 16 | +import asyncio |
| 17 | +import os |
| 18 | +import sys |
| 19 | + |
| 20 | +import offwork |
| 21 | +from offwork import progress |
| 22 | + |
| 23 | +broker_url = os.environ.get("BROKER_URL") |
| 24 | +if not broker_url: |
| 25 | + print("error: BROKER_URL is not set", file=sys.stderr) |
| 26 | + sys.exit(1) |
| 27 | + |
| 28 | +offwork.connect(broker_url) |
| 29 | + |
| 30 | + |
| 31 | +def _burn_cpu(seconds: float) -> int: |
| 32 | + """Busy-loop for *seconds* wall time. Returns iteration count.""" |
| 33 | + import time |
| 34 | + import hashlib |
| 35 | + |
| 36 | + deadline = time.monotonic() + seconds |
| 37 | + data = b"offwork-stress" * 256 # ~3.5 KB |
| 38 | + iters = 0 |
| 39 | + while time.monotonic() < deadline: |
| 40 | + hashlib.sha256(data).digest() |
| 41 | + iters += 1 |
| 42 | + return iters |
| 43 | + |
| 44 | + |
| 45 | +def _alloc_mib(mib: float) -> int: |
| 46 | + """Allocate *mib* MiB, touch every page, return checksum byte.""" |
| 47 | + size = int(mib * 1024 * 1024) |
| 48 | + chunk = bytearray(size) |
| 49 | + # Touch every 4096-byte page so the OS actually faults the memory in. |
| 50 | + for i in range(0, size, 4096): |
| 51 | + chunk[i] = (i // 4096) & 0xFF |
| 52 | + return chunk[-1] |
| 53 | + |
| 54 | + |
| 55 | +@offwork.task |
| 56 | +def stress(cpu_seconds: float, mem_mib_per_iter: float, mem_iters: int) -> dict: |
| 57 | + """Stress the CPU and memory on the worker. |
| 58 | +
|
| 59 | + Args: |
| 60 | + cpu_seconds: Wall-clock seconds to spend hashing. |
| 61 | + mem_mib_per_iter: MiB to allocate and touch per memory iteration. |
| 62 | + mem_iters: How many times to allocate / release that block. |
| 63 | +
|
| 64 | + Returns a summary dict with hash iteration count and total memory touched. |
| 65 | + """ |
| 66 | + import time |
| 67 | + |
| 68 | + start = time.monotonic() |
| 69 | + total_steps = mem_iters # N CPU phase + N mem phases |
| 70 | + |
| 71 | + checksums = [] |
| 72 | + for i in range(mem_iters): |
| 73 | + # --- CPU phase --- |
| 74 | + progress(i, total_steps, message=f"Iter {i + 1}/{mem_iters}") |
| 75 | + hash_iters = _burn_cpu(cpu_seconds) |
| 76 | + checksums.append(_alloc_mib(mem_mib_per_iter)) |
| 77 | + |
| 78 | + elapsed = time.monotonic() - start |
| 79 | + total_mib = mem_mib_per_iter * mem_iters |
| 80 | + return { |
| 81 | + "elapsed_seconds": round(elapsed, 2), |
| 82 | + "cpu_seconds_requested": cpu_seconds, |
| 83 | + "hash_iterations": hash_iters, |
| 84 | + "memory_iterations": mem_iters, |
| 85 | + "mem_mib_per_iter": mem_mib_per_iter, |
| 86 | + "total_mem_touched_mib": total_mib, |
| 87 | + "checksum": sum(checksums) & 0xFF, |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | +async def main() -> None: |
| 92 | + cpu_seconds = float(sys.argv[1]) if len(sys.argv) > 1 else 10.0 |
| 93 | + mem_mib_per_iter = float(sys.argv[2]) if len(sys.argv) > 2 else 64.0 |
| 94 | + mem_iters = int(sys.argv[3]) if len(sys.argv) > 3 else 8 |
| 95 | + |
| 96 | + print(f"submitting stress task: cpu={cpu_seconds}s mem={mem_mib_per_iter}MiB x {mem_iters} iters") |
| 97 | + future = await stress.start(cpu_seconds, mem_mib_per_iter, mem_iters) |
| 98 | + print(f"task id: {future.task_id}") |
| 99 | + |
| 100 | + while not await future.done(): |
| 101 | + p = await future.progress() |
| 102 | + if p is not None: |
| 103 | + print(f" [{p.current}/{p.total}] {p.message}") |
| 104 | + await asyncio.sleep(2.0) |
| 105 | + |
| 106 | + result = await future |
| 107 | + print("\nresult:") |
| 108 | + for k, v in result.items(): |
| 109 | + print(f" {k}: {v}") |
| 110 | + |
| 111 | + |
| 112 | +asyncio.run(main()) |
0 commit comments