"""Run a command under CPU and disk load, to reproduce a flaky test. Every flaky test is treated as a product bug until shown otherwise, and most only fail when the machine is busy. This keeps two cores hashing and one process rewriting and fsyncing a 1 MiB file while the command runs: python3 test/manual/stress.py python3 test/e2e/run.py acp:AcpLoadTests With no command it runs the Python e2e suite. It exits with the command's code. """ import hashlib import multiprocessing import os import subprocess import sys import tempfile from multiprocessing.synchronize import Event BLOCK = b"x" * 1_048_576 def burn(stop: Event) -> None: while not stop.is_set(): hashlib.sha256(BLOCK).digest() def sync(stop: Event) -> None: with tempfile.TemporaryFile() as handle: while not stop.is_set(): handle.seek(0) handle.write(BLOCK) handle.flush() os.fsync(handle.fileno()) def main(command: list[str]) -> int: stop = multiprocessing.Event() load = [ multiprocessing.Process(target=task, args=(stop,)) for task in (burn, burn, sync) ] for process in load: process.start() try: return subprocess.run(command or [sys.executable, "test/e2e/run.py"]).returncode finally: stop.set() for process in load: process.join(5) if process.is_alive(): process.kill() if __name__ == "__main__": sys.exit(main(sys.argv[1:]))