Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
results/
logs/
state/
bench/
.idea/workspace.xml
85 changes: 61 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand Down
11 changes: 8 additions & 3 deletions bin/startsecondary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <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"
echo "startsecondary: id $ID, threads $THREADS, gc ${GC_MODE:-default}, primary port $PRIMARY_PORT, config $CONFIG, pid $!, log $LOG"
14 changes: 14 additions & 0 deletions config/benchmarking.env
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion config/default.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
SEC_CODE=1234

# garbage collection mode for each secondary (-g): 0 deferred (q's default), 1 immediate; empty means q's default
GC_MODE=
138 changes: 138 additions & 0 deletions docs/Benchmarking.md
Original file line number Diff line number Diff line change
@@ -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 <THREADS>`, and with `-g <GC_MODE>` 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 <a column file>` (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 |
33 changes: 33 additions & 0 deletions src/benchmarking/baseline.q
Original file line number Diff line number Diff line change
@@ -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 <threads> -g <gc> -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
Loading