Files
wiki/cpu-bench/Dockerfile
2025-09-18 09:16:05 +09:00

55 lines
1.5 KiB
Docker

FROM python:slim
RUN apt update && apt install -y sysbench fastfetch
RUN pip install tqdm pandas tabulate
RUN tee bench.py <<EOF
#!/usr/bin/env python
def ensure(*packages):
for pkg in packages:
try: __import__(pkg)
except: import os; os.system(f'pip install -q {pkg}')
ensure('pandas', 'tabulate')
import subprocess, multiprocessing as mp, warnings, pandas as pd
from tqdm.auto import tqdm
from tabulate import tabulate
warnings.filterwarnings("ignore")
R, G, B, W = (f'\x1b[{x}m' for x in (31, 32, 34, 0))
subprocess.run('clear')
subprocess.run('fastfetch')
command = lambda core: f'sysbench cpu --cpu-max-prime=20000 --time=1 --threads={core} run | grep second'
print(f'{G}$ {command("$(nproc)")}{W}', flush=True)
def speed(core):
output = subprocess.run(
command(core), shell=True, capture_output=True, text=True
).stdout.split()[-1]
return float(output)
df = []
prev = 0
for core in range(1, mp.cpu_count()+1):
s = speed(core)
row = {
'#Threads': core,
'Throughput(/s)': s,
'(per-core)': s - prev
}
prev = s
df.append(row)
df = pd.DataFrame(df)
df.to_csv('result.csv', index=False)
df.iloc[:, 0] = df.iloc[:, 0].apply(lambda s: f'{R}{s}{W}')
df.iloc[:, 1] = df.iloc[:, 1].apply(lambda s: f'{G}{int(s)}{W}')
df.iloc[:, 2] = df.iloc[:, 2].apply(lambda s: f'{B}{int(s)}{W}')
print(tabulate(df, headers='keys', tablefmt='rounded_outline', showindex=False))
EOF
ENTRYPOINT ["python", "bench.py"]