From b7c5c70dcc007aef8972f24b65fff19534bbd8dc Mon Sep 17 00:00:00 2001 From: jamesrut1996 Date: Fri, 4 Dec 2026 16:57:54 +0000 Subject: [PATCH] bench-461: benchmarking scripts and results --- .gitignore | 1 + README.md | 85 +++++++++++++------ bin/startsecondary.sh | 11 ++- config/benchmarking.env | 14 ++++ config/default.env | 5 +- docs/Benchmarking.md | 138 +++++++++++++++++++++++++++++++ src/benchmarking/baseline.q | 33 ++++++++ src/benchmarking/benchjob.q | 43 ++++++++++ src/benchmarking/benchmarking.sh | 41 +++++++++ src/benchmarking/dropcache.sh | 20 +++++ src/benchmarking/summary.q | 8 ++ src/primary.q | 5 +- 12 files changed, 375 insertions(+), 29 deletions(-) create mode 100644 config/benchmarking.env create mode 100644 docs/Benchmarking.md create mode 100644 src/benchmarking/baseline.q create mode 100644 src/benchmarking/benchjob.q create mode 100644 src/benchmarking/benchmarking.sh create mode 100644 src/benchmarking/dropcache.sh create mode 100644 src/benchmarking/summary.q diff --git a/.gitignore b/.gitignore index f288f38..54bbfd5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ results/ logs/ state/ +bench/ .idea/workspace.xml diff --git a/README.md b/README.md index fa60961..c4b9481 100644 --- a/README.md +++ b/README.md @@ -286,29 +286,71 @@ The integration tests run with `LOCKDOWN=0` (in `config/integration.env`), excep ## Benchmarks -The same work run three ways on homer (64 logical CPUs: 32 physical cores on 2 sockets). Each time is the average of 3 runs per task. - -| | `vwap`, 50 days | `sumtil`, 50 × 50,000,000 | -|---|---|---| -| KDB-X, `-s 0` (one thread) | 3.40s | 4.41s | -| KDB-X, `-s 8` (`peach`) | 0.62s | 0.67s | -| Framework, 8 secondaries, 1 thread each | 1.22s | 2.22s | +The same work run as plain KDB-X with `peach`, and through the framework, on homer (64 logical CPUs: 32 physical cores on 2 sockets). Each time is the average of 3 runs, with no framework memory cap. +- **Warm:** the HDB is already in the page cache. +- **Cold:** the page cache is emptied before each run, so the HDB is read from disk. - **`vwap`:** the VWAP by `date,sym` for each of the HDB's 50 days, one day per task. It reads from disk. -- **`sumtil`:** `sum til 50000000`, 50 times. Pure compute, about 400MB per task. -- **KDB-X:** `raze .task.vwap peach days` and `.task.sumtil peach n`, timed with `.z.P`. +- **`sumtil`:** `sum til 50000000`, 50 times. Pure compute, about 400MB per task, so cold and warm should be the same: a check that emptying the cache only affects disk reads. +- **KDB-X:** each task over its args with `peach`, timed with `.z.p`. - **Framework:** one job of 50 tasks, `finishedtime - submittedtime` from `jobs`. +- **gc:** `-g 0` is q's default (memory given back to the OS later); `-g 1` gives it back as soon as it's freed. For the framework, it's each secondary's `-g`. + +**The main comparisons** (`-g 0`): + +| | `vwap`, warm | `vwap`, cold | `sumtil` | +|---|---|---|---| +| KDB-X, 1 thread | 3.21s | 56.22s | 4.66s | +| KDB-X, `-s 8` (`peach`) | 0.61s | 41.22s | 0.84s | +| KDB-X, `-s 16` (`peach`) | 0.50s | 41.95s | 0.44s | +| Framework, 8 secondaries × 1 thread | 1.15s | 38.40s | 2.24s | +| Framework, 8 secondaries × 8 threads | 0.63s | 29.49s | 0.93s | +| Framework, 8 secondaries × 16 threads | 0.63s | 26.82s | 0.98s | +| Framework, 16 secondaries × 8 threads | 0.64s | 29.89s | 0.94s | + +**All results: KDB-X with `peach`** + +| gc | threads (`-s`) | `vwap` warm | `vwap` cold | `sumtil` warm | `sumtil` cold | +|---|---|---|---|---|---| +| 0 | 1 | 3.21 | 56.22 | 4.66 | 4.73 | +| 0 | 8 | 0.61 | 41.22 | 0.84 | 0.81 | +| 0 | 16 | 0.50 | 41.95 | 0.44 | 0.53 | +| 1 | 1 | 3.27 | 55.63 | 13.85 | 13.88 | +| 1 | 8 | 0.63 | 43.66 | 3.48 | 3.28 | +| 1 | 16 | 0.51 | 41.76 | 2.80 | 2.90 | + +**All results: the framework** + +| gc | secondaries | threads each | `vwap` warm | `vwap` cold | `sumtil` warm | `sumtil` cold | +|---|---|---|---|---|---|---| +| 0 | 1 | 1 | 7.93 | 62.78 | 15.44 | 15.58 | +| 0 | 1 | 8 | 3.57 | 45.59 | 5.46 | 5.53 | +| 0 | 1 | 16 | 3.16 | 36.97 | 4.72 | 4.72 | +| 0 | 8 | 1 | 1.15 | 38.40 | 2.24 | 2.23 | +| 0 | 8 | 8 | 0.63 | 29.49 | 0.93 | 0.98 | +| 0 | 8 | 16 | 0.63 | 26.82 | 0.98 | 0.93 | +| 0 | 16 | 1 | 0.74 | 37.41 | 1.44 | 1.43 | +| 0 | 16 | 8 | 0.64 | 29.89 | 0.94 | 0.91 | +| 0 | 16 | 16 | 0.63 | 27.03 | 0.93 | 0.94 | +| 1 | 1 | 1 | 8.00 | 62.04 | 15.41 | 15.51 | +| 1 | 1 | 8 | 3.19 | 42.18 | 5.19 | 5.21 | +| 1 | 1 | 16 | 2.89 | 36.55 | 4.64 | 4.57 | +| 1 | 8 | 1 | 1.15 | 38.43 | 2.27 | 2.25 | +| 1 | 8 | 8 | 0.63 | 29.07 | 0.98 | 0.98 | +| 1 | 8 | 16 | 0.70 | 26.82 | 1.01 | 0.97 | +| 1 | 16 | 1 | 0.79 | 39.38 | 1.46 | 1.54 | +| 1 | 16 | 8 | 0.65 | 31.40 | 1.15 | 1.10 | +| 1 | 16 | 16 | 0.62 | 29.44 | 1.00 | 1.01 | **What the numbers say** -- **The framework is 2–3 times faster than a single thread.** -- **On small, even tasks like these, `peach` is faster than the framework:** about 2 times for `vwap` and 3.4 times for `sumtil`. Every framework task pays a fixed cost: - - reading memory from `/proc`, and resetting the peak; - - IPC; - - writing its result to disk; - - a `.Q.gc[]` afterwards. -- **The bigger `sumtil` gap is mostly the gc.** Each task gives its 400MB back to the OS, so the next one has to get it again. `peach` threads keep their memory and reuse it. -- **The cost is per task,** so it matters less as tasks get longer. -- **What the extra time buys:** +- **Threads in each secondary matter more than more secondaries.** q runs operations such as `sum`, `til` and grouped aggregations over its own threads, so 8 secondaries with 8 threads each are about twice as fast as with 1 (`vwap` 1.15s to 0.63s, `sumtil` 2.24s to 0.93s). Going past 64 threads in all (homer's 64 logical CPUs) gains nothing: 8×16, 16×8 and 16×16 are no faster than 8×8. On homer, use 8 secondaries with `THREADS=8`. +- **Warm `vwap`: the framework matches `peach -s 8`** (0.63s against 0.61s). Only `peach -s 16` is a little faster (0.50s). +- **Cold `vwap`: the framework is the fastest,** about 1.5 times faster than the best `peach` (26.82s against 41.22s). Reading the HDB from disk takes most of the time cold. `peach` stops improving after 8 threads, while more secondaries and threads keep more of the HDB being read at once. +- **`sumtil`: `peach` is about twice as fast** (0.44s against 0.93s). This is the cost of the gc. Each framework task gives its 400MB back to the OS with `.Q.gc[]`, so the next one has to get it again, while `peach` threads keep their memory. KDB-X with `-g 1`, which also gives memory back straight away, takes 2.80s on 16 threads: 3 times slower than the framework. +- **`-g` makes no difference to the framework,** since it already gives memory back after every task. In KDB-X, `-g 1` makes `sumtil` 3 to 6 times slower, and doesn't change `vwap`. +- **`sumtil`'s cold and warm times match,** so emptying the cache only changed what reads from disk. +- **Every framework task pays a fixed cost:** reading memory from `/proc` and resetting the peak, IPC, writing its result to disk, and a `.Q.gc[]`. On 1 secondary this makes the framework slower than plain KDB-X on 1 thread (`vwap` 7.93s against 3.21s). The cost is per task, so it matters less as tasks get longer. +- **What that cost buys:** - a task killed for memory takes out one secondary, not the whole process; - a hung task is killed at its timeout; - a global memory cap is enforced, and dispatch only sends what fits; @@ -317,12 +359,7 @@ The same work run three ways on homer (64 logical CPUs: 32 physical cores on 2 s With `peach`, one memory kill or one error loses every thread's work. -**To reproduce** -- **Warm the cache first** by running each workload once. A cold `vwap` run takes several seconds, nearly all of it reading the HDB. -- **KDB-X:** `x -s 8` (or `-s 0`), then load `src/approvedtasks.q` and the HDB. -- **Framework:** start the primary, then submit `.primary.newjob[`vwap; days; ()!()]` and `.primary.newjob[`sumtil; 50#50000000; ()!()]` on its console, one job at a time. - -Full steps are in [the demo guide](docs/demo.md#9-benchmarks). +To rerun these benchmarks, see [Benchmarking](docs/benchmarking.md). ## AI usage diff --git a/bin/startsecondary.sh b/bin/startsecondary.sh index f763d25..205dc94 100755 --- a/bin/startsecondary.sh +++ b/bin/startsecondary.sh @@ -14,7 +14,7 @@ if [[ ! "$ID" =~ ^[1-9][0-9]*$ ]]; then exit 1 fi -# set -a exports every variable the file sets, so q can read them with getenv later +# set -a exports every variable the file sets, so q can read them with getenv later (including SEC_CODE, to register) set -a source "$CONFIG" set +a @@ -23,13 +23,18 @@ set +a export QHOME="$KDBX_HOME" THREADS="${THREADS:-1}" +# pass -g only if GC_MODE is set, the same as the primary does for the secondaries it spawns +GCARG="" +if [[ -n "${GC_MODE:-}" ]]; then GCARG="-g $GC_MODE"; fi + # used for creating logs # todo: have it create the absolute path mkdir -p logs LOG="logs/sec$ID.$(date -u +%Y.%m.%dD%H.%M.%S).log" # nohup to run in the background; to run interactively just use x src/secondary.q -id -nohup "$KDBX_HOME/bin/q" src/secondary.q -id "$ID" -s "$THREADS" < /dev/null >> "$LOG" 2>&1 & +# $GCARG is unquoted on purpose: "-g 1" becomes two arguments, and empty adds nothing +nohup "$KDBX_HOME/bin/q" src/secondary.q -id "$ID" -s "$THREADS" $GCARG < /dev/null >> "$LOG" 2>&1 & echo $! > "logs/sec$ID.pid" -echo "startsecondary: id $ID, threads $THREADS, primary port $PRIMARY_PORT, config $CONFIG, pid $!, log $LOG" \ No newline at end of file +echo "startsecondary: id $ID, threads $THREADS, gc ${GC_MODE:-default}, primary port $PRIMARY_PORT, config $CONFIG, pid $!, log $LOG" \ No newline at end of file diff --git a/config/benchmarking.env b/config/benchmarking.env new file mode 100644 index 0000000..b6fd66c --- /dev/null +++ b/config/benchmarking.env @@ -0,0 +1,14 @@ +# benchmark settings, sourced after default.env by src/benchmarking/benchmarking.sh +PRIMARY_PORT=45100 +GLOBAL_MEM_LIMIT= # no framework cap: measure parallel speed, not memory gating +LOCKDOWN=0 # the bench client reads job times with a lambda +TABLE_DIR= # don't save or load jobs between configurations +PURGE_INTERVAL_SECS= +JOBS_MAX_AGE_SECS= +JOBS_MAX_ROWS= +LOG_AGE_DAYS= +LOG_DIR=logs/bench +RESULTS_DIR=$PWD/bench/taskresults +TASK_TIMEOUT_MS=600000 # 10 minutes: a slow cold run on 1 secondary mustn't time out + +HDB_DIR=/home/jrutledge/internalWork/hdb \ No newline at end of file diff --git a/config/default.env b/config/default.env index f6ecaf1..1881551 100644 --- a/config/default.env +++ b/config/default.env @@ -66,4 +66,7 @@ USER_FUNCS=.primary.newjob,.primary.jobpaths,.primary.status,.primary.gettable,. # secondary registration code # in actual prod and client sites they will have a secrets store that this should be stored in -SEC_CODE=1234 \ No newline at end of file +SEC_CODE=1234 + +# garbage collection mode for each secondary (-g): 0 deferred (q's default), 1 immediate; empty means q's default +GC_MODE= \ No newline at end of file diff --git a/docs/Benchmarking.md b/docs/Benchmarking.md new file mode 100644 index 0000000..0aadbd6 --- /dev/null +++ b/docs/Benchmarking.md @@ -0,0 +1,138 @@ +# Benchmarking + +Scripts that time the same work run two ways, plain KDB-X with `peach` and the framework, across a range of settings, so the framework's speed can be compared fairly and the effect of each setting seen on its own. + +All the scripts are in `src/benchmarking/`. Results go to `bench/results.csv`. + +## What it tests + +Each question below is answered by changing one setting while the others stay the same. + +| Question | Setting changed | Values | +|---|---|---| +| How much faster is the framework with more secondaries? | secondaries (`NUM_SECONDARYS`) | 1, 8, 16 | +| Do extra threads help, in `peach` and in each secondary? | threads (`-s`, `THREADS`) | 1, 8, 16 | +| Does garbage collection mode matter? | gc (`-g`, `GC_MODE`) | 0 (deferred, q's default), 1 (immediate) | +| How much of the time is reading from disk? | page cache | cold (emptied before each run), warm (filled by an untimed run first) | +| How does it compare with plain KDB-X? | mode | `kdbx` (one process, `peach`), `framework` | + +Two workloads, the same ones as the README's benchmark table: + +| Task | What it does | Why | +|---|---|---| +| `vwap` | VWAP by `date,sym`, one HDB day per task, over every day in the HDB (50) | reads from disk: shows the effect of the page cache | +| `sumtil` | `sum til 50000000`, 50 times | pure compute, about 400MB per task: shows the cost of each task and of the gc. It reads nothing from disk, so its cold and warm times should match, which makes it a control | + +The full set is: +- **plain KDB-X:** 2 gc modes × 3 thread counts = 6 configurations; +- **the framework:** 2 gc modes × 3 secondary counts × 3 thread counts = 18 configurations. + +Each one runs both tasks, cold and warm, 3 timed runs each. That's 216 timed runs, about 1.5 to 2 hours in all: nearly all of it cold `vwap`, which reads the whole HDB from disk each time (about 40 to 60 seconds a run, against 1 to 3 seconds warm). + +## The files + +| File | What it does | +|---|---| +| `src/benchmarking/benchmarking.sh` | Runs the whole matrix. Loads `config/default.env`, then `config/bench.env`, then runs `baseline.q` once for each KDB-X configuration. For each framework configuration it starts a fresh primary with that many secondaries, threads and gc mode, runs `benchjob.q` against it, waits for the primary to exit, and deletes the task results. | +| `src/benchmarking/baseline.q` | Plain KDB-X, started with the `-s` and `-g` being tested. Loads `src/approvedtasks.q` and the HDB, then runs each task with `peach` over its args and appends one row per timed run to the CSV. | +| `src/benchmarking/benchjob.q` | The framework client. Connects to the running primary, waits for every secondary to register, then submits each task as one job (`.primary.newjob`, which waits until the job is done) and appends one row per timed run. At the end it tells the primary to exit, which stops its secondaries. | +| `src/benchmarking/dropcache.sh` | Empties the page cache before each cold run, so the HDB has to be read from disk again. With passwordless sudo it drops the whole page cache. Otherwise it asks the kernel to drop each HDB file's pages (`posix_fadvise`), which needs no root. `baseline.q` and `benchjob.q` run it with `bash`. | +| `src/benchmarking/summary.q` | Reads the CSV and prints the average time for each configuration, with cold and warm side by side. | +| `config/bench.env` | The benchmark's settings, loaded after `default.env`. See below. | + +### What happens in one framework configuration + +1. `benchmarking.sh` starts `src/primary.q` on `PRIMARY_PORT` (45100), with `NUM_SECONDARYS`, `THREADS` and `GC_MODE` set for this configuration. +2. The primary spawns its secondaries, each with `-s `, and with `-g ` when `GC_MODE` is set. +3. `benchjob.q` waits until every secondary has registered as free. +4. For each task, cold then warm: + - **warm:** one untimed job first, so the HDB pages are cached and the secondaries have run once; + - **3 timed jobs.** Before each cold one, `dropcache.sh` empties the page cache. Each job's two times are appended to the CSV. +5. `benchjob.q` tells the primary to exit. Its `.z.exit` stops the secondaries, then `benchmarking.sh` deletes `bench/taskresults` and moves on to the next configuration. + +The KDB-X runs follow the same pattern without a primary: one q process, started with the `-s` and `-g` being tested, using `peach` over the same args. + +## Settings: `config/bench.env` + +| Setting | Value | Why | +|---|---|---| +| `PRIMARY_PORT` | 45100 | away from a primary you may already have running on 45000 | +| `GLOBAL_MEM_LIMIT` | empty (no cap) | measures parallel speed. Under the 4GB cap, 16 `sumtil` tasks (about 6.4GB) would pause dispatch, so you'd be timing the gating instead | +| `LOCKDOWN` | 0 | `benchjob.q` reads each job's times with a lambda, and stops the primary with `exit` | +| `TABLE_DIR` | empty | jobs aren't saved or loaded, so each configuration starts clean | +| `PURGE_INTERVAL_SECS`, `JOBS_MAX_*`, `LOG_AGE_DAYS` | empty | nothing is deleted part-way through a run | +| `LOG_DIR` | `logs/bench` | keeps the benchmark's logs apart | +| `RESULTS_DIR` | `bench/taskresults` | deleted after each configuration | +| `TASK_TIMEOUT_MS` | 600000 (10 minutes) | a slow cold run on 1 secondary mustn't time out | +| `HDB_DIR` | your HDB | `vwap` needs it; the secondaries and `baseline.q` both load it | + +`benchmarking.sh` sets `NUM_SECONDARYS`, `THREADS` and `GC_MODE` itself for each configuration. + +## Running it + +From the repo's top folder. A quick check first, of one small configuration (about a minute): +```bash +GCS="0" SECS="8" THREADS_LIST="1" CACHES="warm" TASKS="sumtil" RUNS=1 bash src/benchmarking/benchmarking.sh +x src/benchmarking/summary.q +``` + +Then the full matrix, in the background (about 1.5 to 2 hours): +```bash +rm -f bench/results.csv # or keep it: rows are appended +nohup bash src/benchmarking/benchmarking.sh > logs/bench/run.out 2>&1 & +tail -f logs/bench/run.out +x src/benchmarking/summary.q # once it's finished +``` + +Any part of the matrix can be narrowed with these variables: + +| Variable | Default | Example | +|---|---|---| +| `GCS` | `0 1` | `GCS="1"` | +| `SECS` | `1 8 16` | `SECS="8 16"` | +| `THREADS_LIST` | `1 8 16` | `THREADS_LIST="1"` | +| `CACHES` | `cold warm` | `CACHES="warm"` | +| `TASKS` | `vwap sumtil` | `TASKS="vwap"` | +| `RUNS` | `3` | `RUNS=5` | + +To check `-g` reached the secondaries, look at the spawn commands in the primary's log: +```bash +grep "running system command" logs/bench/primary.log | tail +``` + +## Output + +`bench/results.csv` has one row per timed run: + +| Column | What it is | +|---|---| +| `time` | when the run finished | +| `mode` | `kdbx` or `framework` | +| `task` | `vwap` or `sumtil` | +| `gc` | the `-g` setting | +| `secondaries` | how many secondaries (0 for `kdbx`) | +| `threads` | the `-s` setting: `peach`'s threads, or each secondary's | +| `cache` | `cold` or `warm` | +| `run` | 1 to `RUNS` | +| `wallsecs` | what the user waits: from sending the job to getting the reply | +| `jobsecs` | the job's own `finishedtime - submittedtime` (the measure in the README's table). For `kdbx`, the same as `wallsecs` | + +`summary.q` averages `jobsecs` for each configuration, with cold and warm in separate columns. + +## Reading the results + +- **Threads in the framework:** no task calls `peach`. Extra threads in a secondary only help where q spreads a single operation over its threads on its own, such as `sum` or `til` on a large vector. Expect some effect on `sumtil` and little on `vwap`. 16 secondaries with 16 threads each is 256 threads on 64 cores, which is oversubscribed on purpose. +- **gc in the framework:** the secondaries already run `.Q.gc[]` after every task, so `-g 1` only changes when memory is given back *during* a task. Expect a smaller effect than in plain KDB-X. +- **Cold vs warm:** only `vwap` reads from disk. `sumtil`'s cold and warm times should be about the same; if they're not, something else is affecting the runs. +- **Cold without sudo:** dropping each file's pages works on files no process is still mapping. To check a run really was cold, `fincore ` (from util-linux) shows how much of a file is in the cache. +- **The per-task cost:** every framework task pays for memory reads from `/proc`, the IPC, writing its result to disk and a `.Q.gc[]`. That cost is the same whatever the task, so it matters less as tasks get longer. See [Benchmarks](../README.md#benchmarks) in the README. +- **No memory cap:** these numbers show the framework's best case. To see the effect of the cap, run again with `GLOBAL_MEM_LIMIT` set in `bench.env`. + +## If something goes wrong + +| Message | Cause | +|---|---| +| `set HDB_DIR in config/bench.env first` | `HDB_DIR` is empty: `default.env` sets it empty, so it has to be set in `bench.env` | +| `primary didn't start` | see `logs/bench/primary.log`. Usually port 45100 is still in use from an earlier run: `ss -ltnp \| grep 45100` | +| `not every secondary registered` | a secondary failed to start: see `logs/bench/sec*.log` | +| `'.task` in a KDB-X run | `src/approvedtasks.q` names its tasks differently: change `run1` in `baseline.q` to use those names | \ No newline at end of file diff --git a/src/benchmarking/baseline.q b/src/benchmarking/baseline.q new file mode 100644 index 0000000..a497499 --- /dev/null +++ b/src/benchmarking/baseline.q @@ -0,0 +1,33 @@ +/ plain KDB-X: each task over its args with peach, RUNS times, cold and warm, appended to OUT +/ started by src/benchmarking/benchmarking.sh as: q src/benchmarking/baseline.q -s -g -q + +out:hopen hsym `$getenv`OUT; / absolute path: loading the HDB changes directory +dropcmd:"bash ",getenv[`PWD],"/src/benchmarking/dropcache.sh ",getenv`HDB_DIR; / full path: loading the HDB changes directory +drop:{system dropcmd}; +runs:"J"$getenv`RUNS; +tasks:`$" " vs getenv`TASKS; +caches:`$" " vs getenv`CACHES; + +system "l src/approvedtasks.q"; +system "l ",getenv`HDB_DIR; +args:`vwap`sumtil!(date; 50#50000000); + +/ one timed run: seconds +run1:{[tt] t0:.z.p; (.task tt) peach args tt; 1e-9*`long$.z.p-t0}; + +write:{[tt;cache;run;secs] + out enlist "," sv (string .z.p; "kdbx"; string tt; string system "g"; string 0; string system "s"; string cache; string run; string secs; string secs); + }; + +{[tt] + {[tt;cache] + if[cache=`warm; run1 tt]; / one untimed run first, so the pages are cached + {[tt;cache;run] + if[cache=`cold; drop[]]; + write[tt;cache;run] s:run1 tt; + -1 "kdbx -s ",string[system "s"]," -g ",string[system "g"]," ",string[tt]," ",string[cache]," run ",string[run],": ",string s; + }[tt;cache] each 1+til runs; + }[tt] each caches; + } each tasks; + +exit 0 \ No newline at end of file diff --git a/src/benchmarking/benchjob.q b/src/benchmarking/benchjob.q new file mode 100644 index 0000000..2e2201a --- /dev/null +++ b/src/benchmarking/benchjob.q @@ -0,0 +1,43 @@ +/ one framework configuration: each task as one job, RUNS times, cold and warm, appended to OUT +/ started by src/benchmarking/benchmarking.sh once the primary is up; then tells the primary to exit + +h:hopen "J"$getenv`PRIMARY_PORT; +out:hopen hsym `$getenv`OUT; +hdb:getenv`HDB_DIR; +drop:{system "bash src/benchmarking/dropcache.sh ",hdb}; +runs:"J"$getenv`RUNS; +tasks:`$" " vs getenv`TASKS; +caches:`$" " vs getenv`CACHES; +cfg:`gc`secondaries`threads!"J"$getenv each `BENCH_GC`BENCH_SECS`BENCH_THREADS; +args:`vwap`sumtil!({x where not null x} "D"$string key hsym `$hdb; 50#50000000); + +/ wait for every secondary to register (up to a minute) +waitfree:{[n] i:0; while[(n>(h(`.primary.status;::))[`seccounts;`free]) and 300>i+:1; system "sleep 0.2"]; i<300}; +if[not waitfree cfg`secondaries; -2 "not every secondary registered"; exit 1]; + +/ one timed job: (wall seconds as the user sees it, the job's own finishedtime - submittedtime) +job:{[tt] + t0:.z.p; + r:h(`.primary.newjob; tt; args tt; ()!()); + wall:1e-9*`long$.z.p-t0; + (wall; 1e-9*`long$h({first exec finishedtime-submittedtime from jobs where jobid=x}; r`jobid)) + }; + +write:{[tt;cache;run;t] + out enlist "," sv (string .z.p; "framework"; string tt; string cfg`gc; string cfg`secondaries; string cfg`threads; string cache; string run; string t 0; string t 1); + }; + +{[tt] + {[tt;cache] + if[cache=`warm; job tt]; / one untimed job first: pages cached, secondaries warmed up + {[tt;cache;run] + if[cache=`cold; drop[]]; + write[tt;cache;run] t:job tt; + -1 "framework ",string[cfg`secondaries]," secs -s ",string[cfg`threads]," -g ",string[cfg`gc]," ",string[tt]," ",string[cache]," run ",string[run],": ",string t 1; + }[tt;cache] each 1+til runs; + }[tt] each caches; + } each tasks; + +/ stop the primary: its .z.exit stops the secondaries +neg[h]"exit 0"; neg[h][]; +exit 0 \ No newline at end of file diff --git a/src/benchmarking/benchmarking.sh b/src/benchmarking/benchmarking.sh new file mode 100644 index 0000000..6330d3d --- /dev/null +++ b/src/benchmarking/benchmarking.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# the benchmark matrix: plain KDB-X (peach) and the framework, for each gc mode, thread count, secondary count and +# cache state. Results are appended to bench/results.csv; summarise with: x src/benchmarking/summary.q +# usage (from anywhere): bash src/benchmarking/benchmarking.sh +# a subset, e.g.: GCS="1" SECS="8" THREADS_LIST="1" CACHES="warm" TASKS="sumtil" RUNS=1 bash src/benchmarking/benchmarking.sh +set -euo pipefail +cd "$(dirname "$0")/../.." # the repo's top folder: src/benchmarking is two levels down +set -a; source config/default.env; source config/benchmarking.env; set +a +if [[ -z "${HDB_DIR:-}" ]]; then echo "set HDB_DIR in config/benchmarking.env first" >&2; exit 1; fi + +BENCH=src/benchmarking +Q="$KDBX_HOME/bin/q" +export QHOME="$KDBX_HOME" +export OUT="$PWD/bench/results.csv" RUNS="${RUNS:-3}" +export TASKS="${TASKS:-vwap sumtil}" CACHES="${CACHES:-cold warm}" +GCS="${GCS:-0 1}"; SECS="${SECS:-1 8 16}"; THREADS_LIST="${THREADS_LIST:-1 8 16}" + +mkdir -p bench logs/bench +[[ -f "$OUT" ]] || echo "time,mode,task,gc,secondaries,threads,cache,run,wallsecs,jobsecs" > "$OUT" + +waitport() { for _ in $(seq 100); do (exec 3<>"/dev/tcp/localhost/$PRIMARY_PORT") 2>/dev/null && return 0; sleep 0.2; done; return 1; } + +# plain KDB-X +for g in $GCS; do for t in $THREADS_LIST; do + echo "== kdbx -s $t -g $g" + "$Q" "$BENCH/baseline.q" -s "$t" -g "$g" -q < /dev/null +done; done + +# the framework: a fresh primary for each configuration +for g in $GCS; do for n in $SECS; do for t in $THREADS_LIST; do + echo "== framework: $n secondaries, -s $t each, -g $g" + GC_MODE=$g NUM_SECONDARYS=$n THREADS=$t "$Q" src/primary.q -p "$PRIMARY_PORT" < /dev/null >> logs/bench/primary.log 2>&1 & + ppid=$! + if ! waitport; then echo "primary didn't start: see logs/bench/primary.log" >&2; kill "$ppid"; exit 1; fi + BENCH_GC=$g BENCH_SECS=$n BENCH_THREADS=$t "$Q" "$BENCH/benchjob.q" -q < /dev/null || { kill "$ppid"; exit 1; } + wait "$ppid" 2>/dev/null || true # benchjob told it to exit; it stops its secondaries first + rm -rf "$RESULTS_DIR" # each setup's task results aren't needed once timed + sleep 1 # let the port free up +done; done; done + +echo "done: results in $OUT; summarise with: x $BENCH/summary.q" \ No newline at end of file diff --git a/src/benchmarking/dropcache.sh b/src/benchmarking/dropcache.sh new file mode 100644 index 0000000..6d468d2 --- /dev/null +++ b/src/benchmarking/dropcache.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# empty the page cache for the HDB, so the next run has to read it from disk +# with passwordless sudo: drop every clean page; otherwise ask the kernel to drop each HDB file's pages (no root needed) +# usage: src/benchmarking/dropcache.sh +sync +if sudo -n true 2>/dev/null; then + echo 3 | sudo tee /proc/sys/vm/drop_caches > /dev/null +else + python3 -c ' +import os, sys +for root, _, files in os.walk(sys.argv[1]): + for f in files: + try: + fd = os.open(os.path.join(root, f), os.O_RDONLY) + os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED) + os.close(fd) + except OSError: + pass +' "$1" +fi \ No newline at end of file diff --git a/src/benchmarking/summary.q b/src/benchmarking/summary.q new file mode 100644 index 0000000..d224dee --- /dev/null +++ b/src/benchmarking/summary.q @@ -0,0 +1,8 @@ +/ averages from bench/results.csv, one row per configuration, with cold and warm side by side +/ run: x src/benchmarking/summary.q + +r:("PSSJJJSJFF";enlist ",") 0: `:bench/results.csv; +s:0!select avgsecs:avg jobsecs, runs:count i by task, mode, gc, secondaries, threads, cache from r; +c:distinct s`cache; / the cache states that were run: cold, warm or both +p:0!exec c#cache!avgsecs by task, mode, gc, secondaries, threads from s; +show `task`mode`gc`secondaries`threads xasc p; \ No newline at end of file diff --git a/src/primary.q b/src/primary.q index 697b2bf..ab8b066 100644 --- a/src/primary.q +++ b/src/primary.q @@ -27,6 +27,7 @@ system"l src/common/access.q"; .primary.stopgraceperiod:"J"$getenv`STOP_GRACE_PERIOD_SECS; .primary.qbin:getenv[`KDBX_HOME],"/bin/q"; .primary.secondarythreads:1|"J"$getenv`THREADS; +.primary.gcmode:getenv`GC_MODE; // each secondary's -g: 0 deferred (q's default), 1 immediate; empty: not passed .primary.tasktimeout:`time$"J"$getenv `TASK_TIMEOUT_MS; .primary.taskretries:"I"$getenv`TASK_RETRIES_MAX; .primary.tasktypes:`$"," vs getenv`TASK_TYPES; @@ -134,7 +135,7 @@ system"l src/common/access.q"; // used to start a secondary process .primary.spawncmd:{ - " " sv (.primary.qbin; "src/secondary.q"; "-id"; string x; "-s"; string .primary.secondarythreads) + " " sv (.primary.qbin; "src/secondary.q"; "-id"; string x; "-s"; string .primary.secondarythreads),$[count .primary.gcmode; ("-g"; .primary.gcmode); ()] }; // ids taken right now: registered and still connected @@ -152,6 +153,8 @@ system"l src/common/access.q"; // example usage: // .primary.spawn 1 .primary.spawn:{ + if[not (type x) in -5 -6 -7h; '"a secondary's id must be a whole number"]; + if[not x within 1 1000; '"a secondary's id must be from 1 to 1000"]; if[x in .primary.usedids[]; .log.warn "secondary ",string[x]," already exists; next available id is ",string .primary.nextid[]; :()];