From 200076ebc6b3f156e811e7c0c3aa9d06760e6c0f Mon Sep 17 00:00:00 2001 From: gccody <58263011+gccody@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:39:25 -0500 Subject: [PATCH] Halve the server tests' time: keep their data in memory, and compile Argon2 optimized A server test starts a server of its own, on SQLite and again on PostgreSQL, creates the first administrator and tears it all down. Timing one simple test's steps showed that nearly all of its 1.85 s was that, not the test: - The server syncs its key file, its SQLite database and the checkpoint at close to the disk: about 80 ms, 255 ms and 245 ms on a real disk, and under 4 ms together in memory. - Argon2 runs unoptimized in test builds: about a third of a second for each password hashed. So: - CI runs the server's tests with TMPDIR=/dev/shm, and keeps its PostgreSQL service's data on a tmpfs. - scripts/test-server-postgres.sh uses /dev/shm for its cluster and the tests' data where there is one, unless TMPDIR is set. - The dev profile compiles argon2 and blake2 at opt-level 3. Only the server depends on them. - docs/development.md says how to run the tests that way. Nothing in the server or its tests changes. --- .github/workflows/ci.yml | 8 ++++++++ Cargo.toml | 8 ++++++++ docs/development.md | 8 ++++++++ scripts/test-server-postgres.sh | 8 ++++++++ 4 files changed, 32 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 04e17bb7..9691e480 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -157,7 +157,10 @@ jobs: POSTGRES_PASSWORD: postgres ports: - 5432:5432 + # Its data is in memory: every test makes and drops a database, and + # waiting for the disk to sync each one is most of what that costs. options: >- + --tmpfs /var/lib/postgresql/data --health-cmd pg_isready --health-interval 5s --health-timeout 5s @@ -169,7 +172,12 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 - run: cargo clippy --locked -p meshrmm-server --all-targets -- -D warnings + # The tests' data directories and SQLite databases are in memory too: + # the server syncs its key, its database and each checkpoint to the + # disk, which on a real disk takes longer than the tests themselves. - run: cargo test --locked -p meshrmm-server + env: + TMPDIR: /dev/shm - run: python3 -m unittest discover -s scripts/tests -v - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 with: diff --git a/Cargo.toml b/Cargo.toml index 9e8e31ce..fc832d25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,6 +63,14 @@ dbg_macro = "deny" todo = "deny" unimplemented = "deny" +# Unoptimized, Argon2 takes about a third of a second for each password, and +# the server's tests hash one for every account they create. +[profile.dev.package.argon2] +opt-level = 3 + +[profile.dev.package.blake2] +opt-level = 3 + [profile.server-release] inherits = "release" codegen-units = 1 diff --git a/docs/development.md b/docs/development.md index ea624a06..1d9e0dc6 100644 --- a/docs/development.md +++ b/docs/development.md @@ -224,6 +224,14 @@ sh scripts/test-server-postgres.sh # arguments go to cargo test It starts a throwaway PostgreSQL in a temporary directory, which needs only PostgreSQL's `initdb` and `pg_ctl` installed, and removes it afterwards. + +Each test starts a server of its own in a temporary directory, and that +server syncs its key, its database and each checkpoint to the disk. On a real +disk those waits are most of the tests' time, so on Linux keep the +directories in memory: `TMPDIR=/dev/shm cargo test --locked -p +meshrmm-server` takes about half as long. The script above and CI do that +already, and CI keeps its PostgreSQL's data in memory as well. + `tests/schema_parity.rs` checks that the two backends' migrations make the same tables and columns; change both `server/migrations/sqlite` and `server/migrations/postgres` together. diff --git a/scripts/test-server-postgres.sh b/scripts/test-server-postgres.sh index 545428e8..74abbfff 100755 --- a/scripts/test-server-postgres.sh +++ b/scripts/test-server-postgres.sh @@ -8,11 +8,19 @@ # Debian's packages put them. The cluster lives in a temporary directory, # listens only on 127.0.0.1 (port 54329, or MESHRMM_TEST_POSTGRES_PORT) and is # removed when the tests end. +# +# Where the system has /dev/shm (Linux), the cluster and the tests' own data +# are kept there, in memory, unless TMPDIR says where to put them: waiting +# for a real disk to sync them takes longer than the tests themselves. set -eu SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) ROOT_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd) PORT=${MESHRMM_TEST_POSTGRES_PORT:-54329} +if [ -z "${TMPDIR:-}" ] && [ -d /dev/shm ] && [ -w /dev/shm ]; then + TMPDIR=/dev/shm + export TMPDIR +fi if ! command -v initdb >/dev/null 2>&1; then if command -v pg_config >/dev/null 2>&1; then