Wednesday, December 30, 2015

Sending concurrent requests

Many times the testing demands for testing for concurrency which is a very common case in today's scenario of website application development. The challenge for the testers is mimic the actual concurrent scenario manually, which is nearly impossible by manual effort. However, this could be done by using many testing tools or by use of some robust scripting language like Python (as used in the following snippet) to generate concurrent request for the particular URL/API/UI endpoint using the basic authentication.


Pre-requisite

Install python

Then install grequests by the following command

$ pip install grequests


#/home/python/bin/

import grequests
import itertools
from requests.auth import HTTPBasicAuth


username = 'username or email' #Updated this field with required username
password = 'password' #Updated this field with required password


#Updated the following URL with the one needs to be concurrently tested.
reqs = 'URL/API/UI endpoint where concurrent requests need to be sent'

# below 20 represent the count of concurrent requests and 'grequests' support around 2K concurrent request
urls = itertools.repeat(reqs, 20)

def sendConcurrentRequest():
     rs = [grequests.get(u, auth=(username, password)) for u in urls]
     print len(rs)
     

     xs = grequests.map(rs)
     print xs


if __name__ == "__main__":
sendConcurrentRequest()