-
Notifications
You must be signed in to change notification settings - Fork 0
/
siege.sh
211 lines (168 loc) Β· 5.51 KB
/
siege.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!bin/bash
# Bash script to run Siege for API load testing with a simple UI
# Check whether Siege is installed
if ! command -v siege &> /dev/null
then
echo "You don't have Siege installed or else it isn't in your PATH. Please make sure to fix this before running again."
exit 1
fi
echo -e 'This is a simple bash script for running Siege on API endpoints.
You will need to supply: \n
π An optional location to save test logs \n
π The concurrent number of requests to make \n
After that, you can either choose to run it in single-endpoint mode:
π An endpoint \n
π The verb, or type of request you are making \n
π The body of the request \n
Or you can read in a text file of endpoints and verbs for each request.
It has to be in this format - and if you need any authentication for those endpoints
in the form of query params, put them in there too:
# comments behind hashes
http://homer.whoohoo.com/cgi-bin/hello.pl POST name=homer
http://homer.whoohoo.com/haha.jsp POST word=doh!&scope=ALL
# To post the contents of a file, use the redirect character <:
http://www.haha.com/reader.php POST < /path/to/file
# If you need authentication AND post file contents, its best to put the params in
# URL directly instead of separating them out
'
# Logging enabled (optional)
echo -e "Enable logging? If so, logfile location will be outputted at the end of the test"
select choice in "Y" "N"; do
case $choice in
Y )
enable_logging="-l"
break;;
N )
enable_logging=""
break;;
*)
echo "Select either 1 or 2 to proceed";;
esac
done
# Mode functions
single_endpoint_mode() {
# API endpoint (to test)
echo -n 'Please enter your endpoint:'
# Check for valid URLs using an ultra-sophisticated regex
read -r endpoint
http_regex='(https|http)?://([^ ]+[.])+'
while ! [[ $endpoint =~ $http_regex ]] ; do
echo -n "Please enter a valid URL. Make sure to include the protocol prefix: "
read -r endpoint
done
# Verb (GET, POST etc.)
echo -n 'Please enter the HTTP verb:'
read verb
valid_verbs=(GET PATCH POST PUT DELETE)
uppercase_verb=$(echo "$verb" | tr a-z A-Z)
echo $uppercase_verb
while [[ ! " ${valid_verbs[@]} " =~ ${uppercase_verb} ]] ; do
echo -n 'Please enter a valid HTTP verb:'
read verb
done
# Body of the request in JSON format, e.g.
# {
# "coordinates":["11.180168326504514 43.92433128404457"],
# "auth_key":"O46KbWesN7FV5uGBVF8x",
# "auth_token":"o65avfsIU0YhcMGXfuqWk2TVg5ezJw",
# "type":"POINT",
# "buffer":"10000"
# } in oneline format
echo -n 'Please provide the body of the request you wish to pass:'
read -r body
# Concurrent number of requests
echo -n 'Please enter the concurrent number of requests you wish to make:'
# Check for concurrency being an integer
read concurrency
# TODO - fix
# number_regex='^[1-9]\d*$'
# while ! [[ $concurrency =~ $number_regex ]] ; do
# echo -n "Please enter an integer value greater than 0:"
# read concurrency
# done
# Run the Siege program
echo 'Running tests...'
eval "siege $enable_logging --concurrent=$concurrency --verbose --content-type='application/json' '$endpoint $uppercase_verb $body'"
}
file_read_mode() {
# File location
echo -n 'Please enter the absolute path to your file:'
read -r file_location
while ! [[ -f "$file_location" ]]; do
echo "File could not be found - did you provide the correct location?"
echo "Please try again: "
read -r file_location
done
# Concurrent number of requests
echo -n 'Please enter the concurrent number of requests you wish to make:'
# Check for concurrency being an integer
read concurrency
# TODO - fix
# number_regex='^[1-9]+$'
# while ! [[ $concurrency =~ $number_regex ]] ; do
# echo -n "Please enter an integer value greater than 0:"
# read concurrency
# done
echo -e 'Set internet mode on? The URLs in your file will be hit randomly instead of sequentially'
select choice in "Y" "N"; do
case $choice in
Y )
internet_mode="-i"
break;;
N )
internet_mode=""
break;;
*)
echo "Select either 1 or 2 to proceed";;
esac
done
echo -e 'Set delay?'
read delay_amount
if [ $delay_amount == "" ] ;
then
delay=""
else
delay="--delay=$delay_amount"
fi
echo -e 'Set reps?'
read reps_amount
if [ $reps_amount == "" ] ;
then
reps=""
else
reps="--reps=$reps_amount"
fi
# Run the Siege program
echo 'Running tests...'
eval "siege -f $file_location $reps $delay $internet_mode $enable_logging --concurrent=$concurrency --verbose --content-type='application/json'"
}
# There's two options to choose from - you can either provide a single endpoint or a file of endpoints to read from:
echo -e 'Please select from the following options:'
select selection in "Single endpoint mode" "File read mode" "Exit"; do
case $selection in
'Single endpoint mode' )
single_endpoint_mode
break;;
'File read mode' )
file_read_mode
break;;
'Exit' )
exit 0;;
*)
echo "Select a valid option";;
esac
done
echo 'Test done!'
exit 0
# TODO - fix so we can run the script again
# echo -e "Do you wish to run another test?"
# select restart in "Y" "N"; do
# case $restart in
# Y )
# ./$(basename $0) && exit;;
# N )
# exit 0;;
# *)
# echo "Select either 1 or 2 to proceed";;
# esac
# done