diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc index 459853c..0598725 100644 --- a/docs/modules/ROOT/nav.adoc +++ b/docs/modules/ROOT/nav.adoc @@ -21,3 +21,4 @@ * Development ** xref:development/ci.adoc[Continuous integration] ** xref:development/release.adoc[Release] +** xref:development/release-native-smoke.adoc[Speed smoke of the native wheel] diff --git a/docs/modules/ROOT/pages/development/release-native-smoke.adoc b/docs/modules/ROOT/pages/development/release-native-smoke.adoc new file mode 100644 index 0000000..cf3b0f9 --- /dev/null +++ b/docs/modules/ROOT/pages/development/release-native-smoke.adoc @@ -0,0 +1,181 @@ += Speed smoke of the native wheel +:description: The release step that measures the native wheel before it is published: the single-record UPDATE against the previous release, on a host of the class that measures the published numbers. + +Two native wheels built from one commit can differ in speed by about a third on the single-record `UPDATE` (https://github.com/ArcadeData/arcadedb-python/issues/404[#404]). A GraalVM native-image build is not reproducible, so the speed of one hot path is a draw from a spread. The fix is in the release, not in the sources: measure the wheel that would be published, and publish it only when it passes. `scripts/native_update_smoke.py` is that measurement. This page is the runbook. + +== What it measures + +On the host where you run it, in one session, the tool installs each wheel into its own throw-away environment (`uv run --no-project --with `) and starts one fresh process per wheel per round. A process runs the state of the paper cell on synthetic rows: the async executor, the parallel columnar load of `LineItem` (300,000 rows), the `Part` load (200,000 rows), the indexes, the warm-up, and then the single-record phases, each operation in its own `begin` and `commit` pair. The figure that decides is the median latency per operation of + +[source,sql] +---- +UPDATE Crud SET qty = 2 WHERE ckey=:c +---- + +with a parameter, through `command()`. The tool records the other figures too (new-order, payment, insert, read, delete) as information. + +* The wheels run in an interleaved order. Round `2j` and round `2j+1` run them in mirrored order (ABBA), and the order rotates every two rounds, so no wheel always runs first. +* The ratio of a candidate is the median over the rounds of the per-round ratio against the baseline, the previously published wheel. Below 1 is faster. The interval is a 95% percentile bootstrap of that median. +* The control is the baseline wheel against itself, in its own processes, in the same session. Its interval must lie inside 0.97 to 1.03. If it does not, the session is not readable: the host was not quiet, and no verdict counts. +* A candidate passes when the upper end of its interval is below `--max-ratio` (default 1.10). The spread study of #404 found slow builds at 1.33 to 1.35 and fast ones at 0.95 to 0.99 on the host class that measures the published numbers. +* The tool reads `Cpus_allowed_list` from `/proc/self/status`, records it, and refuses to run with more than 4 allowed cores unless `--quick`. It does not pin anything itself: the caller pins it, and every process of the session inherits the set and checks it. +* Each process hashes the library it loaded and compares it with the `libarcadedb.so` member of its wheel. A mismatch stops the session. + +== When to run it + +Run it for every final or `postN` release of the native package (a `devN` release does not need it), after `release-native.yml` has built and tested the wheels, and before anyone approves the publish job. The GitHub environment `pypi` has a required reviewer (the owner), so `Publish to PyPI` waits after `Verify what may be published` has finished. The artifact `native-release-dist` of that run holds the wheels that the publish job will upload, so the wheel you measure is the wheel that goes to PyPI. + +. Push the tag as in xref:development/release.adoc#_release_day[Release day]. Wait until `Verify what may be published` has finished and `Publish to PyPI` waits for review. +. Run the smoke on the Linux x86-64 wheel of that run (this page). +. When a candidate passes, approve the deployment. Record the result (<<_release_notes,below>>). +. When none passes, follow <<_when_none_passes,When no candidate passes>>. + +The smoke covers the `manylinux_2_28_x86_64` wheel only. The other platforms are not measured. + +== Get the files + +The candidate is in the artifact of the release run. The previous release is on PyPI. Use a folder on a disk, not on a tmpfs: the wheels are about 66 MB each. + +[source,bash] +---- +# the candidate: the Linux x86-64 wheel of the release run (run ids: gh run list -R ArcadeData/arcadedb-python --branch ) +gh run download -R ArcadeData/arcadedb-python -n native-release-dist -D smoke/cand1 + +# the baseline: the previous release of `arcadedb` (not `arcadedb-jvm`), with the sha256 that PyPI lists +mkdir -p smoke +curl -fsS https://pypi.org/pypi/arcadedb//json | uv run --no-project python -I -c ' +import json, sys +f = next(f for f in json.load(sys.stdin)["urls"] if f["filename"].endswith("manylinux_2_28_x86_64.whl")) +print(f["url"], f["digests"]["sha256"], f["filename"])' > smoke/base.txt +read -r url sha name < smoke/base.txt +curl -fsSL --create-dirs --output-dir smoke/base -o "$name" "$url" +echo "$sha smoke/base/$name" | sha256sum -c - +---- + +== Run it + +Run it with the cores of one compare lane: the cores that one lane of the host uses for a timed study, at most four, and no other work on them. Pin the command; the tool and all its processes inherit the set. Name the baseline with `--baseline` and the candidates as arguments. + +[source,bash] +---- +cores= + +# a try first: small scale, few rounds, any number of cores. It checks the tool and the environments. It is never a result. +taskset -c "$cores" uv run --no-project python -I scripts/native_update_smoke.py --quick \ + --baseline smoke/base/ --out smoke/quick.json smoke/cand1/ + +# the smoke +taskset -c "$cores" uv run --no-project python -I scripts/native_update_smoke.py \ + --baseline smoke/base/ --out smoke/-attempt1.json \ + --work-dir smoke/cand1/ +---- + +Several candidates can go into one session (`smoke/cand1/... smoke/cand2/...`), so they are measured against the same baseline in the same interleaved rounds. The tool refuses two wheels that hold the same library (same sha256). Useful options: + +[cols="2,4"] +|=== +| Option | Meaning + +| `--rounds N` +| Measured rounds, even, at least 8 (default 12). The warm-up rounds (`--warmup-rounds`, default 1) run first and are never used. + +| `--max-ratio R` +| The pass threshold on the upper end of the interval (default 1.10). + +| `--isolate-args "..."` +| `ARCADEDB_NATIVE_ISOLATE_ARGS` of every process. The default, `-Xmx8g -Darcadedb.txWalFlush=0`, is the heap of the cell at scale factor 1 and its relaxed WAL flush. A different setting changes the cost of a commit. + +| `--python X`, `--numpy SPEC`, `--offline` +| The interpreter and the numpy requirement of the environments, and `--offline` for uv. The tool records the versions it got. + +| `--out FILE` +| The JSON report (every process, the schedule, every ratio and interval, the host, the allowed cores, the sha256 of each wheel and library). The default is `native-update-smoke-.json`. + +| `--hash-only WHEEL ...` +| Print the library sha256 of each wheel and stop. It is how you check a published wheel afterwards. +|=== + +== Read the result + +The tool prints one line per candidate and the control: + +---- +control (baseline against itself): 1.001 [0.996, 1.007] readable + +candidate ratio 95% interval verdict engine library sha256 +cand1 0.985 [0.977, 0.991] PASS 26.12.1 <64 hex digits> +---- + +[cols="1,4"] +|=== +| Exit status | Meaning + +| 0 +| At least one candidate passed. Publish the first one that passed. + +| 1 +| No candidate passed. See <<_when_none_passes,When no candidate passes>>. + +| 2 +| The run was refused or a process failed: no baseline or candidate, an odd or small `--rounds`, more than 4 allowed cores without `--quick`, a wheel that does not load, a library that is not the wheel's, a process that timed out. The message says which. Nothing was measured that you can use. + +| 3 +| The control is not inside 0.97 to 1.03. The session is not readable, whatever the candidates read. Run it again on a quiet host, on the cores of the lane. +|=== + +A `--quick` run prints a banner and records `"quick": true` in the JSON. Never record it as the smoke of a release. + +[[_when_none_passes]] +== When no candidate passes + +A build that fails is a bad draw, not a bug to chase: the same commit, built again, reads differently. The release builds the wheel again until one passes, up to K times in total. In practice K is up to 4. + +. Reject the deployment of `Publish to PyPI` on the run page (nothing is uploaded), then re-run all jobs of the run. The workflow builds all wheels again. Nothing of this version is on PyPI, so `Tag must be the next version` passes again. +. When `Verify what may be published` has finished, download `native-release-dist` of the new attempt into a new folder (`smoke/cand2`) and run the smoke again. Keep the JSON of every attempt. +. Stop at the first pass and approve the deployment. +. When K builds have all failed, stop. The tool does not decide what happens next: that is the owner's call, with the JSON of every attempt in front of them. + +Each attempt is a full release run of all four platforms on Python 3.11 to 3.14, on the runner limit that the organisation shares with upstream ArcadeDB. Do not start one without a reason. A control that reads outside 0.97 to 1.03 (exit status 3) is not a failed build: run the same wheels again. + +[[_release_notes]] +== What goes into the release notes + +Record, for the wheel that is published: + +* The sha256 of its `libarcadedb.so` (the `library sha256` column, or `--hash-only`). The MMDBBench rows of the release name this hash too. +* The ratio and its interval, the control, the baseline version, the number of rounds, which attempt of K it was, and the host class. + +`release-notes.yml` creates the GitHub Release from `scripts/release_notes.py`, and it rewrites the notes whenever it runs again. Add the lines to the finished release, and again after a re-run of that workflow: + +[source,bash] +---- +gh release view -R ArcadeData/arcadedb-python --json body -q .body > notes.md +cat >> notes.md <<'EOF' + +## Native wheel speed smoke + +- Library sha256 (`libarcadedb.so`, linux x86-64): `` +- Single-record `UPDATE` through `command()` against ``: `` `[, ]` (95% interval of the median of `` per-round ratios, below 1 is faster); control `` `[, ]` +- Attempt `` of at most 4, host class ``, `scripts/native_update_smoke.py`, #404 +EOF +gh release edit -R ArcadeData/arcadedb-python --notes-file notes.md +---- + +After the publish job has run, check that the wheel on PyPI is the one you measured. Download it as in "Get the files" and compare the library hash with the recorded one: + +[source,bash] +---- +uv run --no-project python -I scripts/native_update_smoke.py --hash-only +---- + +== What it leaves out of the cell + +The smoke is a release guard, not a replacement for the paper cell. It leaves out: + +* Real TPC-H data. The rows are synthetic, of the same columns and types: `LineItem` has a tenth of the rows of scale factor 1 and `Part` has the scale-1 count. The keys come from a seeded generator of the script, not from the streams of the cell. +* The harness around the cell: its container, its memory sampler, its other phases and queries. +* The host class. The smoke says nothing about a host of another class. The spread study of #404 measured a build as slow on P cores and not slow on E cores, so a pass is a pass on the class of host where you ran it. +* Other figures. Only the `UPDATE` decides. The other figures are in the JSON as information. +* Other wheels. Only the Linux x86-64 wheel is measured. + +The tool is `scripts/native_update_smoke.py`. Its tests are in `tests/test_native_update_smoke.py`: they cover the arithmetic, the schedule, the cores, the verdict and the exit status, and the hash of the library of a wheel, and they need no JVM, no native library and no real wheel. diff --git a/docs/modules/ROOT/pages/development/release.adoc b/docs/modules/ROOT/pages/development/release.adoc index 652e538..ce59e6b 100644 --- a/docs/modules/ROOT/pages/development/release.adoc +++ b/docs/modules/ROOT/pages/development/release.adoc @@ -113,6 +113,7 @@ The version rule above holds for both packages. One tag starts both release work ** The repository variable `ARCADEDB_NATIVE_READINESS_BAR_MET` is `true`. A maintainer sets it after checking the readiness bar: the correctness fixes are merged; the full suite is green on every platform, Windows included, and on Python 3.11 to 3.14; the fault and concurrency probes pass; and the documented API raises no `NotOnNativeYet`. ** The parity run of every platform and every Python version passes with zero `NotOnNativeYet` skips. * Speed parity with the JVM build is not part of the bar. +* The speed of the single-record `UPDATE` of the wheel that is published is checked by hand before the publish job is approved, because two native builds of one commit can differ by about a third (xref:development/release-native-smoke.adoc[Speed smoke of the native wheel], https://github.com/ArcadeData/arcadedb-python/issues/404[#404]). [[_release_day]] == Release day diff --git a/scripts/native_update_smoke.py b/scripts/native_update_smoke.py new file mode 100644 index 0000000..bb71cc3 --- /dev/null +++ b/scripts/native_update_smoke.py @@ -0,0 +1,1304 @@ +#!/usr/bin/env python3 +"""Release-time speed smoke of the native wheel: the single-record UPDATE against the previous release. + +Two native wheels built from ONE commit can differ in speed by about a third on this statement, because a native-image +build is not reproducible and the speed of a hot path is a draw from a spread (ArcadeData/arcadedb-python#404). The release +step is therefore to measure the wheel that would be published, on a host of the class that measures the published numbers, +and to publish it only when it passes. This script is that measurement. The runbook is docs/modules/ROOT/pages/development/ +release-native-smoke.adoc. + +What it measures, on the CURRENT host, in one session: for each candidate wheel and the baseline wheel (the previous release +on PyPI), one fresh process per wheel per round runs the process state of the paper cell on synthetic rows (the async +executor, the parallel columnar load of LineItem, the Part load, the indexes, the OLTP and single-record warm-up), then the +timed single-record phases. The verdict figure is `UPDATE Crud SET qty = 2 WHERE ckey=:c` with a parameter through command() +inside begin and commit: the median latency per operation in microseconds. A round runs every wheel once, in an interleaved +order (ABBA over rounds: a round and the next one run the wheels in mirrored order, and the order rotates every two rounds). +The ratio of a candidate is the median over the rounds of the per-round ratio against the baseline (below 1 is faster), with +a 95% percentile bootstrap interval of that median. A control, the baseline wheel against itself in its own processes of the +same session, shows the noise: its interval must lie inside 0.97 to 1.03, else the session is not readable. + +Each wheel is installed into its own throw-away environment with `uv run --no-project --with ` (the way the test +workflows install a wheel under test), from a private copy named by its sha256, so uv's cache cannot serve another file. +The library the process loads is hashed in the process and compared with the library member of the wheel. + +Left out of the original cell, on purpose: real TPC-H data (the rows are synthetic, of the same columns and types, a tenth of +the LineItem rows of scale factor 1 and the scale-1 count of Part), the harness around the cell (its container, its memory +sampler, its other phases and queries), and the key streams of the cell (the keys are drawn from a seeded generator of this +script). The heap and the one engine setting of the cell are set through ARCADEDB_NATIVE_ISOLATE_ARGS (--isolate-args). + +Usage: + native_update_smoke.py --baseline PREVIOUS.whl CANDIDATE.whl [CANDIDATE.whl ...] [--out FILE] [--rounds 12] + native_update_smoke.py --hash-only WHEEL [WHEEL ...] # the sha256 of the library of each wheel, nothing else + +Run it under the cores of one compare lane (the caller pins the process; the children inherit the set). The script reads +`Cpus_allowed_list` from /proc/self/status, records it, and refuses to run on more than 4 cores unless --quick. --quick is a +short run on the small scale for trying the tool: it is never release evidence. + +Exit status: 0 at least one candidate passed (the upper end of its interval is below --max-ratio, default 1.10), 1 no +candidate passed, 2 the run was refused or a process failed (usage, cores, a wheel that does not load, a library that is not +the wheel's), 3 the control is not inside 0.97 to 1.03, so the session is not readable. + +Tests of the pure parts: tests/test_native_update_smoke.py. Output is ASCII only. +""" + +from __future__ import annotations + +import argparse +import datetime +import hashlib +import json +import math +import os +import platform +import random # nosec B311 - the bootstrap resamples with a seeded generator, nothing secret +import re +import shutil +import signal +import statistics +import subprocess # nosec B404 - runs uv with a fixed argument list, no shell +import sys +import tempfile +import time +import traceback +import zipfile +from pathlib import Path, PurePosixPath +from typing import Callable + +ISSUE = "ArcadeData/arcadedb-python#404" +MAX_CORES = 4 # more allowed cores than this is refused unless --quick +DEFAULT_MAX_RATIO = 1.10 # slow builds read 1.33 to 1.35 and fast ones 0.95 to 0.99 in the spread study of #404 +CONTROL_LOW, CONTROL_HIGH = ( + 0.97, + 1.03, +) # the control (baseline against itself) must lie inside +DEFAULT_ROUNDS, MIN_ROUNDS = 12, 8 +QUICK_ROUNDS, QUICK_MIN_ROUNDS = 4, 2 +CONFIDENCE = 0.95 +DEFAULT_BOOTSTRAP = 10_000 +DEFAULT_SEED = 404 +DEFAULT_PYTHON = "3.12" +# The heap of the paper cell at scale factor 1 and its relaxed WAL flush (the engine setting the cell runs with). +DEFAULT_ISOLATE_ARGS = "-Xmx8g -Darcadedb.txWalFlush=0" +VERDICT_FIGURE = "update" +FIGURES = ("new_order", "payment", "insert", "read", "update", "delete") +LIB_NAMES = ("libarcadedb.so", "libarcadedb.dylib", "libarcadedb.dll") +EXIT_PASS, EXIT_NONE_PASSED, EXIT_REFUSED, EXIT_CONTROL = 0, 1, 2, 3 +RESULT_PREFIX = "SMOKE_RESULT " +SCALES = { + # the counts of the paper cell: LineItem a tenth of scale factor 1, Part the scale-1 count + "cell": { + "li_rows": 300_000, + "part_rows": 200_000, + "warm": 2_000, + "oltp_ops": 1_000, + "ops": 1_000, + }, + "small": { + "li_rows": 20_000, + "part_rows": 2_000, + "warm": 100, + "oltp_ops": 100, + "ops": 200, + }, +} +LI_COLS = ( + "l_orderkey", + "l_partkey", + "l_quantity", + "l_extendedprice", + "l_discount", + "l_returnflag", + "l_linestatus", + "l_shipdate", + "l_shipmode", + "l_tax", +) +LI_TEXT_COLS = ("l_returnflag", "l_linestatus", "l_shipdate", "l_shipmode") +LI_FRAME_ROWS = 122_880 # rows per columnar frame of the load +PART_BATCH = 10_000 + + +class Refused(Exception): + """The run cannot give a reading: exit status 2.""" + + +class ChildFailure(Refused): + """A measurement process failed or did not report what it must.""" + + +# ----- pure parts (tests/test_native_update_smoke.py) ------------------------------------------ + + +def parse_cpu_list(text: str) -> list[int]: + """Parse a kernel CPU list such as `0-3,8,10-11` into sorted core numbers.""" + text = text.strip() + if not text: + raise ValueError("empty CPU list") + cores: set[int] = set() + for part in text.split(","): + m = re.fullmatch(r"(\d+)(?:-(\d+))?", part.strip()) + if m is None: + raise ValueError(f"not a CPU list: {text!r}") + lo = int(m.group(1)) + hi = int(m.group(2)) if m.group(2) is not None else lo + if hi < lo or hi - lo > 65535: + raise ValueError(f"not a CPU list: {text!r}") + cores.update(range(lo, hi + 1)) + return sorted(cores) + + +def read_cpus_allowed(status_text: str) -> tuple[str, list[int]]: + """The `Cpus_allowed_list` line of a /proc//status text: (the text as written, the core numbers).""" + for line in status_text.splitlines(): + if line.startswith("Cpus_allowed_list:"): + text = line.split(":", 1)[1].strip() + return text, parse_cpu_list(text) + raise ValueError("no Cpus_allowed_list line in the status text") + + +def check_cores(cores: list[int], quick: bool) -> str | None: + """None when the run may go on, else the reason it is refused.""" + if not cores: + return "no allowed core was read" + if len(cores) > MAX_CORES and not quick: + return ( + f"{len(cores)} cores are allowed ({','.join(map(str, cores))}), more than {MAX_CORES}: " + "pin the process to the cores of one lane, or pass --quick for a try that is not evidence" + ) + return None + + +def round_order(arms: list[str], k: int) -> list[str]: + """The order of the arms in round k: rotated by k // 2 and reversed on odd rounds, so rounds 2j and 2j+1 mirror each other.""" + n = len(arms) + shift = (k // 2) % n + rotated = arms[shift:] + arms[:shift] + return rotated[::-1] if k % 2 else rotated + + +def build_schedule(arms: list[str], rounds: int, warmup_rounds: int) -> list[dict]: + """The warm-up rounds first (their figures are recorded, never used), then the measured rounds.""" + schedule = [ + {"round": w, "warmup": True, "order": round_order(arms, w)} + for w in range(warmup_rounds) + ] + schedule += [ + {"round": r, "warmup": False, "order": round_order(arms, r)} + for r in range(rounds) + ] + return schedule + + +def round_ratios( + base: dict[int, float], arm: dict[int, float] +) -> list[tuple[int, float]]: + """(round, arm / baseline) for every round both have, in round order.""" + return [(r, arm[r] / base[r]) for r in sorted(arm) if r in base and base[r] > 0] + + +def bootstrap_median_ci( + values: list[float], n_boot: int, seed: int, confidence: float = CONFIDENCE +) -> tuple[float, float]: + """Percentile bootstrap interval of the median: resample the rounds with replacement.""" + n = len(values) + if n < 2: + raise ValueError("a bootstrap needs at least two rounds") + rng = random.Random(seed) # nosec B311 + medians = sorted( + statistics.median([values[rng.randrange(n)] for _ in range(n)]) + for _ in range(n_boot) + ) + lo_i = int(math.floor((1.0 - confidence) / 2.0 * n_boot)) + hi_i = min(n_boot - 1, int(math.floor((1.0 + confidence) / 2.0 * n_boot))) + return medians[lo_i], medians[hi_i] + + +def summarise(values: list[float], n_boot: int, seed: int) -> dict: + lo, hi = bootstrap_median_ci(values, n_boot, seed) + return { + "n": len(values), + "median": statistics.median(values), + "lo": lo, + "hi": hi, + "ratios": list(values), + } + + +def verdict(hi: float, max_ratio: float) -> str: + """PASS when the upper end of the interval is below the threshold.""" + return "PASS" if hi < max_ratio else "FAIL" + + +def control_readable(lo: float, hi: float) -> bool: + return lo >= CONTROL_LOW and hi <= CONTROL_HIGH + + +def exit_status(control_ok: bool, verdicts: list[str]) -> int: + """3 when the session is not readable, else 0 when at least one candidate passed, else 1.""" + if not control_ok: + return EXIT_CONTROL + return EXIT_PASS if any(v == "PASS" for v in verdicts) else EXIT_NONE_PASSED + + +def library_member(names: list[str]) -> str: + """The one member of a wheel that is the native library (`/_native/libarcadedb.*`).""" + found = [ + n + for n in names + if PurePosixPath(n).name in LIB_NAMES + and PurePosixPath(n).parent.name == "_native" + ] + if len(found) != 1: + raise ValueError( + f"expected one native library member in the wheel, found {len(found)}: {found}" + ) + return found[0] + + +def sha256_file(path: Path) -> str: + h = hashlib.sha256() + with open(path, "rb") as f: + for chunk in iter(lambda: f.read(1 << 20), b""): + h.update(chunk) + return h.hexdigest() + + +def hash_wheel_library(wheel: Path) -> dict: + """The library member of a wheel: its name, size and sha256 (read from the zip, nothing is unpacked).""" + try: + zf = zipfile.ZipFile(wheel) + except (zipfile.BadZipFile, OSError) as err: + raise ValueError(f"{wheel} is not a readable wheel: {err}") from err + with zf: + member = library_member(zf.namelist()) + h = hashlib.sha256() + size = 0 + with zf.open(member) as f: + for chunk in iter(lambda: f.read(1 << 20), b""): + h.update(chunk) + size += len(chunk) + return {"member": member, "bytes": size, "sha256": h.hexdigest()} + + +def duplicate_libraries(libs: dict[str, str]) -> tuple[str, str] | None: + """The first two labels that share a library sha256 (the same build twice measures nothing), else None.""" + seen: dict[str, str] = {} + for label, sha in libs.items(): + if sha in seen: + return seen[sha], label + seen[sha] = label + return None + + +def child_command( + uv: str, + wheel: Path, + script: Path, + python: str, + numpy_spec: str, + offline: bool, + worker_args: list[str], +) -> list[str]: + """One measurement process: a throw-away environment of the wheel (and numpy), the interpreter in isolated mode.""" + cmd = [uv, "run", "--no-project", "--python", python] + if offline: + cmd.append("--offline") + cmd += ["--with", str(wheel), "--with", numpy_spec] + cmd += ["python", "-I", str(script), "--worker", *worker_args] + return cmd + + +def child_env(base: dict[str, str], isolate_args: str) -> dict[str, str]: + """The environment of a measurement process: nothing that can swap the library or the backend, the cell's isolate arguments.""" + env = { + k: v + for k, v in base.items() + if k + not in ( + "ARCADEDB_NATIVE_LIB", + "ARCADEDB_NATIVE_LIBDIR", + "ARCADEDB_BACKEND", + "ARCADEDB_JVM_ARGS", + "PYTHONPATH", + ) + } + env["ARCADEDB_NATIVE_ISOLATE_ARGS"] = isolate_args + env["PYTHONDONTWRITEBYTECODE"] = "1" + return env + + +def parse_child_output(stdout: str) -> dict: + """The last `SMOKE_RESULT ` line of a process's stdout.""" + for line in reversed(stdout.splitlines()): + if line.startswith(RESULT_PREFIX): + return json.loads(line[len(RESULT_PREFIX) :]) + raise ValueError("the process printed no result line") + + +def validate_child_result( + result: dict, expected_lib: str, expected_cpus: str | None +) -> list[str]: + """What is wrong with a process's report (empty: nothing). The arm must be what the file says it is.""" + problems = [] + if not result.get("ok"): + problems.append( + "the process reported a failure: " + + str(result.get("error") or result.get("problems") or "no reason given") + ) + if result.get("backend") != "native": + problems.append(f"backend is {result.get('backend')!r}, wanted 'native'") + if result.get("lib_sha256") != expected_lib: + problems.append( + f"the library it loaded has sha256 {result.get('lib_sha256')}, the wheel's is {expected_lib}" + ) + if expected_cpus is not None and result.get("cpus_allowed") != expected_cpus: + problems.append( + f"it ran on cores {result.get('cpus_allowed')!r}, the session on {expected_cpus!r}" + ) + figures = result.get("figures") or {} + for name in FIGURES: + v = figures.get(name) + if not isinstance(v, (int, float)) or not math.isfinite(v) or v <= 0: + problems.append(f"figure {name} is {v!r}") + return problems + + +def figures_by_arm(runs: list[dict]) -> dict[str, dict[str, dict[int, float]]]: + """{arm: {figure: {round: microseconds}}} over the measured (not warm-up) rounds.""" + out: dict[str, dict[str, dict[int, float]]] = {} + for run in runs: + if run["warmup"]: + continue + figs = run["result"]["figures"] + for name in FIGURES: + out.setdefault(run["arm"], {}).setdefault(name, {})[run["round"]] = float( + figs[name] + ) + return out + + +def analyse( + runs: list[dict], + candidates: list[str], + max_ratio: float, + n_boot: int, + seed: int, + baseline: str = "baseline", + control: str = "control", +) -> dict: + """Ratios, intervals and verdicts of every candidate and of the control over the measured rounds.""" + by_arm = figures_by_arm(runs) + + def ratios_of(arm: str) -> dict: + out = {} + for name in FIGURES: + pairs = round_ratios(by_arm[baseline][name], by_arm[arm][name]) + out[name] = summarise([ratio for _r, ratio in pairs], n_boot, seed) + out[name]["rounds"] = [r for r, _ratio in pairs] + return out + + ctl = ratios_of(control) + ctl_summary = ctl[VERDICT_FIGURE] + result: dict = { + "figure": VERDICT_FIGURE, + "max_ratio": max_ratio, + "control": { + "figures": ctl, + "readable": control_readable(ctl_summary["lo"], ctl_summary["hi"]), + "band": [CONTROL_LOW, CONTROL_HIGH], + }, + "candidates": {}, + } + for label in candidates: + figs = ratios_of(label) + result["candidates"][label] = { + "figures": figs, + "verdict": verdict(figs[VERDICT_FIGURE]["hi"], max_ratio), + } + result["exit_status"] = exit_status( + result["control"]["readable"], + [c["verdict"] for c in result["candidates"].values()], + ) + return result + + +def make_streams( + seed: int, warm: int, oltp_ops: int, ops: int, part_rows: int +) -> dict[str, list[int]]: + """The keys of the run, a pure function of the seed: Part keys of the new-orders and of the single-record rows, and the order + each payment picks (an index into the orders of the warm-up or of the timed phase). + """ + rng = random.Random(seed) # nosec B311 + return { + "part_warm_orders": [rng.randrange(1, part_rows + 1) for _ in range(warm)], + "pick_warm_payments": [rng.randrange(warm) for _ in range(warm)], + "part_warm_crud": [rng.randrange(1, part_rows + 1) for _ in range(warm)], + "part_orders": [rng.randrange(1, part_rows + 1) for _ in range(oltp_ops)], + "pick_payments": [rng.randrange(oltp_ops) for _ in range(oltp_ops)], + "part_crud": [rng.randrange(1, part_rows + 1) for _ in range(ops)], + } + + +def kept_latencies(latencies: list[float], n: int) -> list[float]: + """The first min(20, n // 10) operations of a phase are left out, as the cell leaves them out.""" + return latencies[min(20, n // 10) :] + + +def staged_name(sha256: str, name: str) -> str: + """The private path (relative to the work folder) of a copy of a wheel, named by the content so uv's cache cannot mix files.""" + return f"wheels/{sha256[:16]}/{name}" + + +def format_report(report: dict) -> list[str]: + """The text a person reads, ASCII only.""" + a = report["analysis"] + cpus = report["cpus_allowed"] + lines = [] + if report["quick"]: + lines.append( + "QUICK RUN on the small scale: a try of the tool, NOT release evidence." + ) + lines.append( + f"native update smoke: {report['rounds']} rounds (+{report['warmup_rounds']} warm-up), " + f"cores {cpus['text']} ({cpus['count']}), bootstrap {report['bootstrap']} resamples, seed {report['seed']}" + ) + lines.append( + f"figure: single-record UPDATE through command(), median per operation; ratio against the baseline " + f"{report['wheels']['baseline']['name']} (below 1 is faster); verdict PASS when the upper end is below {a['max_ratio']:.2f}" + ) + ctl = a["control"]["figures"][VERDICT_FIGURE] + lines.append( + f"control (baseline against itself): {ctl['median']:.3f} [{ctl['lo']:.3f}, {ctl['hi']:.3f}] " + + ( + "readable" + if a["control"]["readable"] + else f"NOT READABLE (must lie inside {CONTROL_LOW:g} to {CONTROL_HIGH:g}): " + "the session says nothing, run it again on a quiet host" + ) + ) + lines.append("") + lines.append( + f"{'candidate':<10} {'ratio':>7} {'95% interval':>17} {'verdict':>8} {'engine':<14} library sha256" + ) + for cand in report["wheels"]["candidates"]: + label = cand["label"] + s = a["candidates"][label]["figures"][VERDICT_FIGURE] + v = a["candidates"][label]["verdict"] + if not a["control"]["readable"]: + v += "?" + interval = "[{:.3f}, {:.3f}]".format(s["lo"], s["hi"]) + lines.append( + f"{label:<10} {s['median']:>7.3f} {interval:>17} {v:>8} " + f"{str(cand.get('engine_version')):<14} {cand['library']['sha256']}" + ) + lines.append("") + for cand in report["wheels"]["candidates"]: + lines.append(f"{cand['label']} = {cand['path']}") + base = report["wheels"]["baseline"] + lines.append( + f"baseline = {base['path']} (engine {base.get('engine_version')}, library sha256 {base['library']['sha256']})" + ) + lines.append("") + lines.append("information only (ratio of the median, not part of the verdict):") + for cand in report["wheels"]["candidates"]: + parts = [] + for name in FIGURES: + s = a["candidates"][cand["label"]]["figures"][name] + parts.append(f"{name} {s['median']:.3f}") + lines.append(f" {cand['label']}: " + ", ".join(parts)) + passed = [ + c["label"] + for c in report["wheels"]["candidates"] + if a["candidates"][c["label"]]["verdict"] == "PASS" + ] + lines.append("") + if not a["control"]["readable"]: + lines.append("exit status 3: the control is not readable") + elif passed: + lines.append(f"PASS: {', '.join(passed)} (exit status 0)") + else: + lines.append("FAIL: no candidate passed (exit status 1)") + if report["quick"]: + lines.append( + "QUICK RUN: do not record this result as the speed smoke of a release." + ) + return lines + + +def run_session( + schedule: list[dict], + run_child: Callable[[str, dict], dict], + log: Callable[[str], None] = lambda _line: None, + runs: list[dict] | None = None, +) -> list[dict]: + """Walk the schedule, one process per arm per round. A failure raises and stops the session (the rest would be wasted); + the runs done so far stay in `runs`, which the caller can hand in to keep them.""" + if runs is None: + runs = [] + total = sum(len(entry["order"]) for entry in schedule) + for entry in schedule: + for position, arm in enumerate(entry["order"]): + started = time.monotonic() + result = run_child(arm, entry) + wall = time.monotonic() - started + runs.append( + { + "arm": arm, + "round": entry["round"], + "warmup": entry["warmup"], + "position": position, + "wall_s": round(wall, 3), + "result": result, + } + ) + kind = "warm-up" if entry["warmup"] else "round" + log( + f"[{len(runs)}/{total}] {kind} {entry['round']} {arm}: " + f"update {result['figures'][VERDICT_FIGURE]:.1f} us ({wall:.1f} s)" + ) + return runs + + +# ----- the host ------------------------------------------------------------------------------ + + +def _read_text(path: str) -> str | None: + try: + return Path(path).read_text() + except OSError: + return None + + +def host_facts() -> dict: + cpuinfo = _read_text("/proc/cpuinfo") or "" + model = next( + ( + line.split(":", 1)[1].strip() + for line in cpuinfo.splitlines() + if line.startswith("model name") + ), + None, + ) + return { + "hostname": platform.node(), + "kernel": platform.release(), + "machine": platform.machine(), + "cpu_model": model, + "parent_python": sys.version.split()[0], + } + + +def loadavg() -> str | None: + text = _read_text("/proc/loadavg") + return text.strip() if text else None + + +def now_utc() -> str: + return datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + + +def write_json(path: Path, data: dict) -> None: + tmp = path.with_name(path.name + ".new") + tmp.write_text(json.dumps(data, indent=2) + "\n") + os.replace(tmp, path) + + +# ----- one measurement process (runs inside the throw-away environment of one wheel) ------------ + + +def _worker_synthetic_frames(np, n_rows: int, part_rows: int, seed: int): + """LineItem columns of the cell's schema and types, as numpy arrays: int64 keys, float64 numbers, object arrays of str.""" + g = np.random.default_rng(seed) + days = [ + (datetime.date(1992, 1, 1) + datetime.timedelta(days=d)).isoformat() + for d in range(2526) + ] + dates = np.array(days, dtype=object) + modes = np.array( + ["AIR", "FOB", "MAIL", "RAIL", "REG AIR", "SHIP", "TRUCK"], dtype=object + ) + flags = np.array(["A", "N", "R"], dtype=object) + status = np.array(["F", "O"], dtype=object) + done = 0 + while done < n_rows: + m = min(LI_FRAME_ROWS, n_rows - done) + yield { + "l_orderkey": np.arange(done, done + m, dtype=np.int64) // 4 + 1, + "l_partkey": g.integers(1, part_rows + 1, m, dtype=np.int64), + "l_quantity": g.integers(1, 51, m).astype(np.float64), + "l_extendedprice": np.round(g.uniform(900.0, 105000.0, m), 2), + "l_discount": g.integers(0, 11, m) / 100.0, + "l_returnflag": flags[g.integers(0, 3, m)], + "l_linestatus": status[g.integers(0, 2, m)], + "l_shipdate": dates[g.integers(0, len(dates), m)], + "l_shipmode": modes[g.integers(0, 7, m)], + "l_tax": g.integers(0, 9, m) / 100.0, + } + done += m + + +def _count(db, sql: str) -> int: + return int(db.query("sql", sql).to_list()[0]["n"]) + + +def _build_cell_state(db, sc: dict, seed: int, np) -> dict: + """The process state the cell is in when it starts timing: the async executor, LineItem with one bucket per writer and its + parallel columnar load, the Part load, the schema of the other types, the indexes. + """ + ex = db.async_executor() + writers = ex.get_parallel_level() + ex.set_transaction_sync("no") + sync = ex.get_transaction_sync() + db.command("sql", f"CREATE DOCUMENT TYPE LineItem BUCKETS {writers}") + for col in LI_COLS: + if col in LI_TEXT_COLS: + kind = "STRING" + elif col.endswith("key"): + kind = "LONG" + else: + kind = "DOUBLE" + db.command("sql", f"CREATE PROPERTY LineItem.{col} {kind}") + for ddl in ( + "CREATE DOCUMENT TYPE Part", + "CREATE PROPERTY Part.p_partkey LONG", + "CREATE INDEX ON Part (p_partkey) UNIQUE_HASH", + "CREATE DOCUMENT TYPE OrderNew", + "CREATE PROPERTY OrderNew.okey LONG", + "CREATE INDEX ON OrderNew (okey) UNIQUE_HASH", + "CREATE DOCUMENT TYPE Payment", + "CREATE DOCUMENT TYPE Crud", + "CREATE PROPERTY Crud.ckey LONG", + "CREATE INDEX ON Crud (ckey) UNIQUE_HASH", + ): + db.command("sql", ddl) + columnar = hasattr(db, "insert_columns") + buf: list[dict] = [] + call_rows = writers * ex.get_commit_every() + for cols in _worker_synthetic_frames(np, sc["li_rows"], sc["part_rows"], seed): + if columnar: + db.insert_columns("LineItem", cols, parallel=True) + continue + for row in zip(*[cols[c].tolist() for c in LI_COLS]): + buf.append(dict(zip(LI_COLS, row))) + if len(buf) >= call_rows: + db.insert_many("LineItem", buf, parallel=True) + buf = [] + if buf: + db.insert_many("LineItem", buf, parallel=True) + lineitem = _count(db, "SELECT count(*) AS n FROM LineItem") + if lineitem != sc["li_rows"]: + raise RuntimeError( + f"LineItem holds {lineitem} of {sc['li_rows']} rows: the load lost records" + ) + for start in range(0, sc["part_rows"], PART_BATCH): + end = min(start + PART_BATCH, sc["part_rows"]) + db.insert_many( + "Part", + [ + { + "p_partkey": k, + "p_retailprice": 900.0 + (k % 1000) / 10.0, + "stock": 100, + } + for k in range(start + 1, end + 1) + ], + commit_every=PART_BATCH, + ) + db.command("sql", "CREATE INDEX ON LineItem (l_shipdate) NOTUNIQUE") + indexes = len(db.query("sql", "SELECT FROM schema:indexes").to_list()) + part = _count(db, "SELECT count(*) AS n FROM Part") + if part != sc["part_rows"]: + raise RuntimeError(f"Part holds {part} of {sc['part_rows']} rows") + return { + "lineitem": lineitem, + "part": part, + "writers": writers, + "transaction_sync": sync, + "columnar": "insert_columns" if columnar else "insert_many", + "indexes": indexes, + } + + +class _Operations: + """The six statements of the cell, each in its own begin and commit pair (the read is one query).""" + + def __init__(self, db): + self.db = db + + def new_order(self, okey: int, pkey: int) -> None: + db = self.db + db.begin() + db.query( + "sql", + "SELECT p_retailprice, stock FROM Part WHERE p_partkey=:k", + {"k": pkey}, + ).to_list() + db.command( + "sql", + "INSERT INTO OrderNew SET okey=:o, pkey=:p, qty=1, paid=0", + {"o": okey, "p": pkey}, + ) + db.command( + "sql", "UPDATE Part SET stock = stock - 1 WHERE p_partkey=:k", {"k": pkey} + ) + db.commit() + + def payment(self, okey: int) -> None: + db = self.db + db.begin() + rows = db.query( + "sql", "SELECT pkey, qty FROM OrderNew WHERE okey=:o", {"o": okey} + ).to_list() + db.command("sql", "UPDATE OrderNew SET paid = 1 WHERE okey=:o", {"o": okey}) + db.command( + "sql", + "INSERT INTO Payment SET okey=:o, pkey=:p, amount=1.0", + {"o": okey, "p": (rows[0].get("pkey") if rows else 0)}, + ) + db.commit() + + def insert(self, ckey: int, pkey: int) -> None: + db = self.db + db.begin() + db.command( + "sql", + "INSERT INTO Crud SET ckey=:c, pkey=:p, qty=1, price=9.99", + {"c": ckey, "p": pkey}, + ) + db.commit() + + def read(self, ckey: int): + return self.db.query( + "sql", "SELECT ckey, pkey, qty FROM Crud WHERE ckey=:c", {"c": ckey} + ).to_list() + + def update(self, ckey: int) -> None: + db = self.db + db.begin() + db.command("sql", "UPDATE Crud SET qty = 2 WHERE ckey=:c", {"c": ckey}) + db.commit() + + def delete(self, ckey: int) -> None: + db = self.db + db.begin() + db.command("sql", "DELETE FROM Crud WHERE ckey=:c", {"c": ckey}) + db.commit() + + +def _timed_phase(fn: Callable[[int], object], n: int) -> tuple[float, int, list]: + """Time every operation on its own; the figure is the median of the kept ones, in microseconds.""" + import gc + + gc.collect() + clock = time.perf_counter + latencies = [] + returned = [] + for i in range(n): + t = clock() + r = fn(i) + latencies.append((clock() - t) * 1e6) + returned.append(r) + kept = kept_latencies(latencies, n) + return statistics.median(kept), len(kept), returned + + +def _worker_body(args, result: dict) -> object | None: + import numpy as np + + import arcadedb as a + + result["backend"] = a.__backend__ + result["version"] = a.__version__ + result["engine_version"] = getattr(a, "ENGINE_VERSION", None) + try: + from arcadedb import _build_info + + result["source_commit"] = str(getattr(_build_info, "SOURCE_COMMIT", "unknown")) + except ImportError: + result["source_commit"] = "unknown" + result["python"] = sys.version.split()[0] + result["numpy"] = np.__version__ + result["isolate_args"] = os.environ.get("ARCADEDB_NATIVE_ISOLATE_ARGS", "") + result["cpus_allowed"] = read_cpus_allowed(Path("/proc/self/status").read_text())[0] + result["module_file"] = str(a.__file__) + problems: list[str] = [] + result["problems"] = problems + if a.__backend__ != "native": + problems.append(f"backend {a.__backend__!r}, wanted 'native'") + return None + if args.expect_cpus and result["cpus_allowed"] != args.expect_cpus: + problems.append( + f"allowed cores {result['cpus_allowed']!r}, the session's {args.expect_cpus!r}" + ) + return None + try: + from arcadedb._native import lib as native_lib + + lib_path = Path(native_lib.library_path()) + except Exception: + # an older wheel: the library sits next to the module + lib_path = Path(a.__file__).parent / "_native" / LIB_NAMES[0] + result["lib_path"] = str(lib_path) + result["lib_sha256"] = sha256_file(lib_path) + if args.expect_lib_sha256 and result["lib_sha256"] != args.expect_lib_sha256: + problems.append( + f"the library at {lib_path} is not the wheel's ({result['lib_sha256']} against {args.expect_lib_sha256})" + ) + return None + sc = SCALES[args.scale] + result["scale"] = {"name": args.scale, **sc} + s = make_streams(args.seed, sc["warm"], sc["oltp_ops"], sc["ops"], sc["part_rows"]) + db = a.create_database(os.path.join(args.workdir, "db")) + ops = _Operations(db) + t0 = time.perf_counter() + result["load"] = _build_cell_state(db, sc, args.seed, np) + result["load_s"] = round(time.perf_counter() - t0, 3) + warm, oltp, n = sc["warm"], sc["oltp_ops"], sc["ops"] + # the untimed warm-up of the cell, in its order: new-orders, payments, then the four single-record operations + for i in range(warm): + ops.new_order(i, s["part_warm_orders"][i]) + for j in range(warm): + ops.payment(s["pick_warm_payments"][j]) + for i in range(warm): + ops.insert(i, s["part_warm_crud"][i]) + for i in range(warm): + ops.read(i) + for i in range(warm): + ops.update(i) + for i in range(warm): + ops.delete(i) + left = _count(db, "SELECT count(*) AS n FROM Crud") + if left != 0: + problems.append(f"Crud holds {left} rows after the warm-up, wanted 0") + figures: dict[str, float] = {} + kept: dict[str, int] = {} + checks: dict[str, bool] = {} + figures["new_order"], kept["new_order"], _ = _timed_phase( + lambda i: ops.new_order(warm + i, s["part_orders"][i]), oltp + ) + figures["payment"], kept["payment"], _ = _timed_phase( + lambda j: ops.payment(warm + s["pick_payments"][j]), oltp + ) + figures["insert"], kept["insert"], _ = _timed_phase( + lambda i: ops.insert(warm + i, s["part_crud"][i]), n + ) + checks["insert_rows"] = _count(db, "SELECT count(*) AS n FROM Crud") == n + figures["read"], kept["read"], rows = _timed_phase(lambda i: ops.read(warm + i), n) + checks["read_found_every_row"] = all(len(r) == 1 for r in rows) + figures["update"], kept["update"], _ = _timed_phase( + lambda i: ops.update(warm + i), n + ) + checks["update_changed_every_row"] = ( + _count(db, "SELECT count(*) AS n FROM Crud WHERE qty = 2") == n + ) + figures["delete"], kept["delete"], _ = _timed_phase( + lambda i: ops.delete(warm + i), n + ) + checks["delete_emptied_table"] = _count(db, "SELECT count(*) AS n FROM Crud") == 0 + result["figures"] = figures + result["kept"] = kept + result["checks"] = checks + for name, ok in checks.items(): + if not ok: + problems.append(f"check {name} failed") + return db + + +def worker_main(args) -> int: + """One measurement process: print one `SMOKE_RESULT` line, then leave without tearing the engine down.""" + sys.dont_write_bytecode = True + result: dict = {"ok": False} + db = None + try: + db = _worker_body(args, result) + result["ok"] = not result.get("problems") and "figures" in result + except BaseException as err: + # reported in the result line, the parent decides + result["ok"] = False + result["error"] = f"{type(err).__name__}: {err}"[:500] + traceback.print_exc() + print(RESULT_PREFIX + json.dumps(result), flush=True) + if db is not None: + try: + db.close() + except BaseException as err: # the line is already written + print(f"close failed: {type(err).__name__}: {err}", file=sys.stderr) + os._exit(0 if result["ok"] else 3) + + +# ----- the session (the parent) -------------------------------------------------------------- + + +def describe_wheel(path: Path) -> dict: + return { + "path": str(path), + "name": path.name, + "bytes": path.stat().st_size, + "sha256": sha256_file(path), + "library": hash_wheel_library(path), + } + + +def stage_wheel(info: dict, work: Path) -> Path: + """A private copy of the wheel named by its sha256; the copy is hashed again so the measured bytes are the described ones.""" + dest = work / staged_name(info["sha256"], info["name"]) + dest.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(info["path"], dest) + if sha256_file(dest) != info["sha256"]: + raise Refused( + f"the copy of {info['path']} differs from the file: disk trouble?" + ) + return dest + + +def run_child_process( + cmd: list[str], env: dict[str, str], timeout: int +) -> tuple[int, str, str]: + """Run one process in its own group so a timeout ends uv and the interpreter below it.""" + proc = subprocess.Popen( # nosec B603 - fixed argument list, no shell + cmd, + env=env, + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + start_new_session=True, + ) + try: + out, err = proc.communicate(timeout=timeout) + except subprocess.TimeoutExpired: + os.killpg(proc.pid, signal.SIGKILL) + out, err = proc.communicate() + raise ChildFailure(f"timed out after {timeout} s\n{_tail(err)}") from None + except BaseException: + # an interrupt of the parent: the group is in a session of its own and would run on + os.killpg(proc.pid, signal.SIGKILL) + proc.communicate() + raise + return proc.returncode, out, err + + +def _tail(text: str, lines: int = 15) -> str: + return "\n".join(text.splitlines()[-lines:]) + + +def parse_args(argv: list[str] | None) -> argparse.Namespace: + p = argparse.ArgumentParser( + prog="native_update_smoke.py", + description="Speed smoke of native wheels: the single-record UPDATE against the previous release (#404).", + epilog="Exit status: 0 a candidate passed, 1 none passed, 2 refused or a process failed, 3 the control is not readable.", + ) + p.add_argument("candidates", nargs="*", metavar="CANDIDATE_WHEEL") + p.add_argument( + "--baseline", metavar="WHEEL", help="the previously published native wheel" + ) + p.add_argument( + "--out", + metavar="FILE", + help="the JSON report (default native-update-smoke-.json)", + ) + p.add_argument( + "--rounds", + type=int, + help=f"measured rounds, even (default {DEFAULT_ROUNDS}, quick {QUICK_ROUNDS})", + ) + p.add_argument( + "--warmup-rounds", + type=int, + default=1, + help="rounds run first and never used (default 1)", + ) + p.add_argument( + "--max-ratio", + type=float, + default=DEFAULT_MAX_RATIO, + help="PASS when the upper end of the interval is below this (default 1.10)", + ) + p.add_argument( + "--quick", + action="store_true", + help="small scale, few rounds, any number of cores: a try of the tool, never release evidence", + ) + p.add_argument( + "--hash-only", + action="store_true", + help="print the library sha256 of each wheel and stop", + ) + p.add_argument( + "--python", + default=DEFAULT_PYTHON, + help="interpreter of the environments (default 3.12)", + ) + p.add_argument( + "--numpy", + dest="numpy_spec", + default="numpy", + help="requirement of numpy in the environments (default numpy)", + ) + p.add_argument( + "--offline", + action="store_true", + help="pass --offline to uv (everything must be in its cache)", + ) + p.add_argument( + "--work-dir", + metavar="DIR", + help="where the private copies and the databases go (a disk, not a tmpfs)", + ) + p.add_argument("--keep-work", action="store_true", help="keep the work folder") + p.add_argument( + "--isolate-args", + default=DEFAULT_ISOLATE_ARGS, + help=f"ARCADEDB_NATIVE_ISOLATE_ARGS of every process (default {DEFAULT_ISOLATE_ARGS!r})", + ) + p.add_argument( + "--timeout", type=int, default=1800, help="seconds per process (default 1800)" + ) + p.add_argument( + "--bootstrap", + type=int, + default=DEFAULT_BOOTSTRAP, + help="bootstrap resamples (default 10000)", + ) + p.add_argument( + "--seed", + type=int, + default=DEFAULT_SEED, + help="seed of the bootstrap and of the keys (default 404)", + ) + # one measurement process, started by the parent + p.add_argument("--worker", action="store_true", help=argparse.SUPPRESS) + p.add_argument("--workdir", help=argparse.SUPPRESS) + p.add_argument( + "--scale", choices=sorted(SCALES), default="cell", help=argparse.SUPPRESS + ) + p.add_argument("--expect-lib-sha256", help=argparse.SUPPRESS) + p.add_argument("--expect-cpus", help=argparse.SUPPRESS) + return p.parse_intermixed_args(argv) + + +def hash_only(paths: list[str]) -> int: + if not paths: + print("REFUSED: --hash-only needs at least one wheel", file=sys.stderr) + return EXIT_REFUSED + for name in paths: + try: + lib = hash_wheel_library(Path(name)) + except ValueError as err: + print(f"REFUSED: {err}", file=sys.stderr) + return EXIT_REFUSED + print(f"{lib['sha256']} {lib['member']} {lib['bytes']} bytes {name}") + return EXIT_PASS + + +def check_args(args: argparse.Namespace) -> None: + """Refuse what cannot give a reading, before anything is copied or started.""" + if not args.baseline: + raise Refused("--baseline WHEEL is required") + if not args.candidates: + raise Refused("name at least one candidate wheel") + if args.rounds is None: + args.rounds = QUICK_ROUNDS if args.quick else DEFAULT_ROUNDS + least = QUICK_MIN_ROUNDS if args.quick else MIN_ROUNDS + if args.rounds < least or args.rounds % 2: + raise Refused(f"--rounds must be even and at least {least}, got {args.rounds}") + if args.warmup_rounds < 0: + raise Refused("--warmup-rounds cannot be negative") + if args.max_ratio <= 0 or args.bootstrap < 100 or args.timeout <= 0: + raise Refused( + "--max-ratio, --bootstrap (at least 100) and --timeout must be positive" + ) + for name in [args.baseline, *args.candidates]: + if not Path(name).is_file(): + raise Refused(f"no such wheel file: {name}") + if shutil.which("uv") is None: + raise Refused("uv is not on PATH") + + +def main_run(args: argparse.Namespace) -> int: + check_args(args) + status_text = _read_text("/proc/self/status") + if status_text is None: + raise Refused("cannot read /proc/self/status: this tool measures on Linux") + try: + cpus_text, cpus = read_cpus_allowed(status_text) + except ValueError as err: + raise Refused(f"cannot read the allowed cores: {err}") from err + reason = check_cores(cpus, args.quick) + if reason: + raise Refused(reason) + uv = shutil.which("uv") + assert uv is not None # nosec B101 - check_args found it + scale = "small" if args.quick else "cell" + out_path = Path( + args.out or f"native-update-smoke-{now_utc().replace(':', '')}.json" + ) + candidates = [f"cand{i + 1}" for i in range(len(args.candidates))] + arms = ["baseline", "control", *candidates] + + base_dir = Path(args.work_dir) if args.work_dir else None + if base_dir is not None: + base_dir.mkdir(parents=True, exist_ok=True) + work = Path(tempfile.mkdtemp(prefix="native-update-smoke-", dir=base_dir)) + report: dict = { + "schema": 1, + "tool": "scripts/native_update_smoke.py", + "issue": ISSUE, + "started_utc": now_utc(), + "status": "aborted", + "error": None, + "quick": args.quick, + "figure": VERDICT_FIGURE, + "max_ratio": args.max_ratio, + "control_band": [CONTROL_LOW, CONTROL_HIGH], + "rounds": args.rounds, + "warmup_rounds": args.warmup_rounds, + "bootstrap": args.bootstrap, + "confidence": CONFIDENCE, + "seed": args.seed, + "scale": {"name": scale, **SCALES[scale]}, + "isolate_args": args.isolate_args, + "python": args.python, + "numpy_spec": args.numpy_spec, + "host": {**host_facts(), "loadavg_start": loadavg()}, + "cpus_allowed": { + "text": cpus_text, + "list": cpus, + "count": len(cpus), + "limit": MAX_CORES, + }, + "work_dir": str(work), + } + try: + uv_version = run_child_process([uv, "--version"], dict(os.environ), 60) + report["host"]["uv"] = uv_version[1].strip() + base_info = describe_wheel(Path(args.baseline)) + cand_infos = [] + for label, name in zip(candidates, args.candidates): + info = describe_wheel(Path(name)) + info["label"] = label + cand_infos.append(info) + dup = duplicate_libraries( + {"baseline": base_info["library"]["sha256"]} + | {c["label"]: c["library"]["sha256"] for c in cand_infos} + ) + if dup: + raise Refused( + f"{dup[0]} and {dup[1]} hold the same library (same sha256): the same build twice measures nothing" + ) + report["wheels"] = {"baseline": base_info, "candidates": cand_infos} + staged = {"baseline": stage_wheel(base_info, work)} + staged["control"] = staged["baseline"] + for info in cand_infos: + staged[info["label"]] = stage_wheel(info, work) + lib_of = {"baseline": base_info["library"]["sha256"]} + lib_of["control"] = lib_of["baseline"] + lib_of |= {c["label"]: c["library"]["sha256"] for c in cand_infos} + schedule = build_schedule(arms, args.rounds, args.warmup_rounds) + report["schedule"] = schedule + script = Path(__file__).resolve() + env = child_env(dict(os.environ), args.isolate_args) + engine_of: dict[str, str | None] = {} + + def run_child(arm: str, entry: dict) -> dict: + tag = ("w" if entry["warmup"] else "r") + str(entry["round"]) + run_dir = work / "runs" / f"{arm}-{tag}" + run_dir.mkdir(parents=True) + try: + worker_args = [ + "--workdir", + str(run_dir), + "--scale", + scale, + "--seed", + str(args.seed), + "--expect-lib-sha256", + lib_of[arm], + "--expect-cpus", + cpus_text, + ] + cmd = child_command( + uv, + staged[arm], + script, + args.python, + args.numpy_spec, + args.offline, + worker_args, + ) + code, out, err = run_child_process(cmd, env, args.timeout) + try: + result = parse_child_output(out) + except ValueError as perr: + raise ChildFailure( + f"{arm} {tag}: exit {code}, {perr}\n{_tail(err)}" + ) from perr + problems = validate_child_result(result, lib_of[arm], cpus_text) + if code != 0 or problems: + raise ChildFailure( + f"{arm} {tag}: exit {code}; " + + "; ".join(problems) + + f"\n{_tail(err)}" + ) + first = engine_of.setdefault(arm, result.get("engine_version")) + if result.get("engine_version") != first: + raise ChildFailure( + f"{arm} {tag}: engine version {result.get('engine_version')!r}, earlier {first!r}" + ) + return result + finally: + shutil.rmtree(run_dir, ignore_errors=True) + + runs = report["runs"] = [] + run_session(schedule, run_child, lambda line: print(line, flush=True), runs) + first_results = {} + for run in runs: + first_results.setdefault(run["arm"], run["result"]) + base_info["engine_version"] = first_results["baseline"].get("engine_version") + base_info["version"] = first_results["baseline"].get("version") + base_info["source_commit"] = first_results["baseline"].get("source_commit") + for info in cand_infos: + res = first_results[info["label"]] + info["engine_version"] = res.get("engine_version") + info["version"] = res.get("version") + info["source_commit"] = res.get("source_commit") + report["analysis"] = analyse( + runs, candidates, args.max_ratio, args.bootstrap, args.seed + ) + report["host"]["loadavg_end"] = loadavg() + report["finished_utc"] = now_utc() + report["status"] = "complete" + report["exit_status"] = report["analysis"]["exit_status"] + print() + print("\n".join(format_report(report))) + return report["exit_status"] + except Refused as err: + report["error"] = str(err) + report["exit_status"] = EXIT_REFUSED + raise + finally: + try: + report.setdefault("finished_utc", now_utc()) + write_json(out_path, report) + print(f"report: {out_path}") + except OSError as werr: + print(f"could not write {out_path}: {werr}", file=sys.stderr) + if not args.keep_work: + shutil.rmtree(work, ignore_errors=True) + + +def main(argv: list[str] | None = None) -> int: + args = parse_args(argv) + if args.worker: + return worker_main(args) + if args.hash_only: + return hash_only(([args.baseline] if args.baseline else []) + args.candidates) + try: + return main_run(args) + except Refused as err: + print(f"REFUSED: {err}", file=sys.stderr) + return EXIT_REFUSED + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/test_native_update_smoke.py b/tests/test_native_update_smoke.py new file mode 100644 index 0000000..4394770 --- /dev/null +++ b/tests/test_native_update_smoke.py @@ -0,0 +1,573 @@ +"""The pure parts of scripts/native_update_smoke.py, the release-time speed smoke of the native wheel (#404). + +No JVM, no native library and no real wheel are needed: the ratio and bootstrap arithmetic on synthetic numbers, the ABBA +schedule, the allowed cores read from a text, the verdict and the exit status, the library member hash of a tiny fake wheel, +the command and the environment of a measurement process, the check of what a process reports, and the session loop with a +fake process. Nothing here times anything. +""" + +from __future__ import annotations + +import hashlib +import importlib.util +import json +import sys +import zipfile +from pathlib import Path + +import pytest + +SCRIPT_PATH = Path(__file__).resolve().parents[1] / "scripts" / "native_update_smoke.py" + + +def _load(): + spec = importlib.util.spec_from_file_location("native_update_smoke", SCRIPT_PATH) + module = importlib.util.module_from_spec(spec) + assert spec.loader is not None + sys.modules["native_update_smoke"] = module + spec.loader.exec_module(module) + return module + + +smoke = _load() + +STATUS_TEXT = ( + "Name:\tpython\n" + "Cpus_allowed:\tc0\n" + "Cpus_allowed_list:\t6-7\n" + "Mems_allowed:\t1\n" + "Mems_allowed_list:\t0\n" +) + + +# ----- the allowed cores ----------------------------------------------------------------------- + + +@pytest.mark.parametrize( + "text, want", + [ + ("0-3,8,10-11", [0, 1, 2, 3, 8, 10, 11]), + ("6", [6]), + (" 4-5 \n", [4, 5]), + ("1,1,0-1", [0, 1]), + ("0-15", list(range(16))), + ], +) +def test_parse_cpu_list(text, want): + assert smoke.parse_cpu_list(text) == want + + +@pytest.mark.parametrize("text", ["", " ", "a", "3-1", "1,,2", "0-", "-3", "1;2"]) +def test_parse_cpu_list_refuses(text): + with pytest.raises(ValueError): + smoke.parse_cpu_list(text) + + +def test_read_cpus_allowed_takes_the_list_line_not_the_mask(): + assert smoke.read_cpus_allowed(STATUS_TEXT) == ("6-7", [6, 7]) + + +def test_read_cpus_allowed_without_the_line(): + with pytest.raises(ValueError): + smoke.read_cpus_allowed("Name:\tpython\nCpus_allowed:\tc0\n") + + +def test_check_cores_limit_and_quick(): + four = [4, 5, 6, 7] + five = [3, 4, 5, 6, 7] + assert smoke.check_cores(four, quick=False) is None + assert "5 cores" in smoke.check_cores(five, quick=False) + assert smoke.check_cores(five, quick=True) is None + assert smoke.check_cores([], quick=True) is not None + + +def test_main_refuses_more_than_four_allowed_cores(tmp_path, monkeypatch, capsys): + base, cand = tmp_path / "base.whl", tmp_path / "cand.whl" + base.write_bytes(b"x") + cand.write_bytes(b"y") + eight = "Cpus_allowed_list:\t0-7\n" + monkeypatch.setattr(smoke, "_read_text", lambda _path: eight) + monkeypatch.setattr(smoke.shutil, "which", lambda _name: "/usr/bin/uv") + code = smoke.main(["--baseline", str(base), str(cand)]) + assert code == smoke.EXIT_REFUSED == 2 + assert "8 cores" in capsys.readouterr().err + + +# ----- the schedule ---------------------------------------------------------------------------- + + +def test_round_order_two_arms_is_abba(): + arms = ["A", "B"] + flat = [a for k in range(4) for a in smoke.round_order(arms, k)] + assert flat == ["A", "B", "B", "A", "B", "A", "A", "B"] + + +@pytest.mark.parametrize("n", [2, 3, 4, 6]) +def test_round_order_mirrors_pairs_and_balances_positions(n): + arms = [f"arm{i}" for i in range(n)] + for k in range(0, 4 * n, 2): + assert smoke.round_order(arms, k + 1) == smoke.round_order(arms, k)[::-1] + positions = {a: [] for a in arms} + for k in range(2 * n): + order = smoke.round_order(arms, k) + assert sorted(order) == sorted(arms) + for pos, a in enumerate(order): + positions[a].append(pos) + for a in arms: + # over one full period every arm stood at every position + assert sorted(positions[a]) == sorted(list(range(n)) * 2) + + +def test_build_schedule_warmup_first_and_counts(): + arms = ["baseline", "control", "cand1"] + sched = smoke.build_schedule(arms, rounds=6, warmup_rounds=2) + assert [e["warmup"] for e in sched] == [True, True] + [False] * 6 + assert [e["round"] for e in sched if not e["warmup"]] == list(range(6)) + assert all(sorted(e["order"]) == sorted(arms) for e in sched) + assert smoke.build_schedule(arms, rounds=4, warmup_rounds=0)[0]["warmup"] is False + + +# ----- ratios, bootstrap, verdict -------------------------------------------------------------- + + +def test_round_ratios_pairs_rounds_present_in_both(): + base = {0: 100.0, 1: 200.0, 2: 0.0, 3: 50.0} + arm = {0: 110.0, 1: 100.0, 2: 10.0, 4: 7.0} + assert smoke.round_ratios(base, arm) == [(0, 1.1), (1, 0.5)] + + +def test_bootstrap_of_a_constant_is_the_constant(): + assert smoke.bootstrap_median_ci([1.25] * 10, 500, seed=1) == (1.25, 1.25) + + +def test_bootstrap_is_seeded_and_inside_the_data(): + values = [0.98, 1.0, 1.02, 1.01, 0.99, 1.03, 0.97, 1.0] + first = smoke.bootstrap_median_ci(values, 2000, seed=404) + assert first == smoke.bootstrap_median_ci(values, 2000, seed=404) + lo, hi = first + assert min(values) <= lo <= hi <= max(values) + assert lo <= 1.0 <= hi + + +def test_bootstrap_needs_two_rounds(): + with pytest.raises(ValueError): + smoke.bootstrap_median_ci([1.0], 100, seed=1) + + +def test_summarise_median_and_interval_of_a_slow_build(): + ratios = [1.31, 1.34, 1.33, 1.32, 1.35, 1.33, 1.34, 1.32, 1.33, 1.36] + s = smoke.summarise(ratios, 2000, seed=404) + assert s["n"] == 10 and s["median"] == pytest.approx(1.33) + assert 1.31 <= s["lo"] <= s["median"] <= s["hi"] <= 1.36 + assert smoke.verdict(s["hi"], 1.10) == "FAIL" + + +@pytest.mark.parametrize( + "hi, want", + [(0.95, "PASS"), (1.099, "PASS"), (1.10, "FAIL"), (1.101, "FAIL"), (1.35, "FAIL")], +) +def test_verdict_is_strictly_below_the_threshold(hi, want): + assert smoke.verdict(hi, 1.10) == want + + +@pytest.mark.parametrize( + "lo, hi, want", + [ + (0.99, 1.01, True), + (0.97, 1.03, True), + (0.969, 1.01, False), + (0.99, 1.031, False), + (0.90, 1.10, False), + ], +) +def test_control_band(lo, hi, want): + assert smoke.control_readable(lo, hi) is want + + +@pytest.mark.parametrize( + "control_ok, verdicts, want", + [ + (True, ["PASS"], 0), + (True, ["FAIL", "PASS", "FAIL"], 0), + (True, ["FAIL", "FAIL"], 1), + (True, [], 1), + (False, ["PASS"], 3), + (False, ["FAIL"], 3), + ], +) +def test_exit_status(control_ok, verdicts, want): + assert smoke.exit_status(control_ok, verdicts) == want + + +# ----- the analysis of a whole session, on synthetic runs -------------------------------------- + + +def _runs(factor, rounds=10, warmup=1): + """Synthetic runs of the arms: factor(arm, round) times a level that drifts from round to round, shared by every arm.""" + runs = [] + arms = ["baseline", "control", "fast", "slow"] + for w in range(warmup): + for arm in arms: # absurd warm-up figures: they must never enter a ratio + figs = {n: 9999.0 for n in smoke.FIGURES} + runs.append(_run(arm, w, True, figs)) + for r in range(rounds): + level = 100.0 + (r % 4) * 2.0 + for arm in arms: + value = level * factor(arm, r) + runs.append(_run(arm, r, False, {n: value for n in smoke.FIGURES})) + return runs + + +def _run(arm, rnd, warmup, figures): + return { + "arm": arm, + "round": rnd, + "warmup": warmup, + "position": 0, + "wall_s": 1.0, + "result": {"figures": figures}, + } + + +def _quiet(arm, r): + wiggle = 1.0 + 0.004 * ((r + len(arm)) % 3 - 1) + return {"baseline": 1.0, "control": 1.0, "fast": 0.97, "slow": 1.33}[arm] * wiggle + + +def test_analyse_passes_the_fast_build_and_fails_the_slow_one(): + a = smoke.analyse(_runs(_quiet), ["fast", "slow"], 1.10, 2000, 404) + assert a["control"]["readable"] is True + assert a["candidates"]["fast"]["verdict"] == "PASS" + assert a["candidates"]["slow"]["verdict"] == "FAIL" + fast = a["candidates"]["fast"]["figures"]["update"] + assert fast["n"] == 10 and fast["median"] == pytest.approx(0.97, abs=0.01) + assert a["candidates"]["slow"]["figures"]["update"]["lo"] > 1.30 + assert a["exit_status"] == 0 + + +def test_analyse_exit_one_when_no_candidate_passes(): + a = smoke.analyse(_runs(_quiet), ["slow"], 1.10, 2000, 404) + assert a["exit_status"] == 1 + + +def test_analyse_noisy_control_is_not_readable_whatever_the_verdicts(): + def noisy(arm, r): + swing = 1.0 + (0.08 if r % 2 else -0.08) if arm == "control" else 1.0 + return _quiet(arm, r) * swing + + a = smoke.analyse(_runs(noisy), ["fast", "slow"], 1.10, 2000, 404) + assert a["control"]["readable"] is False + assert a["candidates"]["fast"]["verdict"] == "PASS" + assert a["exit_status"] == 3 + + +def test_analyse_ignores_the_warmup_rounds(): + a = smoke.analyse(_runs(_quiet, warmup=3), ["fast"], 1.10, 500, 404) + assert a["candidates"]["fast"]["figures"]["update"]["n"] == 10 + assert a["exit_status"] == 0 + + +def test_format_report_is_ascii_and_names_the_verdicts(): + runs = _runs(_quiet) + analysis = smoke.analyse(runs, ["fast", "slow"], 1.10, 500, 404) + report = { + "quick": False, + "rounds": 10, + "warmup_rounds": 1, + "bootstrap": 500, + "seed": 404, + "cpus_allowed": {"text": "6-7", "count": 2}, + "analysis": analysis, + "wheels": { + "baseline": { + "name": "arcadedb-26.11.1-py3-none-manylinux_2_28_x86_64.whl", + "path": "/w/base.whl", + "engine_version": "26.11.1", + "library": {"sha256": "b" * 64}, + }, + "candidates": [ + { + "label": "fast", + "path": "/w/fast.whl", + "engine_version": "26.12.1", + "library": {"sha256": "f" * 64}, + }, + { + "label": "slow", + "path": "/w/slow.whl", + "engine_version": "26.12.1", + "library": {"sha256": "5" * 64}, + }, + ], + }, + } + text = "\n".join(smoke.format_report(report)) + assert text.isascii() + assert "f" * 64 in text and "5" * 64 in text and "b" * 64 in text + assert "PASS: fast (exit status 0)" in text + assert "QUICK" not in text + report["quick"] = True + assert "NOT release evidence" in "\n".join(smoke.format_report(report)) + + +# ----- the session loop, with a fake process --------------------------------------------------- + + +def test_run_session_follows_the_schedule_and_keeps_partial_runs_on_failure(): + arms = ["baseline", "control", "cand1"] + sched = smoke.build_schedule(arms, rounds=4, warmup_rounds=1) + calls = [] + + def child(arm, entry): + calls.append((arm, entry["round"], entry["warmup"])) + return {"figures": {"update": 100.0}} + + runs = smoke.run_session(sched, child) + want = [(a, e["round"], e["warmup"]) for e in sched for a in e["order"]] + assert calls == want and len(runs) == 15 + assert [r["position"] for r in runs[:3]] == [0, 1, 2] + + kept, seen = [], [] + + def failing(arm, entry): + seen.append(arm) + if len(seen) == 4: + raise smoke.ChildFailure("no good") + return {"figures": {"update": 1.0}} + + with pytest.raises(smoke.ChildFailure): + smoke.run_session(sched, failing, runs=kept) + assert len(kept) == 3 + assert isinstance(smoke.ChildFailure("x"), smoke.Refused) + + +def test_make_streams_is_a_pure_function_of_the_seed(): + a = smoke.make_streams(404, warm=50, oltp_ops=40, ops=30, part_rows=1000) + assert a == smoke.make_streams(404, 50, 40, 30, 1000) + assert a != smoke.make_streams(405, 50, 40, 30, 1000) + assert [len(a[k]) for k in a] == [50, 50, 50, 40, 40, 30] + assert all( + 1 <= p <= 1000 + for k in ("part_warm_orders", "part_warm_crud", "part_orders", "part_crud") + for p in a[k] + ) + assert all(0 <= p < 50 for p in a["pick_warm_payments"]) + assert all(0 <= p < 40 for p in a["pick_payments"]) + + +@pytest.mark.parametrize("n, dropped", [(1000, 20), (200, 20), (100, 10), (5, 0)]) +def test_kept_latencies_leave_out_the_first_ones(n, dropped): + latencies = list(range(n)) + assert smoke.kept_latencies(latencies, n) == latencies[dropped:] + + +# ----- the library member of a wheel ----------------------------------------------------------- + + +def _fake_wheel(path, lib_bytes=b"library-bytes", extra=()): + with zipfile.ZipFile(path, "w") as zf: + zf.writestr("arcadedb/__init__.py", "x = 1\n") + zf.writestr("arcadedb/_native/libarcadedb.so", lib_bytes) + zf.writestr("arcadedb/_native/libjvector-x86_64.so", b"other library") + zf.writestr("arcadedb-26.12.1.dist-info/METADATA", "Name: arcadedb\n") + for name, data in extra: + zf.writestr(name, data) + return path + + +def test_hash_wheel_library_hashes_the_library_member_only(tmp_path): + lib = b"\x7fELF" + b"a" * 3_000_000 # more than one read chunk + wheel = _fake_wheel(tmp_path / "arcadedb-26.12.1-py3-none-any.whl", lib) + info = smoke.hash_wheel_library(wheel) + assert info == { + "member": "arcadedb/_native/libarcadedb.so", + "bytes": len(lib), + "sha256": hashlib.sha256(lib).hexdigest(), + } + assert ( + smoke.describe_wheel(wheel)["sha256"] + == hashlib.sha256(wheel.read_bytes()).hexdigest() + ) + + +def test_two_wheels_with_different_libraries_differ(tmp_path): + a = smoke.hash_wheel_library(_fake_wheel(tmp_path / "a.whl", b"one")) + b = smoke.hash_wheel_library(_fake_wheel(tmp_path / "b.whl", b"two")) + assert a["sha256"] != b["sha256"] + assert smoke.duplicate_libraries({"x": a["sha256"], "y": b["sha256"]}) is None + assert smoke.duplicate_libraries( + {"x": a["sha256"], "y": b["sha256"], "z": a["sha256"]} + ) == ("x", "z") + + +@pytest.mark.parametrize( + "names, ok", + [ + (["arcadedb/_native/libarcadedb.so"], True), + (["arcadedb/_native/libarcadedb.dylib", "arcadedb/__init__.py"], True), + (["arcadedb/_native/libjvector-x86_64.so"], False), + (["libarcadedb.so"], False), + (["arcadedb/_native/libarcadedb.so", "other/_native/libarcadedb.so"], False), + ([], False), + ], +) +def test_library_member(names, ok): + if ok: + assert smoke.library_member(names) in names + else: + with pytest.raises(ValueError): + smoke.library_member(names) + + +def test_hash_wheel_library_refuses_a_file_that_is_not_a_wheel(tmp_path): + bad = tmp_path / "not-a-wheel.whl" + bad.write_bytes(b"plain text") + with pytest.raises(ValueError): + smoke.hash_wheel_library(bad) + with pytest.raises(ValueError): + smoke.hash_wheel_library(tmp_path / "missing.whl") + + +def test_hash_only_prints_the_hash_and_the_exit_status(tmp_path, capsys): + wheel = _fake_wheel(tmp_path / "w.whl", b"lib") + assert smoke.main(["--hash-only", str(wheel)]) == 0 + out = capsys.readouterr().out + assert hashlib.sha256(b"lib").hexdigest() in out and "libarcadedb.so" in out + assert smoke.main(["--hash-only"]) == 2 + assert smoke.main(["--hash-only", str(tmp_path / "missing.whl")]) == 2 + + +def test_staged_name_is_named_by_the_content(): + sha = "ab" * 32 + assert smoke.staged_name(sha, "w.whl") == f"wheels/{sha[:16]}/w.whl" + + +# ----- the measurement process: command, environment, report ----------------------------------- + + +def test_child_command_uses_a_throwaway_environment_of_the_wheel(): + cmd = smoke.child_command( + "uv", + Path("/w/arcadedb.whl"), + Path("/s/native_update_smoke.py"), + "3.12", + "numpy", + False, + ["--workdir", "/d"], + ) + assert cmd[:5] == ["uv", "run", "--no-project", "--python", "3.12"] + assert "--with" in cmd and cmd[cmd.index("--with") + 1] == str( + Path("/w/arcadedb.whl") + ) + assert "--offline" not in cmd + assert cmd[cmd.index("python") + 1] == "-I" + assert cmd[-3:] == ["--worker", "--workdir", "/d"] + offline = smoke.child_command( + "uv", Path("/w/a.whl"), Path("/s/x.py"), "3.13", "numpy==2.5.4", True, [] + ) + assert "--offline" in offline and "numpy==2.5.4" in offline + + +def test_child_env_removes_what_could_swap_the_library(): + base = { + "PATH": "/bin", + "ARCADEDB_NATIVE_LIB": "/elsewhere/libarcadedb.so", + "ARCADEDB_NATIVE_LIBDIR": "/elsewhere", + "ARCADEDB_BACKEND": "jvm", + "ARCADEDB_JVM_ARGS": "-Xmx1g", + "PYTHONPATH": "/x", + "ARCADEDB_NATIVE_ISOLATE_ARGS": "-Xmx1g", + } + env = smoke.child_env(base, "-Xmx8g -Darcadedb.txWalFlush=0") + assert env["PATH"] == "/bin" + assert env["ARCADEDB_NATIVE_ISOLATE_ARGS"] == "-Xmx8g -Darcadedb.txWalFlush=0" + assert env["PYTHONDONTWRITEBYTECODE"] == "1" + for name in ( + "ARCADEDB_NATIVE_LIB", + "ARCADEDB_NATIVE_LIBDIR", + "ARCADEDB_BACKEND", + "ARCADEDB_JVM_ARGS", + "PYTHONPATH", + ): + assert name not in env + assert "ARCADEDB_NATIVE_LIB" in base # the caller's environment is not touched + + +def _good_result(**over): + result = { + "ok": True, + "backend": "native", + "lib_sha256": "c" * 64, + "cpus_allowed": "6-7", + "figures": {n: 123.0 for n in smoke.FIGURES}, + } + result.update(over) + return result + + +def test_parse_child_output_takes_the_last_result_line(): + first = smoke.RESULT_PREFIX + json.dumps({"n": 1}) + last = smoke.RESULT_PREFIX + json.dumps({"n": 2}) + text = f"engine log line\n{first}\nmore noise\n{last}\ntrailing log\n" + assert smoke.parse_child_output(text) == {"n": 2} + with pytest.raises(ValueError): + smoke.parse_child_output("only logs\n") + + +def test_validate_child_result_accepts_a_good_report(): + assert smoke.validate_child_result(_good_result(), "c" * 64, "6-7") == [] + assert smoke.validate_child_result(_good_result(), "c" * 64, None) == [] + + +@pytest.mark.parametrize( + "over, needle", + [ + ({"ok": False, "error": "boom"}, "boom"), + ({"backend": "jvm"}, "backend"), + ({"lib_sha256": "d" * 64}, "library it loaded"), + ({"cpus_allowed": "0-15"}, "cores"), + ({"figures": {"update": 5.0}}, "figure new_order"), + ({"figures": {n: float("nan") for n in smoke.FIGURES}}, "figure"), + ({"figures": {n: 0.0 for n in smoke.FIGURES}}, "figure"), + ], +) +def test_validate_child_result_names_the_problem(over, needle): + problems = smoke.validate_child_result(_good_result(**over), "c" * 64, "6-7") + assert problems and any(needle in p for p in problems) + + +# ----- the arguments --------------------------------------------------------------------------- + + +def _args(*extra): + return smoke.parse_args(["--baseline", "base.whl", "cand.whl", *extra]) + + +def test_check_args_refuses_what_cannot_read(tmp_path, monkeypatch): + with pytest.raises(smoke.Refused): + smoke.check_args(smoke.parse_args(["cand.whl"])) # no baseline + with pytest.raises(smoke.Refused): + smoke.check_args(smoke.parse_args(["--baseline", "b.whl"])) # no candidate + for bad in ("7", "6", "0", "4"): # odd, or below the minimum of a release run + with pytest.raises(smoke.Refused): + smoke.check_args(_args("--rounds", bad)) + with pytest.raises(smoke.Refused): + smoke.check_args(_args("--rounds", "12")) # the files do not exist + base, cand = tmp_path / "base.whl", tmp_path / "cand.whl" + base.write_bytes(b"x") + cand.write_bytes(b"y") + monkeypatch.setattr(smoke.shutil, "which", lambda _name: "/usr/bin/uv") + args = smoke.parse_args(["--baseline", str(base), str(cand)]) + smoke.check_args(args) + assert args.rounds == smoke.DEFAULT_ROUNDS + quick = smoke.parse_args(["--baseline", str(base), str(cand), "--quick"]) + smoke.check_args(quick) + assert quick.rounds == smoke.QUICK_ROUNDS + monkeypatch.setattr(smoke.shutil, "which", lambda _name: None) + with pytest.raises(smoke.Refused): + smoke.check_args(args) + + +def test_main_without_arguments_is_a_refusal(capsys): + assert smoke.main([]) == smoke.EXIT_REFUSED + assert "REFUSED" in capsys.readouterr().err