Why should you care: Understanding and testing resource utilization is crucial in Kubernetes and containerized environments. These resource-hogging tools provide a practical way to simulate extreme conditions, enabling you to validate scaling and autoscaling mechanisms effectively. By stress testing your system under high CPU and memory loads, you can identify performance bottlenecks, verify resource allocation strategies, and ensure your infrastructure responds correctly to varying demands. This proactive approach helps in fine-tuning your Kubernetes configurations, improving overall system reliability, and preparing for real-world scenarios where sudden spikes in resource usage could occur. Ultimately, these tests contribute to building more robust, efficient, and resilient containerized applications and infrastructure.
CPU hogging script:
#!/bin/bash
while true; do
for i in $(seq 1 10000); do
if [ $(echo "$(factor $i)" | wc -w) -eq 2 ]; then
echo $i
fi
done
done
How it works:
This script is a CPU hog that continuously calculates prime numbers up to 10000 in an infinite loop. It uses the computationally intensive process of factoring each number and checking if it has exactly two factors (itself and 1) to determine primeness. The script runs without any pauses, rapidly iterating through numbers and performing calculations, which causes it to consistently consume a high percentage of available CPU resources. This constant, intensive computation makes it an effective tool for testing CPU utilization and performance in various environments.
Memory hogging script:
#!/usr/bin/env python3
import time
# Allocate a large list (approximately 512 MB)
x = [0] * (1024 * 1024 * 1024)
# Sleep to keep the memory allocated
while True:
time.sleep(1000000)
How it works: This Python script is designed to hog memory by allocating a large amount of RAM and keeping it occupied. It creates a list 'x' containing 1 billion zeros, which consumes approximately 8 GB of memory (since each integer typically uses 8 bytes in Python 3). After allocating this memory, the script enters an infinite loop where it sleeps for a very long time (1,000,000 seconds, or about 11.5 days). This sleep keeps the process running and prevents the memory from being released. As a result, the script quickly consumes a significant portion of the system's available RAM and keeps it allocated, effectively hogging the memory resources for an extended period.
Pack it all into a container using this Dockerfile:
FROM python:3.9-slim
# Install bash and other necessary tools
RUN apt-get update && apt-get install -y bash bc
# Copy the scripts into the container
COPY cpu-hog.sh /usr/local/bin/cpu-hog.sh
COPY memory-hog.py /usr/local/bin/memory-hog.py
# Make the scripts executable
RUN chmod +x /usr/local/bin/cpu-hog.sh /usr/local/bin/memory-hog.py
# Set the default command to bash
CMD ["/bin/bash"]
Build it:
$ docker build -t antonbiz/resource-hog:1.0 .
Run it:
$ docker run -it --rm --name resource-hog --cpuset-cpus="0" --cpus=0.9 --memory=12.8g antonbiz/resource-hog:1.0