Skip to content

Instantly share code, notes, and snippets.

@corylanou
Created September 15, 2025 22:43
Show Gist options
  • Select an option

  • Save corylanou/c94680b325cc5daa3971ab5d6aa97262 to your computer and use it in GitHub Desktop.

Select an option

Save corylanou/c94680b325cc5daa3971ab5d6aa97262 to your computer and use it in GitHub Desktop.
Litestream Test Harness Demo Script - Comprehensive testing of litestream-test binary
#!/bin/bash
# Litestream Test Harness Demonstration Script
# This script demonstrates the litestream-test harness capabilities
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}=== Litestream Test Harness Demo ===${NC}"
echo ""
# Build the test harness if needed
if [ ! -f ./bin/litestream-test ]; then
echo -e "${BLUE}Building litestream-test binary...${NC}"
go build -o bin/litestream-test ./cmd/litestream-test
fi
# Test directory
TEST_DIR="/tmp/litestream-test-demo"
mkdir -p "$TEST_DIR"
# Function to display database info
show_db_info() {
local db_path=$1
local label=$2
echo -e "\n${GREEN}=== $label ===${NC}"
# File sizes
if [ -f "$db_path" ]; then
echo "Main DB size: $(ls -lh "$db_path" | awk '{print $5}')"
fi
if [ -f "$db_path-wal" ]; then
echo "WAL size: $(ls -lh "$db_path-wal" | awk '{print $5}')"
fi
if [ -f "$db_path-shm" ]; then
echo "SHM size: $(ls -lh "$db_path-shm" | awk '{print $5}')"
fi
# Quick stats from SQLite
echo -e "\nDatabase statistics:"
sqlite3 "$db_path" <<EOF
.mode column
.headers on
SELECT
name as table_name,
(SELECT COUNT(*) FROM sqlite_master WHERE type='index' AND tbl_name=m.name) as indexes,
(SELECT COUNT(*) FROM pragma_table_info(m.name)) as columns
FROM sqlite_master m
WHERE type='table' AND name NOT LIKE 'sqlite_%';
.headers off
SELECT '---' as '';
SELECT 'Total rows in tables:' as '';
EOF
# Row counts for each table
for table in $(sqlite3 "$db_path" "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';"); do
count=$(sqlite3 "$db_path" "SELECT COUNT(*) FROM $table;")
echo " $table: $count rows"
done
# Page stats
echo -e "\nPage statistics:"
sqlite3 "$db_path" <<EOF
PRAGMA page_count;
PRAGMA page_size;
PRAGMA journal_mode;
EOF
}
# Test 1: Small database (10MB)
echo -e "\n${BLUE}Test 1: Creating a 10MB database${NC}"
DB1="$TEST_DIR/test_10mb.db"
rm -f "$DB1"*
./bin/litestream-test populate \
-db "$DB1" \
-target-size 10MB \
-row-size 1024 \
-table-count 2 \
-index-ratio 0.3
show_db_info "$DB1" "10MB Database After Population"
# Test 2: Generate load on the database
echo -e "\n${BLUE}Test 2: Generating load for 30 seconds${NC}"
echo "Pattern: burst mode with 100 writes/sec"
./bin/litestream-test load \
-db "$DB1" \
-write-rate 100 \
-duration 30s \
-pattern burst \
-read-ratio 0.3 &
LOAD_PID=$!
# Let it run for a bit and show progress
sleep 5
echo -e "\n${GREEN}Load generation in progress...${NC}"
sleep 25
wait $LOAD_PID
show_db_info "$DB1" "10MB Database After Load Generation"
# Test 3: Database with specific page size (testing for lock page scenario)
echo -e "\n${BLUE}Test 3: Creating database with 8KB page size${NC}"
DB2="$TEST_DIR/test_8k_pages.db"
rm -f "$DB2"*
./bin/litestream-test populate \
-db "$DB2" \
-target-size 50MB \
-page-size 8192 \
-row-size 2048 \
-table-count 3
show_db_info "$DB2" "50MB Database with 8KB Pages"
# Test 4: Shrink operation
echo -e "\n${BLUE}Test 4: Testing shrink operation${NC}"
echo "Deleting 40% of data and running checkpoint"
# Show before size
BEFORE_SIZE=$(stat -f%z "$DB2" 2>/dev/null || stat -c%s "$DB2")
./bin/litestream-test shrink \
-db "$DB2" \
-delete-percentage 40 \
-checkpoint \
-checkpoint-mode FULL
# Show after size
AFTER_SIZE=$(stat -f%z "$DB2" 2>/dev/null || stat -c%s "$DB2")
echo -e "\n${GREEN}Shrink Results:${NC}"
echo "Size before: $(echo "scale=2; $BEFORE_SIZE / 1048576" | bc) MB"
echo "Size after: $(echo "scale=2; $AFTER_SIZE / 1048576" | bc) MB"
show_db_info "$DB2" "Database After Shrink"
# Test 5: Different write patterns
echo -e "\n${BLUE}Test 5: Testing different write patterns${NC}"
DB3="$TEST_DIR/test_patterns.db"
rm -f "$DB3"*
./bin/litestream-test populate \
-db "$DB3" \
-target-size 5MB
echo -e "\n${GREEN}Testing write patterns (10 seconds each):${NC}"
for pattern in constant burst random wave; do
echo -e "\nPattern: $pattern"
./bin/litestream-test load \
-db "$DB3" \
-write-rate 50 \
-duration 10s \
-pattern "$pattern" \
-workers 2
done
show_db_info "$DB3" "Database After Pattern Tests"
# Summary
echo -e "\n${BLUE}=== Test Summary ===${NC}"
echo "Created databases in: $TEST_DIR"
echo ""
ls -lh "$TEST_DIR"/*.db | awk '{print " " $NF ": " $5}'
echo -e "\n${GREEN}Demo complete!${NC}"
echo "You can examine the databases with:"
echo " sqlite3 $TEST_DIR/<database>.db"
echo ""
echo "To test with Litestream replication:"
echo " 1. Start Litestream: litestream replicate $TEST_DIR/test_10mb.db file://$TEST_DIR/replica"
echo " 2. Generate load: ./bin/litestream-test load -db $TEST_DIR/test_10mb.db -duration 1m"
echo " 3. Validate: ./bin/litestream-test validate -source-db $TEST_DIR/test_10mb.db -replica-url file://$TEST_DIR/replica"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment