Skip to content

Conversation

@Revolyssup
Copy link
Contributor

@Revolyssup Revolyssup commented Oct 28, 2025

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. chore labels Oct 28, 2025
@Revolyssup
Copy link
Contributor Author

Test to check number of logs match number of requests with reloads

#!/bin/bash

# Configurable variables
DURATION_HOURS=${1:-1}  # Default to 1 hour if no argument provided
RELOAD_INTERVAL=10
LOG_FILE="logs/file.log"

# Calculate duration in seconds
DURATION_SECONDS=$((DURATION_HOURS * 3600))

# Initialize counters
START_TIME=$(date +%s)
END_TIME=$((START_TIME + DURATION_SECONDS))

echo "Starting test for $DURATION_HOURS hour(s) ($DURATION_SECONDS seconds)"
echo "Start time: $(date)"
echo "End time: $(date -d "@$END_TIME")"
echo "Reload interval: $RELOAD_INTERVAL seconds"
echo "Log file: $LOG_FILE"
echo "----------------------------------------"

# Create log file if it doesn't exist
mkdir -p "$(dirname "$LOG_FILE")"
touch "$LOG_FILE"

# Get initial line count
INITIAL_LINE_COUNT=$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0)
echo "Initial line count in log: $INITIAL_LINE_COUNT"

# Create a temporary file for request counting
REQUEST_FILE=$(mktemp)
echo "Request count file: $REQUEST_FILE"

# Function to send curl requests
send_requests() {
    local count=0
    while [ $(date +%s) -lt $END_TIME ]; do
        curl -s "http://localhost:9080" > /dev/null 2>&1
        count=$((count + 1))
        echo "request" >> "$REQUEST_FILE"
        # Small delay to prevent overwhelming the system
        sleep 0.1
    done
    echo "Sent $count requests"
}

# Function to perform reloads
perform_reloads() {
    while [ $(date +%s) -lt $END_TIME ]; do
        sleep $RELOAD_INTERVAL
        if [ $(date +%s) -lt $END_TIME ]; then
            echo "$(date): Performing make reload..."
            make reload > /dev/null 2>&1
        fi
    done
}

# Start the reload process in background
perform_reloads &
RELOAD_PID=$!

# Start the request sender in foreground (so we can count properly)
echo "Starting request sender..."
send_requests &
REQUEST_PID=$!

# Wait for the duration or until the request sender finishes
echo "Test running... (PID: $REQUEST_PID, $RELOAD_PID)"
wait $REQUEST_PID

# Kill reload process if it's still running
kill $RELOAD_PID 2>/dev/null
wait $RELOAD_PID 2>/dev/null

echo "----------------------------------------"
echo "Test completed at: $(date)"

# Wait a moment for any pending log writes
sleep 10

# Get final line count
FINAL_LINE_COUNT=$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0)
LINES_ADDED=$((FINAL_LINE_COUNT - INITIAL_LINE_COUNT))

# Count the number of requests from the temporary file
REQUEST_COUNT=$(wc -l < "$REQUEST_FILE" 2>/dev/null || echo 0)

echo "Final line count in log: $FINAL_LINE_COUNT"
echo "Lines added during test: $LINES_ADDED"
echo "Total curl requests sent: $REQUEST_COUNT"

# Clean up temporary file
rm -f "$REQUEST_FILE"

# Compare results
if [ $LINES_ADDED -eq $REQUEST_COUNT ]; then
    echo "✅ SUCCESS: Number of log lines ($LINES_ADDED) matches number of requests ($REQUEST_COUNT)"
    exit 0
else
    echo "❌ FAILURE: Number of log lines ($LINES_ADDED) does not match number of requests ($REQUEST_COUNT)"
    echo "Difference: $((REQUEST_COUNT - LINES_ADDED))"
    exit 1
fi

Test result

Fri Oct 31 03:28:05 PM IST 2025: Performing make reload...
Fri Oct 31 03:28:15 PM IST 2025: Performing make reload...
Fri Oct 31 03:28:26 PM IST 2025: Performing make reload...
Fri Oct 31 03:28:37 PM IST 2025: Performing make reload...
Sent 29079 requests
----------------------------------------
Test completed at: Fri Oct 31 03:28:42 PM IST 2025
Final line count in log: 29732
Lines added during test: 29079
Total curl requests sent: 29079
✅ SUCCESS: Number of log lines (29079) matches number of requests (29079)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant