#!/bin/bash # Simple but effective PostgreSQL Load Test # Direct SQL execution with timing set -e DB_URL="postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt" TEST_DURATION=20 # seconds per test RESULTS_FILE="DB_LOAD_TEST_RESULTS_$(date +%Y%m%d_%H%M%S).txt" echo "========================================" | tee "$RESULTS_FILE" echo "PostgreSQL Load Test - Foxhunt Trading" | tee -a "$RESULTS_FILE" echo "Test Duration: $TEST_DURATION seconds per scenario" | tee -a "$RESULTS_FILE" echo "Start Time: $(date)" | tee -a "$RESULTS_FILE" echo "========================================" | tee -a "$RESULTS_FILE" echo "" | tee -a "$RESULTS_FILE" # Health check echo "=== Database Configuration ===" | tee -a "$RESULTS_FILE" psql "$DB_URL" -t -c "SHOW max_connections;" | awk '{print "Max Connections: " $1}' | tee -a "$RESULTS_FILE" psql "$DB_URL" -t -c "SHOW shared_buffers;" | awk '{print "Shared Buffers: " $1}' | tee -a "$RESULTS_FILE" psql "$DB_URL" -t -c "SHOW synchronous_commit;" | awk '{print "Synchronous Commit: " $1}' | tee -a "$RESULTS_FILE" echo "" | tee -a "$RESULTS_FILE" echo "=== Baseline Table Counts ===" | tee -a "$RESULTS_FILE" psql "$DB_URL" -c "SELECT 'orders' AS table_name, COUNT(*) FROM orders;" | tee -a "$RESULTS_FILE" echo "" | tee -a "$RESULTS_FILE" # Function to run load test run_load_test() { local clients=$1 local test_name=$2 echo "========================================" | tee -a "$RESULTS_FILE" echo "TEST: $test_name ($clients clients)" | tee -a "$RESULTS_FILE" echo "========================================" | tee -a "$RESULTS_FILE" local temp_dir="/tmp/dbtest_$$_$clients" mkdir -p "$temp_dir" local start_time=$(date +%s.%N) # Launch workers for ((i=1; i<=clients; i++)); do ( local count=0 local errors=0 local end_time=$(echo "$start_time + $TEST_DURATION" | bc) while (( $(echo "$(date +%s.%N) < $end_time" | bc -l) )); do # Insert order if psql "$DB_URL" -c "INSERT INTO orders (account_id, symbol, side, order_type, quantity, limit_price, venue, created_at, updated_at) VALUES ('load_test', 'BTC/USD', 'buy', 'limit', 100000000, 5000000000000, 'test', EXTRACT(EPOCH FROM NOW())*1000000000, EXTRACT(EPOCH FROM NOW())*1000000000);" > /dev/null 2>&1; then ((count++)) else ((errors++)) fi # Periodically run queries if (( count % 5 == 0 )); then psql "$DB_URL" -c "SELECT COUNT(*) FROM orders WHERE symbol = 'BTC/USD';" > /dev/null 2>&1 || ((errors++)) fi done echo "$count $errors" > "$temp_dir/worker_$i.txt" ) & done # Wait for completion wait local end_time=$(date +%s.%N) local duration=$(echo "$end_time - $start_time" | bc) # Aggregate results local total_ops=0 local total_errors=0 for ((i=1; i<=clients; i++)); do if [ -f "$temp_dir/worker_$i.txt" ]; then read ops errors < "$temp_dir/worker_$i.txt" total_ops=$((total_ops + ops)) total_errors=$((total_errors + errors)) fi done local tps=$(echo "scale=2; $total_ops / $duration" | bc) echo "Duration: $duration seconds" | tee -a "$RESULTS_FILE" echo "Total Operations: $total_ops" | tee -a "$RESULTS_FILE" echo "Errors: $total_errors" | tee -a "$RESULTS_FILE" echo "Transactions per Second (TPS): $tps" | tee -a "$RESULTS_FILE" # Connection pool status echo "" | tee -a "$RESULTS_FILE" echo "Connection Pool:" | tee -a "$RESULTS_FILE" psql "$DB_URL" -c "SELECT state, COUNT(*) FROM pg_stat_activity WHERE datname='foxhunt' GROUP BY state;" | tee -a "$RESULTS_FILE" # Lock stats echo "" | tee -a "$RESULTS_FILE" local locks=$(psql "$DB_URL" -t -c "SELECT COUNT(*) FROM pg_locks WHERE NOT granted;" | tr -d ' ') echo "Locks Waiting: $locks" | tee -a "$RESULTS_FILE" # Deadlocks local deadlocks=$(psql "$DB_URL" -t -c "SELECT deadlocks FROM pg_stat_database WHERE datname='foxhunt';" | tr -d ' ') echo "Deadlocks: $deadlocks" | tee -a "$RESULTS_FILE" echo "" | tee -a "$RESULTS_FILE" rm -rf "$temp_dir" } # Run tests run_load_test 1 "BASELINE" run_load_test 10 "MODERATE" run_load_test 50 "HIGH" run_load_test 100 "STRESS" # Final analysis echo "========================================" | tee -a "$RESULTS_FILE" echo "FINAL ANALYSIS" | tee -a "$RESULTS_FILE" echo "========================================" | tee -a "$RESULTS_FILE" echo "=== Table Statistics ===" | tee -a "$RESULTS_FILE" psql "$DB_URL" -c "SELECT relname, n_tup_ins as inserts, n_tup_upd as updates, idx_scan FROM pg_stat_user_tables WHERE relname='orders';" | tee -a "$RESULTS_FILE" echo "" | tee -a "$RESULTS_FILE" echo "=== Cache Hit Ratio ===" | tee -a "$RESULTS_FILE" psql "$DB_URL" -t -c "SELECT (sum(blks_hit)::float / NULLIF(sum(blks_hit) + sum(blks_read), 0) * 100)::numeric(5,2) || '%' FROM pg_stat_database WHERE datname='foxhunt';" | tee -a "$RESULTS_FILE" echo "" | tee -a "$RESULTS_FILE" echo "=== Final Counts ===" | tee -a "$RESULTS_FILE" psql "$DB_URL" -c "SELECT COUNT(*) as total_orders FROM orders;" | tee -a "$RESULTS_FILE" echo "" | tee -a "$RESULTS_FILE" echo "========================================" | tee -a "$RESULTS_FILE" echo "Test Completed: $(date)" | tee -a "$RESULTS_FILE" echo "========================================" | tee -a "$RESULTS_FILE" echo "" echo "Results saved to: $RESULTS_FILE"