Last active
May 24, 2021 06:08
-
-
Save haizaar/91469f5c4dfdef1f1965 to your computer and use it in GitHub Desktop.
Simple CPU loader in Python for Kebernetes GKE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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