Skip to content

Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.

fix(bench): propagate quick measurement failures and retain cleanup ownership (#54)

Afloat311f7dcf8b1a
diff --git a/bench/quick.sh b/bench/quick.sh
index 394742a..23ab43a 100755
--- a/bench/quick.sh
+++ b/bench/quick.sh
@@ -19,9 +19,17 @@ command -v python3 >/dev/null || { echo "python3 is needed" >&2; exit 1; }
PREFIX=${OPENSET_LLM_HOME:-$HOME/.openset-llm}
[ -x "$PREFIX/bin/llama-server" ] || { echo "not installed: run ./install.sh (or ./start) first" >&2; exit 1; }
if curl -sf "http://127.0.0.1:$PORT/health" >/dev/null 2>&1; then echo "port $PORT is in use; pass --port" >&2; exit 1; fi
-LOG=$(mktemp); PID=
-stop() { [ -n "$PID" ] && kill "$PID" 2>/dev/null && wait "$PID" 2>/dev/null; PID=; return 0; }
-trap 'stop; rm -f "$LOG"' EXIT
+LOG=$(mktemp); ROWS=$(mktemp); PID=
+stop() {
+ if [ -n "$PID" ]; then
+ kill "$PID" 2>/dev/null || true
+ wait "$PID" 2>/dev/null || true
+ fi
+ PID=
+}
+trap 'stop; rm -f "$LOG" "$ROWS"' EXIT
+trap 'exit 130' INT
+trap 'exit 143' TERM
arm() { # arm <label> <serve args...>: one server, NP prompts, one result line on stdout
local label=$1; shift
@@ -60,8 +68,13 @@ cores=$(lscpu 2>/dev/null | awk -F: '/^Core\(s\) per socket/ {c=$2} /^Socket\(s\
ram=$(awk '/^MemTotal:/ {printf "%.1f", $2 / 1048576}' /proc/meminfo)
commit=$(git -C "$HERE" rev-parse --short HEAD 2>/dev/null || echo "no git checkout")
echo "measuring: $NP prompts x $NTOK tokens per row (a few minutes)..." >&2
-rows=$(arm "./serve"); [ "$PLAIN" = 1 ] && rows="$rows
-$(arm "./serve --no-spec" --no-spec)"
+# Keep arms in this shell: command substitution disables errexit and hides PID
+# from the EXIT trap, so a failed request could otherwise print a success report.
+arm "./serve" > "$ROWS"
+if [ "$PLAIN" = 1 ]; then
+ arm "./serve --no-spec" --no-spec >> "$ROWS"
+fi
+rows=$(cat "$ROWS")
cat <<OUT
**openset-llm quick check** (\`bench/quick.sh\`, commit $commit, $(date -u +%Y-%m-%d))
diff --git a/bench/test_quick_failures.py b/bench/test_quick_failures.py
new file mode 100644
index 0000000..083e927
--- /dev/null
+++ b/bench/test_quick_failures.py
@@ -0,0 +1,59 @@
+import os
+from pathlib import Path
+import shutil
+import signal
+import subprocess
+import tempfile
+import unittest
+
+
+class QuickBenchmarkTest(unittest.TestCase):
+ def run_quick(self, failure, plain=False):
+ with tempfile.TemporaryDirectory() as directory:
+ root = Path(directory)
+ (root / "bench").mkdir()
+ (root / "commands").mkdir()
+ (root / "home/bin").mkdir(parents=True)
+ shutil.copy(Path(__file__).with_name("quick.sh"), root / "bench/quick.sh")
+
+ def script(path, text):
+ path.write_text("#!/bin/bash\n" + text)
+ path.chmod(0o755)
+
+ script(root / "home/bin/llama-server", "exit 0\n")
+ script(root / "commands/curl", 'if [ ! -e "$TEST_ROOT/probed" ]; then touch "$TEST_ROOT/probed"; exit 1; fi\nwhile [ ! -s "$TEST_ROOT/server.pid" ]; do sleep 0.01; done\nexit 0\n')
+ script(root / "commands/python3", "exit 42\n" if failure else "echo '| synthetic result |'\n")
+ script(root / "commands/lscpu", "printf 'Core(s) per socket: 1\\nSocket(s): 1\\n'\n")
+ script(root / "serve", 'echo $$ > "$TEST_ROOT/server.pid"\necho "serving on test (synthetic mode)"\nexec sleep 60\n')
+ env = dict(os.environ, TEST_ROOT=str(root), TMPDIR=str(root), OPENSET_LLM_HOME=str(root / "home"),
+ PATH=str(root / "commands") + os.pathsep + os.environ["PATH"])
+ proc = subprocess.Popen(["bash", str(root / "bench/quick.sh")] + (["--plain"] if plain else []),
+ env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ text=True, start_new_session=True)
+ try:
+ stdout, stderr = proc.communicate(timeout=10)
+ self.assertTrue((root / "server.pid").exists(), stderr)
+ pid = int((root / "server.pid").read_text())
+ with self.assertRaises(ProcessLookupError):
+ os.kill(pid, 0)
+ return proc.returncode, stdout, stderr
+ finally:
+ try:
+ os.killpg(proc.pid, signal.SIGKILL)
+ except ProcessLookupError:
+ pass
+ proc.wait()
+
+ def test_failed_measurement_fails_without_a_success_report(self):
+ code, stdout, _ = self.run_quick(True)
+ self.assertNotEqual(code, 0)
+ self.assertNotIn("openset-llm quick check", stdout)
+
+ def test_two_successful_arms_keep_both_rows(self):
+ code, stdout, stderr = self.run_quick(False, plain=True)
+ self.assertEqual(code, 0, stderr)
+ self.assertEqual(stdout.count("| synthetic result |"), 2)
+
+
+if __name__ == "__main__":
+ unittest.main()