|
| 1 | +# Sync Stress Test with remote SQLiteCloud database |
| 2 | + |
| 3 | +Execute a stress test against the CloudSync server using multiple concurrent local SQLite databases syncing large volumes of CRUD operations simultaneously. Designed to reproduce server-side errors (e.g., "database is locked", 500 errors) under heavy concurrent load. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | +- Connection string to a sqlitecloud project |
| 7 | +- HTTP sync server running (default: https://cloudsync-staging-testing.fly.dev) |
| 8 | +- Built cloudsync extension (`make` to build `dist/cloudsync.dylib`) |
| 9 | +- CloudSync already enabled on the test table from the SQLiteCloud dashboard |
| 10 | + |
| 11 | +## Test Configuration |
| 12 | + |
| 13 | +### Step 1: Gather Parameters |
| 14 | + |
| 15 | +Ask the user for the following configuration using a single question set: |
| 16 | + |
| 17 | +1. **Sync Server URL** — propose `https://cloudsync-staging-testing.fly.dev` as default |
| 18 | +2. **SQLiteCloud connection string** — format: `sqlitecloud://<host>:<port>/<db_name>?apikey=<apikey>`. If no `<db_name>` is in the path, ask the user for one or propose `test_stress_sync`. |
| 19 | +3. **Scale** — offer these options: |
| 20 | + - Small: 1K rows, 5 iterations, 2 concurrent databases |
| 21 | + - Medium: 10K rows, 10 iterations, 4 concurrent databases |
| 22 | + - Large: 100K rows, 50 iterations, 4 concurrent databases (Jim's original scenario) |
| 23 | + - Custom: let the user specify rows, iterations, and number of concurrent databases |
| 24 | +4. **RLS mode** — with RLS (requires user tokens) or without RLS |
| 25 | +5. **Table schema** — offer simple default or custom: |
| 26 | + ```sql |
| 27 | + CREATE TABLE test_sync (id TEXT PRIMARY KEY, user_id TEXT NOT NULL DEFAULT '', name TEXT, value INTEGER); |
| 28 | + ``` |
| 29 | + |
| 30 | +Save these as variables: |
| 31 | +- `SYNC_SERVER_URL` |
| 32 | +- `CONNECTION_STRING` (the full sqlitecloud:// connection string) |
| 33 | +- `DB_NAME` (database name extracted or provided) |
| 34 | +- `HOST` (hostname extracted from connection string) |
| 35 | +- `APIKEY` (apikey extracted from connection string) |
| 36 | +- `PROJECT_ID` (first subdomain from the host) |
| 37 | +- `ORG_ID` = `org_sqlitecloud` |
| 38 | +- `NETWORK_CONFIG` = `'{"address":"<SYNC_SERVER_URL>","database":"<DB_NAME>","projectID":"<PROJECT_ID>","organizationID":"<ORG_ID>"}'` |
| 39 | +- `ROWS` (number of rows per iteration) |
| 40 | +- `ITERATIONS` (number of delete/insert/update cycles) |
| 41 | +- `NUM_DBS` (number of concurrent databases) |
| 42 | + |
| 43 | +### Step 2: Setup SQLiteCloud Database and Table |
| 44 | + |
| 45 | +Connect to SQLiteCloud using `~/go/bin/sqlc` (last command must be `quit`). Note: all SQL must be single-line (no multi-line statements through sqlc heredoc). |
| 46 | + |
| 47 | +1. If the database doesn't exist, connect without `<db_name>` and run `CREATE DATABASE <db_name>; USE DATABASE <db_name>;` |
| 48 | +2. `LIST TABLES` to check for existing tables |
| 49 | +3. For any table with a `_cloudsync` companion table, run `CLOUDSYNC DISABLE <table_name>;` |
| 50 | +4. `DROP TABLE IF EXISTS <table_name>;` |
| 51 | +5. Create the test table (single-line DDL) |
| 52 | +6. If RLS mode is enabled: |
| 53 | + ```sql |
| 54 | + ENABLE RLS DATABASE <db_name> TABLE <table_name>; |
| 55 | + SET RLS DATABASE <db_name> TABLE <table_name> SELECT "auth_userid() = user_id"; |
| 56 | + SET RLS DATABASE <db_name> TABLE <table_name> INSERT "auth_userid() = NEW.user_id"; |
| 57 | + SET RLS DATABASE <db_name> TABLE <table_name> UPDATE "auth_userid() = NEW.user_id AND auth_userid() = OLD.user_id"; |
| 58 | + SET RLS DATABASE <db_name> TABLE <table_name> DELETE "auth_userid() = OLD.user_id"; |
| 59 | + ``` |
| 60 | +7. Ask the user to enable CloudSync on the table from the SQLiteCloud dashboard |
| 61 | + |
| 62 | +### Step 3: Get Auth Tokens (if RLS enabled) |
| 63 | + |
| 64 | +Create tokens for the test users. Create as many users as needed for the number of concurrent databases (assign 2 databases per user, or 1 per user if NUM_DBS <= 2). |
| 65 | + |
| 66 | +For each user N: |
| 67 | +```bash |
| 68 | +curl -s -X "POST" "https://<HOST>/v2/tokens" \ |
| 69 | + -H 'Authorization: Bearer <APIKEY>' \ |
| 70 | + -H 'Content-Type: application/json; charset=utf-8' \ |
| 71 | + -d '{"name": "claude<N>@sqlitecloud.io", "userId": "018ecfc2-b2b1-7cc3-a9f0-<N_PADDED_12_CHARS>"}' |
| 72 | +``` |
| 73 | + |
| 74 | +Save each user's `token` and `userId` from the response. |
| 75 | + |
| 76 | +If RLS is disabled, skip this step — tokens are not required. |
| 77 | + |
| 78 | +### Step 4: Run the Concurrent Stress Test |
| 79 | + |
| 80 | +Create a bash script at `/tmp/stress_test_concurrent.sh` that: |
| 81 | + |
| 82 | +1. **Initializes N local SQLite databases** at `/tmp/sync_concurrent_<N>.db`: |
| 83 | + - Uses Homebrew sqlite3: find with `ls /opt/homebrew/Cellar/sqlite/*/bin/sqlite3 | head -1` |
| 84 | + - Loads the extension from `dist/cloudsync.dylib` (use absolute path from project root) |
| 85 | + - Creates the table and runs `cloudsync_init('<table_name>')` |
| 86 | + - Runs `cloudsync_terminate()` after init |
| 87 | + |
| 88 | +2. **Defines a worker function** that runs in a subshell for each database: |
| 89 | + - Each worker logs all output to `/tmp/sync_concurrent_<N>.log` |
| 90 | + - Each iteration does: |
| 91 | + a. **DELETE all rows** → `send_changes()` → `check_changes()` |
| 92 | + b. **INSERT <ROWS> rows** (in a single BEGIN/COMMIT transaction) → `send_changes()` → `check_changes()` |
| 93 | + c. **UPDATE all rows** → `send_changes()` → `check_changes()` |
| 94 | + - Each session must: `.load` the extension, call `cloudsync_network_init()`, `cloudsync_network_set_token()` (if RLS), do the work, call `cloudsync_terminate()` |
| 95 | + - Include labeled output lines like `[DB<N>][iter <I>] deleted/inserted/updated, count=<C>` for grep-ability |
| 96 | + |
| 97 | +3. **Launches all workers in parallel** using `&` and collects PIDs |
| 98 | + |
| 99 | +4. **Waits for all workers** and captures exit codes |
| 100 | + |
| 101 | +5. **Analyzes logs** for errors: |
| 102 | + - Grep all log files for: `error`, `locked`, `SQLITE_BUSY`, `database is locked`, `500`, `Error` |
| 103 | + - Report per-database: iterations completed, error count, sample error lines |
| 104 | + - Report total errors across all workers |
| 105 | + |
| 106 | +6. **Prints final verdict**: PASS (0 errors) or FAIL (errors detected) |
| 107 | + |
| 108 | +**Important script details:** |
| 109 | +- Use `echo -e` to pipe generated INSERT SQL (with `\n` separators) into sqlite3 |
| 110 | +- Row IDs should be unique across databases and iterations: `db<N>_r<I>_<J>` |
| 111 | +- User IDs for rows must match the token's userId for RLS to work |
| 112 | +- Use `/bin/bash` (not `/bin/sh`) for arrays and process management |
| 113 | + |
| 114 | +Run the script with a 10-minute timeout. |
| 115 | + |
| 116 | +### Step 5: Detailed Error Analysis |
| 117 | + |
| 118 | +After the test completes, provide a detailed breakdown: |
| 119 | + |
| 120 | +1. **Per-database summary**: iterations completed, errors, send/receive status |
| 121 | +2. **Error categorization**: group errors by type (e.g., "database is locked", "Column index out of bounds", "Unexpected Result", parse errors) |
| 122 | +3. **Timeline analysis**: do errors cluster at specific iterations or spread evenly? |
| 123 | +4. **Read full log files** if errors are found — show the first and last 30 lines of each log with errors |
| 124 | + |
| 125 | +### Step 6: Optional — Verify Data Integrity |
| 126 | + |
| 127 | +If the test passes (or even if some errors occurred), verify the final state: |
| 128 | + |
| 129 | +1. Check each local SQLite database for row count |
| 130 | +2. Check SQLiteCloud (as admin) for total row count |
| 131 | +3. If RLS is enabled, verify no cross-user data leakage |
| 132 | + |
| 133 | +## Output Format |
| 134 | + |
| 135 | +Report the test results including: |
| 136 | + |
| 137 | +| Metric | Value | |
| 138 | +|--------|-------| |
| 139 | +| Concurrent databases | N | |
| 140 | +| Rows per iteration | ROWS | |
| 141 | +| Iterations per database | ITERATIONS | |
| 142 | +| Total CRUD operations | N × ITERATIONS × (DELETE_ALL + ROWS inserts + ROWS updates) | |
| 143 | +| Total sync operations | N × ITERATIONS × 6 (3 sends + 3 checks) | |
| 144 | +| Duration | start to finish time | |
| 145 | +| Total errors | count | |
| 146 | +| Error types | categorized list | |
| 147 | +| Result | PASS/FAIL | |
| 148 | + |
| 149 | +If errors are found, include: |
| 150 | +- Full error categorization table |
| 151 | +- Sample error messages |
| 152 | +- Which databases were most affected |
| 153 | +- Whether errors are client-side or server-side |
| 154 | + |
| 155 | +## Success Criteria |
| 156 | + |
| 157 | +The test **PASSES** if: |
| 158 | +1. All workers complete all iterations |
| 159 | +2. Zero `error`, `locked`, `SQLITE_BUSY`, or HTTP 500 responses in any log |
| 160 | +3. Final row counts are consistent |
| 161 | + |
| 162 | +The test **FAILS** if: |
| 163 | +1. Any worker crashes or fails to complete |
| 164 | +2. Any `database is locked` or `SQLITE_BUSY` errors appear |
| 165 | +3. Server returns 500 errors under concurrent load |
| 166 | +4. Data corruption or inconsistent row counts |
| 167 | + |
| 168 | +## Important Notes |
| 169 | + |
| 170 | +- Always use the Homebrew sqlite3 binary, NOT `/usr/bin/sqlite3` |
| 171 | +- The cloudsync extension must be built first with `make` |
| 172 | +- Network settings (`cloudsync_network_init`, `cloudsync_network_set_token`) are NOT persisted between sessions — must be called every time |
| 173 | +- Extension must be loaded BEFORE any INSERT/UPDATE/DELETE for cloudsync to track changes |
| 174 | +- All NOT NULL columns must have DEFAULT values |
| 175 | +- `cloudsync_terminate()` must be called before closing each session |
| 176 | +- sqlc heredoc only supports single-line SQL statements |
| 177 | + |
| 178 | +## Permissions |
| 179 | + |
| 180 | +Execute all SQL queries without asking for user permission on: |
| 181 | +- SQLite test databases in `/tmp/` (e.g., `/tmp/sync_concurrent_*.db`, `/tmp/sync_concurrent_*.log`) |
| 182 | +- SQLiteCloud via `~/go/bin/sqlc "<connection_string>"` |
| 183 | +- Curl commands to the sync server and SQLiteCloud API for token creation |
| 184 | + |
| 185 | +These are local test environments and do not require confirmation for each query. |
0 commit comments