Files
unkernel/test/guest/work/pycpu.py

36 lines
1.4 KiB
Python

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
# A pure-Python CPU workload for the guest (test/work-bench.sh): integers,
# floats, strings, dicts, small objects, a sort and recursion, N rounds
# (default 10). Prints a checksum, the same for a given N everywhere.
import sys
class Item:
__slots__ = ("id", "name", "score")
def __init__(self, id, name, score):
self.id, self.name, self.score = id, name, score
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
n = int(sys.argv[1]) if len(sys.argv) > 1 else 10
total = 0
for r in range(n):
x, f = (r * 2654435761) & 0xFFFFFFFF, 0.0
for i in range(4000):
x = (x * 1664525 + 1013904223) & 0xFFFFFFFF
f += (x & 0xFFFF) ** 0.5 * 0.5
total = (total + x + int(f)) & 0xFFFFFFFF
words = ["w" + format((x + i * 7919) % 9973, "x") for i in range(400)]
text = " ".join(words)
parts = [w.upper() for w in text.split(" ")]
total = (total + len(text) + sum(1 for w in parts if "A" in w) + text.find("w1")) & 0xFFFFFFFF
counts = {}
for w in words:
counts[w] = counts.get(w, 0) + 1
items = [Item(i, words[i], (x >> (i % 16)) & 255) for i in range(300)]
items.sort(key=lambda o: (o.score, o.id))
total = (total + len(counts) + min(counts) .__len__() + items[0].id + items[-1].id + fib(14)) & 0xFFFFFFFF
print(total)