SQLite file format and SQL engine in pure OCaml
4 folders · 25 files

seeds
suite
wal_snapshot
workloads
baseline.json
c_parity.c
c_parity.ml
c_parity.sh
churn.ml
dune
insert_profile.ml
join_bench.ml
join_plan_bench.ml
join_seek_bench.ml
kv_bench.ml
partial_index_bench.ml
range_bench.ml
read_bench.ml
README.md
rewind_bench.ml
sections.sh
speedtest1.sh
tpcc.ml
tpcc.sh
tpch.sh
trace_top.ml
typed_insert.ml
typed_scan.ml
write_bench.ml
README.md

sqlite benchmarks #

The benchmarks of the package sqlite-bench hold the engine against libsqlite3 3.54.1, in one runner, on the same SQL. Their measure is the instructions each engine retires above its files, which a loaded machine moves far less than wall time, and the calls it makes to them. A committed baseline, baseline.json, holds both engines' counts for every workload, primitive and section, and a rerun fails when ours is more than 1% worse than it or makes one file call more.

Running #

The numbers compare with the baseline only from a release build linked with flambda LTO; a dev build's are another program's, and the runner refuses to compare them. In a release-profile context, the bench alias runs every workload and checks it against the baseline:

dune build @bench/suite/bench

and the runner itself, suite/main.exe in that build directory, runs a selection or refreshes the baseline:

suite/main.exe --dir bench report sel w_trig
suite/main.exe --dir bench report --baseline bench/baseline.json
suite/main.exe --dir bench report --record bench/baseline.json

--record replaces the entries it measured and keeps the others, so a refresh of a few workloads leaves the rest of the baseline as it was. --samples N (default 3) is the number of runs per engine and workload, the two engines in turn, each in a process of its own; the least of each measure is kept. --reruns N (default 3) is how many more rounds of runs an entry over the tolerance takes before the check judges it (The check).

The runner links libsqlite3 from Homebrew's keg on macOS, not the system's (Apple's build of 3.54.0), or the one pkg-config names elsewhere, and refuses any version but 3.54.1.

What is counted #

The counter is the measuring thread's: thread_selfcounts on macOS, which counts the kernel's work for the thread with its own and needs no privilege, and a perf_event_open counter of the thread's user-space instructions on Linux. No unprivileged macOS process can count user-space instructions alone.

From a run's count the runner sets aside two kinds of work, each between two reads of the counter:

  • each call an engine makes to its files: libsqlite3's through a VFS over its default one that brackets every method, ours' through an Eio directory over the host's that brackets every file and directory operation, the page reads and writes ours makes on a file's descriptor, past Eio's scheduler, among them;
  • each blocking section of the OCaml runtime, where ours waits on a system call or on one of the threads Eio hands system calls to.

What is left is the engine's own work. What is set aside is mostly the kernel's, which moves with the contention of every other process on the machine: on lit_upd, which commits 2,000 times, it is about 95% of libsqlite3's count and moved it by 20% from one run to the next. The reads that open and close a bracket are system calls themselves, and the part of them outside it, with any preemption at their return, stays in the count; so do ours' record locks, which it takes on a file's descriptor rather than through Eio.

The calls themselves are counted, ours io and sqlite io in the report. A run makes the same calls every time, so the check fails on any call ours makes beyond the baseline's: that is how a change that writes or syncs more shows, its cost being set aside.

On a machine at a load of 30 to 60 on 18 cores, ten runs of one binary gave the same file calls every time; a run's count moved by up to 1% on most workloads and by up to 6% on the two of 2,000 commits, against 17 to 20% and more before the file system's work was set aside.

The check #

What stays in a run's count beside the engine's work is the kernel's work the measuring thread does outside the brackets, and it only adds: a page fault costs the thread about 8,000 instructions, more when the page comes back from the memory compressor, and a preemption a few thousand. How many a run takes is the machine's: of ten runs of kv_update at a load of 9, the lightest retired 4,242 M instructions of ours and the heaviest 4,483 M, with 26,541 page faults against 618; libsqlite3's lit_upd ran from 135.5 M to 151.7 M, with 271 to 10,067 involuntary context switches.

The check against the baseline is made on the least of an entry's runs, and an entry that comes out worse runs again before it is judged:

  • each engine runs each workload --samples times (3), the two in turn, each run in a process of its own, and the least of each measure is kept;
  • a workload or primitive whose least is more than the tolerance (1%) over the baseline runs another round of --samples runs, up to --reruns rounds (3), until the least of all its runs is within; the report prints a line for each one that needed it;
  • it is worse when that least is still over, or when it makes one file call more than the baseline.

--record alone takes the entries it replaces as the baseline of the same rule, so that a run a busy moment made heavier is run again before it is recorded: a baseline entry recorded too heavy would hide a change that costs up to as much.

Since the machine only adds, more runs only bring the least closer to the engine's own count: a change that costs more than the tolerance stays over it after every round, and a run a busy moment made heavier is cleared. Twenty-four runs of every workload, at a load of 7 to 34, make 2,520 pairs of a least of three checked against another least of three: 115 of them flagged ours (4.6%), and 10 still did after the reruns, on lit_upd, lit_two and o_seek. One run of o_seek or o_desc was under 5 M instructions, which a handful of preemptions moved by several percent; they now run their query 200 times.

What reruns cannot clear is a machine busier through the whole check than through the recording of the baseline: at a load of 20, the least of twelve runs of lit_upd, which makes 24,041 file calls, is 1.8% over its least at a load of 10, and of three checks of one binary right after it recorded its baseline at a load of 22, those at 19 to 39 found nothing and the one at 45 to 75 flagged lit_upd 1.03% worse after its reruns. sqlite3's count, which the check reports when it moves, tells a moved machine from a moved engine, and a change is best judged against a baseline recorded in the same session.

A run's count depends on its workload and its engine alone, never on what the runner measured before it. Where ours' major collections fall depends on the heap a run starts from and on how far the collector has paced its cycle, which a run inherits from every run before it in the same process, however compacted the heap. So each run of a workload or a primitive is a process of its own, which runs it once, compacts its heap and counts its second run: the state it is counted from is the one that same run leaves, whatever the runner measured before. The engines still take their turns, and the least of the runs is kept, as above.

Each section of the standard benchmarks runs once, on a heap compacted before ours' run of it, and is judged on that one run. The sections of a script share one database, so they run in one process, and a section's count still carries the collector's pace from the sections before it.

The report #

Before it measures anything, report runs each workload once on each engine, in a child process, and compares the rows the workload returned and the database it left, both read back through libsqlite3: integrity_check, the schema and every table's rows. A workload whose engines disagree is printed as DIFFER with the reason, gets no number, and fails the run.

Each workload then runs on a fresh copy of its seed. The columns are:

  • ours and sqlite3: millions of instructions one run retires, its file calls and waits set aside;
  • ratio: ours over sqlite3;
  • ours ms and sqlite ms: wall time of one run;
  • ours MW: millions of words ours allocates on the OCaml heap, among them the few words the counted directory allocates for each file ours opens;
  • ours io and sqlite io: the calls one run makes to its files.

The primitives follow, per call: instructions, nanoseconds and words.

Both engines open each database with PRAGMA cache_size=-2000, a rollback journal and synchronous=OFF: every commit writes its journal and its pages, and neither engine waits for the disk, whose fsyncs would otherwise make the time of a workload of 2,000 commits the disk's.

The allocation gate #

The bench alias runs a second check beside the instruction count: Thumper's, over the same workloads and primitives. Each workload is two Thumper cases, workload/NAME/ours and workload/NAME/sqlite3, each call of which starts from a fresh clone of the seed, and each primitive a case of micro/. Every case is budgeted at 1% growth of the words it allocates, which Thumper counts exactly where a call allocates the same every time and which no machine load moves, and at 5% of its wall time, which Thumper declares inconclusive on a loaded host. The baseline is suite/sqlite-release.thumper, one section per machine.

Any first argument other than report, sample (one run of report's, in its own process), sections and the drivers' helpers below (tpch-queries, tpcc-rate, parity-row) is Thumper's command line, after the engines are checked to agree on the workloads it selects:

suite/main.exe --dir bench check -f workload/w_trig/
suite/main.exe --dir bench bless --force -f workload/kv_read/

bless records the cases it measures into this machine's section, and refuses a loaded host without --force, which the baseline then records.

Workloads #

Each workload is a file of workloads. Its first comment lines are directives: -- group: files it, -- seed: NAME starts it from the database seeds/NAME.sql builds at the start of the run (from no file without one), and -- journal: wal opens both engines in WAL mode. A -- repeat N line writes the line after it N times, with $i from 1 to N.

The seeds are scan (767,910 rows of an edition, a code shared by 22 rows, a level and an area, with an index on the code), var (400,000 rows whose text runs from 0 to 39 bytes), src (100,000 integers), join (20,000 rows and a four-way fan-out table), speed (20,000 rows of 200-byte texts), wide (5,000 rows of 158 columns), flows (40,000 places of 300 bytes keyed by a TEXT primary key, and 150,000 flows naming one at each end) and kv (kvtest's).

  • reads: sel (a count under a scan), sel15 (the same fifteen times), selrows (its 69,810 rows), selvar (filters after texts of varying length), cnt_cov (a count from the index alone), cte (a recursive CTE of 100,000 rows), like_scan (a LIKE prefix filter), wide_first and wide_last (a count of the first and the last of 158 columns).
  • plans: o_seek, o_cov, o_desc and o_noidx (ORDER BY through an index, its covering scan, descending, and a top-k sort without one; the seek and the descending scan 200 times each, one alone being too short to count to 1%), order_limit (a top-k sort of 100), join (a range count, a key lookup, a grouped row, and joins pinned on either side, on a two-column key, unpinned and ordered), r_bound (rowid ranges bounded by a real, a text and an index equality), r_index (ranges of an index: counts it covers, a sum read from the rows, a range under a LIMIT and one walked backwards), wr_isearch and wr_order (index searches and orderings on a WITHOUT ROWID table), pk_lookup (5,000 key lookups), lit_sub (3,000 EXISTS subqueries differing in a literal), flow_ends (each flow joined by key to the place at either end, which sqlite3 answers with two seeks of the key index and two rows a flow, over a table past the page cache).
  • writes: upd, del, updrange and delrange (an UPDATE and a DELETE by scan and by rowid range), r_ins, r_check, s_ins and s_plain (100,000 inserts from a CTE or a table, with and without a CHECK and a UNIQUE index), w_row and w_row_wal (inserts, two whole-table UPDATEs and a DELETE, in either journal mode), ins_txn (20,000 INSERT statements in one transaction), lit_upd and lit_two (2,000 one-row UPDATEs differing in a literal).
  • triggers: w_trig (2,000 INSERT triggers, then 1,000 UPDATE triggers pinning a row by NEW.id), t_ins and t_upd (each half alone), trig_del (DELETE triggers pinning by OLD.id), trig_idx (an UPDATE trigger pinning through a UNIQUE column).
  • expressions: x_cnt (arithmetic in a filter), x_proj (arithmetic projected), x_cast (a CAST in a filter), x_func (length and substr), x_colcol (the rowid compared with a column).
  • kvtest: kv_read and kv_update, below.

The primitives are a record decoded and encoded, a table B-tree sought by rowid and an index B-tree by key, 1,000 appends to a table B-tree, and a filter of 1,000 rows run as closures.

Standard benchmarks #

SQLite publishes two benchmarks of its own, and TPC two of the database world's. Each runs through the section runner, suite/main.exe sections: both engines run the same script, in a rollback journal, section by section; each runs it once to compare the rows every section returns and once to count each section's instructions and CPU time, and prints each section's line as it ends. A section is an entry of the baseline when the engines agree on it, and the runner exits 1 when they answer one apart, with different rows or an error on one side alone. --limit SECONDS caps the CPU time of each run of a section on either engine, and reports a section past it as a timeout rather than wait for it. sections.sh builds the runner in the release context and runs it; the drivers pass it more options through SECTIONS_ARGS, which take --baseline FILE and --record FILE as report does.

speedtest1 #

SQLite's own performance benchmark, speedtest1.c in the test directory of sqlite at the version-3.54.1 tag. SQLite tracks its performance by the instructions cachegrind counts over this program. speedtest1.sh runs it as SQLite's speed-check.sh script does: --size 5 over the test sets of its mix1 macro that need no extension, at mix1's scales, --testset main,orm/25,cte/20,json,fp/3,parsenumber/25.

The script is what speedtest1 --script writes: each test's SQL between -- begin test N and -- end test N. Both engines run that text. The table adds a third column, the native speedtest1 program built against the same libsqlite3: its seconds per test as it prints them, and its instructions over the whole run on the summary line. The native program prepares and binds its statements, so it is the upstream reference rather than a like-for-like figure; libsqlite3 running the script is that.

SQLITE_SRC=~/git/sqlite SECTIONS_ARGS="--record bench/baseline.json" \
  bench/speedtest1.sh

SQLITE_SRC is a clone of sqlite holding the tag; without it the source is fetched from GitHub at the tag.

kvtest #

kvtest.c, in the same directory at the same tag: the blob store SQLite measures against a directory of files. Its database is pure SQL, so it is the seed kv, the database of kvtest init --count 10000 --size 10000 --variance 1000 with the blobs filled with a byte pattern rather than randomblob(), so both engines hold the same bytes. Its run is driven by its SQL rather than ported: kv_read is kvtest run --count 10000 --random, each read a SELECT v FROM kv WHERE k=... statement of its own, in an order that visits every key once, and kv_update is the same with --update, each blob rewritten in place at its own size in one transaction. kvtest prepares one statement and binds each key, and its update writes random bytes through a function of its own; here both engines parse each statement and write zeros. They are workloads of report, in the group kvtest.

TPC-H and TPC-C #

tpch.sh runs the 22 TPC-H queries, at the specification's validation parameters, on a TPC-H database at scale factor 0.1 built by the TPCH-sqlite kit (make SCALE_FACTOR=0.1); tpcc.sh runs the TPC-C population and transaction mix that tpcc.exe writes (tpcc.ml says where it departs from TPC-C 5.11). TPC's dbgen and query templates come under the TPC EULA, so the kit stays outside this repository; the runner's tpch-queries KIT SF turns its templates into SQLite's dialect, and each query runs under a limit of 120 CPU seconds (tpch.sh's fourth argument). tpcc.sh prints each engine's New-Order transactions per minute of CPU time through the runner's tpcc-rate:

SECTIONS_ARGS="--record bench/baseline.json" bench/tpch.sh KIT_DIR 0.1
SECTIONS_ARGS="--record bench/baseline.json" bench/tpcc.sh 1 5000 off

Refreshing the baseline #

A change that makes ours faster leaves the baseline above it, and the check lists it as better; a change that makes it slower fails the check. Either way, once the change is meant, record the suites it moved from a release build and commit baseline.json with it:

suite/main.exe --dir bench report --record bench/baseline.json
SQLITE_SRC=~/git/sqlite SECTIONS_ARGS="--record bench/baseline.json" \
  bench/speedtest1.sh

sqlite3's counts are recorded beside ours as the reference. A check reports an entry whose sqlite3 count moved by more than the tolerance, which means libsqlite3, the compiler or the machine changed, and fails on ours alone.