#!/usr/bin/env python3
"""Allocates roughly <intensity> MB (+/-20% random noise) and 1000 MB more
and holds it for one second.

The added noise is a harder test for slurmise fits
"""

import argparse
import random
import time


def main():
    parser = argparse.ArgumentParser(description="Allocate noisy memory and hold it for one second.")
    parser.add_argument("--intensity", type=int, default=1000, help="target megabytes of memory (before noise)")
    args = parser.parse_args()

    noise = random.uniform(-0.2, 0.2)
    actual_mb = max(1, round(args.intensity * (1 + noise))) + 1000

    # bytearray zero-fills the buffer, forcing every page resident so the
    # allocation shows up in SLURM's MaxRSS rather than being lazily reserved.
    _ = bytearray(actual_mb * 1024 * 1024)
    time.sleep(1)


if __name__ == "__main__":
    main()
