rickub/ci-benchpublic Fork 0
0baf736bf04d237117e719e3f8b2c8d5860f6bd4
Commits
Clone
git clone https://git.rickub.com/rickub/ci-bench.git
git clone ssh://git@rickub.com/rickub/ci-bench.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

probe.sh · 132 lines · 4.6 KBBash Blame HistoryRaw
ci-bench: cross-platform CI race scaffold (rickub CI vs GitHub Actions) 0baf736 Olivier Girardot yesterday1#!/usr/bin/env bash
2# probe.sh — deterministic runner-capability probe. Every measurement degrades
3# to a "null" value (still a recorded line) when a tool is missing; the job
4# NEVER fails from the probe itself.
5#
6# Measurements:
7# probe-cpu single-core shell-arithmetic loop throughput (kops/s over a
8# fixed 1,000,000-iteration loop)
9# probe-write sequential write throughput (dd 2 GiB to the job scratch
10# dir, then removed)
11# probe-read sequential read throughput of that same file
12# probe-fs-type filesystem type of the scratch dir
13# probe-fs-free free space of the scratch dir (MB)
14#
15# Usage: run from the repo root (the scripts/ dir next to workloads/):
16# bash workloads/probe/probe.sh
17
18set -u
19
20REPO_ROOT=$(cd "$(dirname "$0")/../.." && pwd)
21# shellcheck disable=SC1091
22. "$REPO_ROOT/scripts/emit_timing.sh"
23
24# Scratch dir: the runner's temp when we have one, else mktemp -d.
25PROBE_DIR=${RUNNER_TEMP:-${TMPDIR:-/tmp}}
26mkdir -p "$PROBE_DIR" 2>/dev/null || PROBE_DIR=$(mktemp -d)
27PROBE_BIN=$PROBE_DIR/probe.bin
28
29now_s() { # fractional seconds, best available clock
30 local t
31 t=$(date -u +%s.%N 2>/dev/null || true)
32 case $t in
33 *N) t=$(python3 -c 'import time; print(f"{time.time():.6f}")' 2>/dev/null || echo '') ;;
34 esac
35 [ -n "$t" ] || t=$(date +%s).000
36 printf '%s\n' "$t"
37}
38
39probe_cpu() {
40 if ! command -v python3 >/dev/null 2>&1 && ! command -v bc >/dev/null 2>&1; then
41 bench_metric probe-cpu null kops_per_s # no fractional clock available
42 return 0
43 fi
44 local iters=1000000 t0 t1 elapsed
45 t0=$(now_s)
46 local i=0
47 while [ "$i" -lt "$iters" ]; do
48 i=$((i + 1))
49 done
50 t1=$(now_s)
51 if command -v python3 >/dev/null 2>&1; then
52 elapsed=$(python3 -c "print(f'{$t1 - $t0:.6f}')")
53 else
54 elapsed=$(echo "$t1 - $t0" | bc -l)
55 fi
56 bench_metric probe-cpu "$(python3 -c "print(f'{$iters / $elapsed / 1000:.2f}')" 2>/dev/null \
57 || echo "$iters $elapsed" | awk '{printf "%.2f", $1 / $2 / 1000}')" kops_per_s
58}
59
60# Parse the "bytes ... copied/transferred in N s" line dd writes to stderr.
61# GNU: "X bytes (...) copied, 3.123 s, ...". BSD: "X bytes transferred in
62# 3.123456 secs". Prints "BYTES SECONDS" on stdout, nothing when unmatched.
63# ERE (-E) because BSD sed has no BRE alternation.
64parse_dd() {
65 sed -nE 's/^([0-9]+) bytes.*[ ,] ?([0-9.]+) s.*/\1 \2/p'
66}
67
68probe_write() {
69 command -v dd >/dev/null 2>&1 || { bench_metric probe-write null MB_per_s; return 0; }
70 local out
71 out=$(dd if=/dev/zero of="$PROBE_BIN" bs=1M count=2048 2>&1) || true
72 local parsed
73 parsed=$(printf '%s\n' "$out" | parse_dd)
74 if [ -z "$parsed" ]; then
75 bench_metric probe-write null MB_per_s
76 return 0
77 fi
78 set -- $parsed
79 bench_metric probe-write "$(awk -v b="$1" -v s="$2" 'BEGIN { printf "%.1f", b / s / 1000000 }')" MB_per_s
80}
81
82probe_read() {
83 if [ ! -f "$PROBE_BIN" ]; then
84 bench_metric probe-read null MB_per_s # write probe never produced it
85 return 0
86 fi
87 command -v dd >/dev/null 2>&1 || { bench_metric probe-read null MB_per_s; return 0; }
88 # Drop the page cache when we can (best effort, usually not permitted);
89 # otherwise this measures warm cache — still comparable across platforms.
90 sync 2>/dev/null || true
91 local out
92 out=$(dd if="$PROBE_BIN" of=/dev/null bs=1M 2>&1) || true
93 local parsed
94 parsed=$(printf '%s\n' "$out" | parse_dd)
95 if [ -z "$parsed" ]; then
96 bench_metric probe-read null MB_per_s
97 return 0
98 fi
99 set -- $parsed
100 bench_metric probe-read "$(awk -v b="$1" -v s="$2" 'BEGIN { printf "%.1f", b / s / 1000000 }')" MB_per_s
101}
102
103probe_fs() {
104 # Filesystem type: GNU stat -f -c %T, BSD stat -f %T, else null.
105 local fstype=null
106 if stat -f -c %T "$PROBE_DIR" >/dev/null 2>&1; then
107 fstype='"'$(stat -f -c %T "$PROBE_DIR" 2>/dev/null)'"'
108 elif stat -f %T "$PROBE_DIR" >/dev/null 2>&1; then
109 fstype='"'$(stat -f %T "$PROBE_DIR" 2>/dev/null | tr -d '"')'"'
110 fi
111 bench_metric probe-fs-type "$fstype" fstype
112
113 # Free space in MB via df -k (POSIX), else null.
114 if command -v df >/dev/null 2>&1; then
115 local free_kb
116 free_kb=$(df -Pk "$PROBE_DIR" 2>/dev/null | awk 'NR==2 {print $4}') || free_kb=''
117 case $free_kb in
118 ''|*[!0-9]*) bench_metric probe-fs-free null MB ;;
119 *) bench_metric probe-fs-free "$((free_kb / 1024))" MB ;;
120 esac
121 else
122 bench_metric probe-fs-free null MB
123 fi
124}
125
126probe_cpu
127probe_write
128probe_read
129rm -f "$PROBE_BIN" 2>/dev/null || true
130probe_fs
131
132exit 0