Quick Stresstest

A quick python script to eat up some cpu.

Run with this command to use more cores:

powershell:
foreach ($i in 1..8) { Start-Job { python your_script.name.py } }

linux:
for i in {1..8}; do python3 your_script_name.py & done

python:

import sys
from decimal import Decimal, getcontext

import sys
from decimal import Decimal, getcontext

def stress_test_pi():
# Set precision very high to force heavy CPU math
# Increase this number for more RAM usage + CPU load
getcontext().prec = 10000

print(“Starting Pi Stress Test… Press Ctrl+C to stop.”)

pi = Decimal(0)
k = 0

try:
while True:
# Using Nilakantha Series for faster convergence/heavy math
# Formula: 3 + 4/(2*3*4) – 4/(4*5*6) + 4/(6*7*8)…
if k == 0:
pi = Decimal(3)
else:
d = Decimal(2 * k)
term = Decimal(4) / (d * (d + 1) * (d + 2))
if k % 2 == 1:
pi += term
else:
pi -= term

# Print every 1000th iteration to avoid I/O bottlenecks
if k % 1000 == 0:
sys.stdout.write(f”\rCalculated {k} terms… Current Pi: {str(pi)[:50]}…”)
sys.stdout.flush()

k += 1
except KeyboardInterrupt:
print(“\n\nTest stopped by user.”)
print(f”Final terms calculated: {k}”)

if __name__ == “__main__”:
stress_test_pi()

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>