Skip to content

Commit 2501142

Browse files
dakomSupeeerpower
authored andcommitted
concurrency test
1 parent 303c99d commit 2501142

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# This script is used to test the concurrency of the WAVS middleware mock deployer.
5+
# Make sure you have anvil running in the background and that the variables
6+
# below are set correctly before running the script.
7+
######### CONFIGURATION #########
8+
N=50 # Change this to how many wallets you want
9+
MIDDLEWARE_IMAGE="ghcr.io/lay3rlabs/wavs-middleware:0.5.0-beta.7"
10+
ANVIL_RPC_URL="http://localhost:8545"
11+
ANVIL_PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 # default anvil key
12+
################################
13+
14+
WALLETS=()
15+
TEMP_DIRS=()
16+
17+
# 1. Exit early with an error message if anvil is not running
18+
# We check it by a simple cast command to get gas price
19+
if ! cast gas-price --rpc-url "$ANVIL_RPC_URL" &>/dev/null; then
20+
echo "Anvil is not running on $ANVIL_RPC_URL. Please start it first."
21+
exit 1
22+
fi
23+
24+
# 1. Generate wallets
25+
echo "Generating $N random wallets..."
26+
for i in $(seq 1 $N); do
27+
WALLET=$(cast wallet new --json)
28+
ADDRESS=$(echo "$WALLET" | jq -r '.[0].address')
29+
PRIVATE_KEY=$(echo "$WALLET" | jq -r '.[0].private_key')
30+
31+
WALLETS+=("$PRIVATE_KEY")
32+
33+
echo "[$i] Wallet: $ADDRESS"
34+
echo "Funding..."
35+
36+
# 2. Fund wallet (serially)
37+
cast send --rpc-url "$ANVIL_RPC_URL" --private-key "$ANVIL_PRIVATE_KEY" "$ADDRESS" --value 1ether
38+
done
39+
40+
# 2. Create temp dirs
41+
for i in $(seq 1 $N); do
42+
TMP_DIR=$(mktemp -d -t nodes-$i-XXXXXXXX)
43+
TEMP_DIRS+=("$TMP_DIR")
44+
done
45+
46+
# 3. Run containers in parallel
47+
echo "Launching $N containers..."
48+
49+
PIDS=()
50+
for i in $(seq 0 $((N - 1))); do
51+
KEY="${WALLETS[$i]}"
52+
DIR="${TEMP_DIRS[$i]}"
53+
54+
echo "[$((i + 1))] Starting container"
55+
56+
docker run --rm --network host \
57+
-v "$DIR:/root/.nodes" \
58+
-e MOCK_DEPLOYER_KEY="$KEY" \
59+
-e MOCK_RPC_URL="$ANVIL_RPC_URL" \
60+
$MIDDLEWARE_IMAGE -m mock deploy &
61+
62+
PIDS+=($!)
63+
done
64+
65+
# Wait for all processes concurrently
66+
echo "Waiting for all containers to finish..."
67+
FAILED_CONTAINERS=()
68+
PROCESSED_PIDS=()
69+
70+
# Monitor all processes concurrently
71+
FINISHED_COUNT=0
72+
73+
while [ $FINISHED_COUNT -lt ${#PIDS[@]} ]; do
74+
# Check each process
75+
for i in "${!PIDS[@]}"; do
76+
PID=${PIDS[$i]}
77+
78+
# Skip if we already processed this PID
79+
if [[ " ${PROCESSED_PIDS[@]:-} " =~ " $PID " ]]; then
80+
continue
81+
fi
82+
83+
# Check if process is still running
84+
# `kill -0` checks if the process exists without actually sending the kill signal
85+
if ! kill -0 $PID 2>/dev/null; then
86+
echo "Container $((i+1)) (PID: $PID) is not running, checking exit status..."
87+
# Process finished, use non-blocking wait to get exit code
88+
if wait $PID 2>/dev/null || true; then
89+
# Check the actual exit status from the wait command
90+
EXIT_CODE=$?
91+
if [ $EXIT_CODE -eq 0 ]; then
92+
echo "Container $((i+1)) finished successfully"
93+
else
94+
echo "Container $((i+1)) failed with exit code $EXIT_CODE"
95+
FAILED_CONTAINERS+=($((i+1)))
96+
fi
97+
else
98+
echo "Container $((i+1)) finished successfully"
99+
fi
100+
101+
PROCESSED_PIDS+=($PID)
102+
FINISHED_COUNT=$((FINISHED_COUNT + 1))
103+
else
104+
echo "Container $((i+1)) (PID: $PID) is still running..."
105+
fi
106+
done
107+
108+
# Small sleep to avoid busy waiting
109+
sleep 1
110+
done
111+
112+
# Report results
113+
if [ ${#FAILED_CONTAINERS[@]} -eq 0 ]; then
114+
echo "All containers finished successfully."
115+
else
116+
echo "Failed containers: ${FAILED_CONTAINERS[*]}"
117+
exit 1
118+
fi

0 commit comments

Comments
 (0)