-
Notifications
You must be signed in to change notification settings - Fork 0
/
stressTest.sh
executable file
·111 lines (97 loc) · 2.14 KB
/
stressTest.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
#### Default Configuration
trap "kill 0" SIGINT
CONCURRENCY=1
REQUESTS=10
ADDRESS="http://35.224.147.78/pi/?random_limit=10000" # by changin the random_limit number you can set the dificulty of the test for the api
HTTP_METHOD=""
BODY=""
HEADERS=""
CURL_PARAMETERS=""
show_help() {
cat << EOF
Naive Stress Test with cURL.
Usage: ./stress-test.sh [-a ADDRESS] [-c CONCURRENCY] [-r REQUESTS] [-X HTTP_METHOD] [-d BODY] [-H HEADERS]
Params:
-a address to be tested.
Defaults to localhost:8080
-c concurrency: how many process to spawn
Defaults to 1
-r number of requests per process
Defaults to 10
-X http method
Defaults to GET
-d body
Defaults to NULL
-H headers
Defaults to NULL
-h show this help text
Example:
$ ./stressTest.sh -c 4 -r 100 (400 requests to localhost:8080)
EOF
}
#### CLI
while getopts ":a:c:r:h:X:d:H:" opt; do
case $opt in
a)
ADDRESS=$OPTARG
;;
c)
CONCURRENCY=$OPTARG
;;
r)
REQUESTS=$OPTARG
;;
X)
HTTP_METHOD=$OPTARG
CURL_PARAMETERS+=" $ADDRESS"
;;
d)
BODY=$OPTARG
CURL_PARAMETERS+=" -d '$BODY'"
;;
H)
HEADERS=$OPTARG
CURL_PARAMETERS+="--header '$HEADERS' "
;;
h)
show_help
exit 0
;;
\?)
show_help >&2
echo "Invalid argument: $OPTARG" &2
exit 1
;;
esac
done
shift $((OPTIND-1))
#### Main
if [ -n "$CURL_PARAMETERS" ]; then
if [ -z "$HTTP_METHOD" ]; then
CURL_PARAMETERS=" '$ADDRESS' $CURL_PARAMETERS"
fi
else
CURL_PARAMETERS="$ADDRESS"
fi
CURL_COMMAND="for n in {1..$REQUESTS}; do curl -s $CURL_PARAMETERS; done"
echo "curl command: $CURL_COMMAND"
for i in `seq 1 $CONCURRENCY`; do
if [[ "$CURL_COMMAND" == *"#INCREMENT_TIMESTAMP"* ]]; then
timestamp="$(date +%s%N)"
CURL_COMMAND="${CURL_COMMAND//#INCREMENT_TIMESTAMP/$timestamp}"
fi
eval "$CURL_COMMAND" & pidlist="$pidlist $!"
done
# Execute and wait
FAIL=0
for job in $pidlist; do
echo $job
wait $job || let "FAIL += 1"
done
# Verify if any failed
if [ "$FAIL" -eq 0 ]; then
echo "SUCCESS!"
else
echo "Failed Requests: ($FAIL)"
fi