43 lines
2.2 KiB
Bash
Executable File
43 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")/.."
|
|
runid=$(date +%Y%m%d%H%M%S)
|
|
out=evidence/plans-$runid
|
|
mkdir -p "$out"
|
|
src=bench_$runid; restored=restore_$runid
|
|
pg=(docker compose exec -T db)
|
|
"${pg[@]}" createdb -U lab "$src"
|
|
"${pg[@]}" createdb -U lab "$restored"
|
|
sql=("${pg[@]}" psql -X -U lab -v ON_ERROR_STOP=1 -d "$src")
|
|
"${sql[@]}" < migrations/001.sql >/dev/null
|
|
"${sql[@]}" > "$out/plans.txt" <<'SQL'
|
|
\timing on
|
|
INSERT INTO users(username,display_name,salt,hash) VALUES('bench','Bench','dummy','dummy');
|
|
INSERT INTO chats(title) SELECT 'c'||g FROM generate_series(1,10) g;
|
|
CREATE TEMP TABLE seed AS SELECT c.id chat_id,u.id sender_id,'sample '||g AS text FROM generate_series(1,100000) g JOIN chats c ON c.title='c'||CASE WHEN g<=90000 THEN 1 ELSE 2+g%9 END CROSS JOIN users u;
|
|
DROP INDEX history_key;
|
|
CREATE TABLE alternative (LIKE messages INCLUDING ALL);
|
|
ALTER TABLE alternative ADD FOREIGN KEY(chat_id) REFERENCES chats, ADD FOREIGN KEY(sender_id) REFERENCES users;
|
|
CREATE INDEX alt_chat ON alternative(chat_id);
|
|
INSERT INTO alternative(chat_id,sender_id,text) SELECT * FROM seed;
|
|
CREATE INDEX history_key ON messages(chat_id,created_at,id);
|
|
INSERT INTO messages(chat_id,sender_id,text) SELECT * FROM seed;
|
|
ANALYZE messages;
|
|
ANALYZE alternative;
|
|
SELECT id AS cid FROM chats WHERE title='c1' \gset
|
|
EXPLAIN(ANALYZE,BUFFERS) SELECT * FROM alternative WHERE chat_id=:'cid' ORDER BY created_at DESC,id DESC LIMIT 51;
|
|
EXPLAIN(ANALYZE,BUFFERS) SELECT * FROM messages WHERE chat_id=:'cid' ORDER BY created_at DESC,id DESC LIMIT 51;
|
|
DROP TABLE alternative;
|
|
SQL
|
|
start=$SECONDS
|
|
"${pg[@]}" pg_dump -Fc -U lab "$src" > "$out/benchmark.dump"
|
|
echo "dump_seconds=$((SECONDS-start))" > "$out/recovery.txt"
|
|
start=$SECONDS
|
|
"${pg[@]}" pg_restore --exit-on-error -U lab -d "$restored" < "$out/benchmark.dump"
|
|
echo "restore_seconds=$((SECONDS-start))" >> "$out/recovery.txt"
|
|
for database in "$src" "$restored"; do
|
|
"${pg[@]}" psql -XAt -U lab -d "$database" -c "SELECT count(*),md5(string_agg(id::text||chat_id::text||sender_id::text||text||created_at::text,'' ORDER BY id)) FROM messages" > "$out/$database.digest"
|
|
done
|
|
diff -u "$out/$src.digest" "$out/$restored.digest"
|
|
echo "Databases retained: $src and $restored"
|