I first created this script to heavy test the web application hosted in elastic beanstalk.

Why python? Just for fun :)

Let’s begin

First, define a main function for the python script

1
2
3
4
5
6
7
8
9
10
11
12
13
import datetime
import time
def main():
for i in range(0, 20):
print datetime.datetime.now()
# http requests goes here
time.sleep(1) # every 1 second submit http requests
if __name__ == "__main__":
main()

Then create a function to perform http requests

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
...
import requests
import re
import random
def http_test():
# define a base url
base_url = 'http://example.com'
# Assumed that your web application need to login
url = base_url + '/auth/login'
payload = {
'username': 'root',
'password': 'root',
}
res = requests.post(url, data=payload)
# we need the session cookie
cookies = res.cookies
# generate sales report
url = base_url + '/reports/sales'
# need to pass the cookies in order to tell the server that I'm "that" person
# this is GET request, because need to get the CSRF token before can do a POST
res = requests.get(url, cookies=cookies)
html = res.text.encode('utf-8')
# the "pattern" is depends on how you construct your html page
pattern = 'name="csrf-token" content="(\w+)"'
matches = re.findall(pattern, html)
token = matches[0]
# POST to generate sales report
payload = {
'date': '2016-06-27',
'_token': token,
'sales_id': random.randint(1, 30),
}
res = requests.post(url, cookies=cookies, data=payload)
...

If you don’t understand why need the cookie, you can refer Session hijacking: facebook.com example.

Note that the above function http_test() actually perform POST login, GET sales report, POST sales report

Now in your main function, replace # http requests goes here with http_test()

Make the python script executable

1
$ chmod +x http_requests.py

if you run this script, means 1 client with 20 (for loop) x 3 requests, which equals to 60 requests one after one.

What if want to make it 100 concurrent connections?

Simply create another shell script, let’s name it run.sh

1
2
3
4
5
for i in {1..100}
do
echo "Requests ($i) begin"
./http_requests.py &
done

also make it executable

1
$ chmod +x run.sh

Then run it

1
$ ./run.sh

Now you can simulate 100 concurrent connections with each 60 requests.

You can download the scripts here:

References: