# CFS performance and load tests (Phase 13) This directory implements the **performance and load-testing** phase of the CFS (Compressed File Store) plan (`develop/plan.md`, *Phase 13*). It is a **harness**, not a regression suite: the scripts here produce a numeric report that a human (or CI job) compares against a baseline. There are no hard assertions, because absolute TPS depends on hardware. ## Layout | File | Purpose | |-------------------------------|----------------------------------------------------| | `cfs_perf_setup.sql` | Creates four identical tables (plain, lz4/1, zstd/1, zstd/3) loaded with the same compressible data. | | `cfs_perf_report.sql` | Per-table logical/physical size, compression ratio, dead space, sidecar sizes. | | `pgbench_readonly.sql` | Custom pgbench script: point lookup by PK. | | `pgbench_readwrite.sql` | Custom pgbench script: UPDATE + SELECT by PK (churn). | | `pgbench_hotupdate.sql` | Custom pgbench script: random UPDATE of an existing row (write-heavy). | | `pgbench_readonly.sh` | **A13.1** read-only harness: TPS/latency matrix over `(table, clients)` on a cache-resident table. | | `pgbench_readwrite.sh` | **A13.1** read-write harness: same matrix, plus dead-space report. | | `pgbench_writeheavy.sh` | **A13.1+** write-cost harness: own cluster with SMALL `shared_buffers`, oversubscribed hot-UPDATE — exposes the CPU cost of CFS compression on the write path. | | `pgbench_coldread.sh` | **A13.1+** read-cost harness: own cluster, oversubscribed random point lookup — exposes the CPU cost of CFS decompression on cache misses. | | `datashape_jsonb.sql` | **A13.2** large JSONB documents. | | `datashape_text.sql` | **A13.2** highly repetitive text (best case). | | `datashape_bytea.sql` | **A13.2** random bytea (incompressible; exercises the "store uncompressed when no gain" path). | | `datashape_toast.sql` | **A13.2** TOAST-heavy (>1 MB) values. | | `datashape_churn.sql` | **A13.2** update-heavy single-page churn (dead-space growth + compaction). | ## A13.1 — pgbench profiles Run the read-only matrix against a throwaway cluster: ```sh PG_BINDIR=/path/to/pg/bin ./pgbench_readonly.sh /tmp/cfsperf ``` Or against an already-running server: ```sh PG_BINDIR=/path/to/pg/bin PGHOST=127.0.0.1 PGPORT=55413 \ SCALE=10000 CLIENTS="1 8 32 64" DURATION=30 ./pgbench_readonly.sh ``` The read-write variant is identical: ```sh PG_BINDIR=/path/to/pg/bin ./pgbench_readwrite.sh /tmp/cfsperf ``` ### What the report contains (acceptance criterion for A13.1) For each `(table, clients)` cell: * **TPS** (`tps = ... excluding connections establishing`), * **p95 / average latency** (`latency average`, in microseconds — pgbench's `-P`/`--report-per-command` can be added for a true percentile if needed), * **CPU%** (best-effort; reported as `-` when not measurable portably). After the matrix, `cfs_perf_report.sql` prints: * logical size, **physical (compressed) size**, **compression ratio**, * `dead_pages` / `dead_bytes`, * main-fork vs. **total including sidecar** sizes (`.cfp` / `.cfsmap` / `.cfsjrn`), via `pg_total_relation_size()`. ### Tuning knobs | Variable | Default | Meaning | |--------------|--------------|-------------------------------------------| | `SCALE` | `10000` | rows loaded into each table | | `PAYLOAD_LEN`| `320` | payload length in chars | | `CLIENTS` | `1 8 32 64` | space-separated client counts | | `DURATION` | `15` | per-run duration in seconds | | `PGPORT` | `55413` | port for the throwaway cluster | > **Codec availability.** lz4/zstd tables require the server to be built with > `--with-lz4` / `--with-zstd`. The setup script errors out cleanly if a codec > is missing. ## A13.2 — data-shape benchmarks Each `datashape_*.sql` is a standalone psql script that builds a plain and a CFS copy of a dataset and prints the compression ratio. Pass the algorithm and (optionally) the row count: ```sh psql -v algo=lz4 -v rows=2000 -f datashape_jsonb.sql psql -v algo=zstd -v rows=5000 -f datashape_text.sql psql -v algo=lz4 -v rows=200 -f datashape_churn.sql ``` What to look for: * **jsonb / text** — high ratio (the codecs' happy path). * **bytea** — ratio near 1.0; proves the `min_gain_bytes` "store uncompressed" guard does not *inflate* incompressible pages. * **toast** — the ratio is reported for the TOAST relation only, since the main fork holds pointers; TOAST inherits the table's policy (A5.4). * **churn** — `dead_bytes` / `dead_pages` rise after the UPDATE loop and drop after `pg_cfs_compact()`; the basis for the background compactor (A8.5). ## A13.1+ — write-cost and read-cost harnesses (exposing the real CFS overhead) The `pgbench_readonly.sh` / `pgbench_readwrite.sh` matrices run against a **cache-resident** table (the table fits in `shared_buffers`), so they measure the *SQL path* overhead of CFS, not the codec. On a warm cache CFS does nothing (pages are already decompressed in RAM) and the numbers come out within noise of plain — which is the intended zero-overhead guarantee but is not very informative if you want to see what compression *costs*. The two scripts below deliberately defeat that by starting a **throwaway cluster with a small `shared_buffers`** and loading a table that is several times larger than the cache. They always bring up their own cluster (argument `PGDATA` is not needed), because oversubscription is the whole point. ### `pgbench_writeheavy.sh` — CPU cost of compression on the write path ```sh PG_BINDIR=/path/to/pg/bin ./pgbench_writeheavy.sh ``` Runs a random-UPDATE workload (`pgbench_hotupdate.sql`) against an oversubscribed table for plain / lz4 / zstd. Every dirty page evicted from the small cache must be compressed and appended to `.cfp`, so the per-table TPS drop vs plain is the **compression CPU cost**. Report columns: * `TPS`, `lat_avg_ms` — per `(table, clients)`. * `overhead_vs_plain` — `(cfs_tps / plain_tps − 1) × 100` **at the same client count** (positive = CFS faster, negative = CFS slower). * After the matrix: `dead_pages` / `dead_bytes` — a non-zero value is the proof the CFS write path actually ran (on a cache-resident table it stays at 0). Typical reading on the reference box (8 cores, `shared_buffers=16MB`, scale=20000): lz4 within ±2% of plain, zstd −4…−6% (compression CPU). When I/O is the bottleneck (very large, very compressible table), CFS can even come out *faster* than plain because it writes fewer bytes to disk. ### `pgbench_coldread.sh` — CPU cost of decompression on cache misses ```sh PG_BINDIR=/path/to/pg/bin ./pgbench_coldread.sh ``` Random point lookups (`pgbench_readonly.sql`) against an oversubscribed table. Every cache miss has to read the compressed record from `.cfp` and decompress it, so the per-table TPS drop vs plain is the **decompression CPU cost**. Report columns mirror the write harness. `decompress_count` in the accounting block is non-zero iff decompression actually ran. Typical reading: lz4/zstd decompression is cheap enough that the I/O savings (smaller compressed records) often cancel the CPU cost, so CFS lands within ±3% of plain — but on a CPU-bound box the −3…−5% decompression overhead is visible. ### Tuning knobs (both harnesses) | Variable | Default | Meaning | |--------------|--------------|-------------------------------------------| | `SCALE` | `20000` (write) / `300000` (read) | rows loaded into each table | | `PAYLOAD_LEN`| `400` | payload length in chars | | `CLIENTS` | `1 8` (write) / `1 8 32` (read) | space-separated client counts | | `DURATION` | `20` | per-run duration in seconds | | `BUF_KB` | `16` | `shared_buffers` for the throwaway cluster (MB) | | `LICENSE_FILE` | `/tmp/license.xml` | license file for `initdb` (this fork) | > If you raise `SCALE` aggressively (e.g. 50 000+) with many clients, the > background MVCC churn can grow the heap to tens of thousands of pages; in > that regime a rare residual extension race may still surface as > `unexpected data beyond EOF` (the matrix reports it as a `FAILED` row instead > of aborting). The defaults above avoid it. ## Relationship to the regression suite Concurrency, crash-injection and SQL-contract scenarios that *do* have hard assertions live with the rest of the test tree (the former `066_*`/`067_*` recovery tests were consolidated into the `500_*` series): * `src/test/recovery/t/501_cfs_crash.pl` — crash recovery, checkpoint vs concurrent flush, restart-point races (A13.1a). * `src/test/recovery/t/504_cfs_concurrency.pl` — concurrent extension and same-block rewrites, read/write coordination, a live cursor across `ALTER TABLE SET (algorithm)` (A13.3). * `src/test/recovery/t/503_cfs_compaction.pl` — `pg_cfs_compact` vs a concurrent checkpoint (quiesce), interrupted-swap recovery, the automatic CfsBgWorker (A13.3 / A13.4). * `src/test/isolation/specs/cfs-cluster-conflict.spec` — a full rewrite (`CLUSTER`, the same atomic-swap path compaction uses) serialized through the lock manager on a CFS table (A13.3). The kill -9/restartpoint-under-load scenarios of the former `067_cfs_perf_recovery.pl` were not ported into the `500_*` series and have no scripted equivalent today; this harness covers only their load-generation half, without the crash injection.