Skip to content

Commit d0d7cd0

Browse files
committed
Add domain_socket_stress_test.sh
1 parent 98d2bad commit d0d7cd0

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

domain_socket_stress_test.sh

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
3+
BYTE_LENGTH="$1"
4+
SERVER_MODE="${2:-nc-save}"
5+
CLIENT_MODE="${3:-nc}"
6+
7+
TEST_FILE_PATH="/tmp/domain_socket_test_file"
8+
SOCKET_PATH="/tmp/domain_socket_test.sck"
9+
SERVER_RESULT_PATH="$TEST_FILE_PATH.server"
10+
CLIENT_RESULT_PATH="$TEST_FILE_PATH.client"
11+
dd if=/dev/urandom of="$TEST_FILE_PATH" bs=1 count="$BYTE_LENGTH" 2>/dev/null
12+
13+
if [[ "$SERVER_MODE" == *"-save" ]]; then
14+
TEST_RESULT_PATH="$SERVER_RESULT_PATH"
15+
else
16+
TEST_RESULT_PATH="$CLIENT_RESULT_PATH"
17+
fi
18+
19+
test_socket() {
20+
# Start a process to consume the data from the socket
21+
if [[ "$SERVER_MODE" == "nc-save" ]]; then
22+
(nc -l -U "$SOCKET_PATH" > "$SERVER_RESULT_PATH" && echo "Completed saving random bytes from the socket") &
23+
elif [[ "$SERVER_MODE" == "ncat-save" ]]; then
24+
(ncat -l -U "$SOCKET_PATH" > "$SERVER_RESULT_PATH" && echo "Completed saving random bytes from the socket") &
25+
elif [[ "$SERVER_MODE" == "socat-save" ]]; then
26+
(socat UNIX-LISTEN:"$SOCKET_PATH" - > "$SERVER_RESULT_PATH" && echo "Completed saving random bytes from the socket") &
27+
elif [[ "$SERVER_MODE" == "socat-echo" ]]; then
28+
(socat UNIX-LISTEN:"$SOCKET_PATH" EXEC:"/bin/cat" && echo "Completed echoing random bytes from the socket") &
29+
else
30+
echo "Invalid server mode: $SERVER_MODE"
31+
exit 1
32+
fi
33+
SERVER_PID=$!
34+
echo "Starting the server (PID: $SERVER_PID)"
35+
36+
# Wait for the socket file to be created so that the server has started
37+
while [ ! -e "$SOCKET_PATH" ]; do
38+
sleep 0.001
39+
done
40+
echo "The server has started and is listening to the socket (PID: $SERVER_PID)"
41+
42+
# `nc` can fail even if we wait for another second to ensure the server has started
43+
# sleep 1
44+
45+
# Start dumping random bytes to the socket in the background
46+
if [[ "$CLIENT_MODE" == "nc" ]]; then
47+
(nc -U "$SOCKET_PATH" < "$TEST_FILE_PATH" > "$CLIENT_RESULT_PATH" && echo "Completed dumping random bytes to the socket") &
48+
elif [[ "$CLIENT_MODE" == "ncat" ]]; then
49+
(ncat -U "$SOCKET_PATH" < "$TEST_FILE_PATH" > "$CLIENT_RESULT_PATH" && echo "Completed dumping random bytes to the socket") &
50+
elif [[ "$CLIENT_MODE" == "socat" ]]; then
51+
(socat - UNIX-CONNECT:"$SOCKET_PATH" < "$TEST_FILE_PATH" > "$CLIENT_RESULT_PATH" && echo "Completed dumping random bytes to the socket") &
52+
else
53+
echo "Invalid client mode: $CLIENT_MODE"
54+
exit 1
55+
fi
56+
CLIENT_PID=$!
57+
echo "Started dumping random bytes to the socket (PID: $CLIENT_PID)"
58+
59+
# Ensure the client process is killed
60+
wait $CLIENT_PID 2>/dev/null
61+
echo "The client process has stopped (PID: $CLIENT_PID)"
62+
63+
# Ensure the server process is killed
64+
wait $SERVER_PID 2>/dev/null
65+
echo "The server process has stopped (PID: $SERVER_PID)"
66+
67+
# Check the size of the data read from the socket
68+
DATA_SIZE=$(wc -c < "$TEST_RESULT_PATH")
69+
if [ "$DATA_SIZE" -ne "$BYTE_LENGTH" ]; then
70+
echo "Error: Expected $BYTE_LENGTH bytes, but read $DATA_SIZE bytes"
71+
exit 1
72+
else
73+
echo "Successfully read $BYTE_LENGTH bytes from the socket"
74+
fi
75+
76+
rm -f "$SOCKET_PATH"
77+
rm -f "$SERVER_RESULT_PATH"
78+
rm -f "$CLIENT_RESULT_PATH"
79+
}
80+
81+
rm -f "$SOCKET_PATH"
82+
83+
# Repeat the process
84+
counter=0;
85+
while test_socket; do
86+
((counter++)); echo "Iterations completed: $counter";
87+
done
88+
echo "Command failed after $counter successful iterations."

0 commit comments

Comments
 (0)