Skip to content

Instantly share code, notes, and snippets.

@haizaar
Last active May 24, 2021 06:08
Show Gist options
  • Save haizaar/91469f5c4dfdef1f1965 to your computer and use it in GitHub Desktop.
Save haizaar/91469f5c4dfdef1f1965 to your computer and use it in GitHub Desktop.
Simple CPU loader in Python for Kebernetes GKE
apiVersion: v1
kind: Pod
metadata:
name: cpu-large
spec:
containers:
- image: docker.io/haizaar/cpu-loader:1.1
name: cpu-large
resources:
requests:
cpu: "2500m"
apiVersion: v1
kind: Pod
metadata:
name: cpu-small
spec:
containers:
- image: docker.io/haizaar/cpu-loader:1.1
name: cpu-small
resources:
requests:
cpu: "500m"
import multiprocessing
import threading
import time
import sys
class Worker(multiprocessing.Process):
def __init__(self, total):
self.loops = 0
self.total = total
super().__init__()
def run(self):
threading.Thread(target=self.reporter).start()
self.load()
def load(self):
while True:
for i in range(1000):
i*i
self.loops += 1
def reporter(self):
last = 0
while True:
time.sleep(.1)
current = self.loops
with self.total.get_lock():
self.total.value += (current - last)
last = current
class Manager:
def __init__(self, amount=None):
self.total = multiprocessing.Value("i", 0)
amount = amount or multiprocessing.cpu_count()
for i in range(amount):
Worker(total=self.total).start()
self.report()
def report(self):
last = 0
while True:
time.sleep(1)
current = self.total.value
sys.stderr.write("%s loops/sec\n" % (current - last))
last = current
if __name__ == "__main__":
Manager()
FROM python:3.5-alpine
ADD https://gist.github.com/haizaar/91469f5c4dfdef1f1965/raw/702e42eefa28d2b81ffd09990edf61035f51638f/cpu_loader.py cpu_loader.py
CMD nice -n ${NICENESS:-0} python -u cpu_loader.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment