10

I am currently writing python script for load testing an API.I want to check how many requests can an API take at a time.The API is for registration so I have to send unique parameters everytime.

Is there anyway I could achieve it through locust or any other way?

Any help would be appreciated.

This is my code for registration of single user.

def registration:
    URL = "ip"
    PARAMS = {'name':'test','password':'test1','primary_email':'test667@gmail.com','primary_mobile_number':'9999999999','country_abbrev':'US'} 
    r = requests.post(url = URL,params = PARAMS,auth=HTTPDigestAuth('user', 'pass')) 
    response = r.text 
    print response
dave Miller
  • 304
  • 1
  • 6
  • 16

4 Answers4

9

Take a look at Faker Python Package. This generates fake data for you whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.

from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
    def on_start(self):
        pass  # add code that you want to run during ramp up

    def on_stop(self):
        pass  # add code that you want to run during ramp down

    def registration(self):
        name = fake.first_name()
        last_name = fake.last_name()
        password = ''
        email = name + last_name + '@gmail.com'
        phone = fake.phone_number()
        URL = "ip"
        PARAMS = {'name':name,'password': password,'primary_email': email,'primary_mobile_number':phone,'country_abbrev':'US'} 
        self.client.post(URL, PARAMS)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

To start the load test, run locust -f locust_files/my_locust_file.py --host=http://example.com For more info, visit Locust Quickstart

GPT14
  • 809
  • 6
  • 13
  • Hi,I understand that it would generate fake data but wouldn't it send the request one after the other? In my case I want to send multiple requests together. – dave Miller Aug 27 '19 at 06:23
  • @daveMiller added more information to help you get started with load testing using locust – GPT14 Aug 27 '19 at 06:30
3
from locust import HttpLocust, TaskSet

def login(self):
    params= {'name':'test','password':'test1','primary_email':'test667@gmail.com','primary_mobile_number':'9999999999','country_abbrev':'US'}
    self.client.post(URL, data=params)
    #The data parameter or json can both be used here. If it's a dict then data would work but for json replace data with json. For more information you can check out requests package as Locust internally uses requests only.

class UserBehavior(TaskSet):
    tasks = {index: 2, profile: 1}

    def on_start(self):
        login(self)

    def on_stop(self):
        pass

    @task
    def try(self):
       pass

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

To start the load test, run locust -f locust_files/my_locust_file.py --host=http://example.com where host would be your IP. You can then go to 127.0.0.1:8089 to select the number of virtual users to simulate. On windows there's a limitation of 1024 users only. But you can use the amazing support of Master Slave Architecture provided by Locust.

PS: Anything put in the on_start method runs only once for each user. So since you want to test the limit of the API you should prefer adding that request under the @task decorator.

Hope this helps! :)

Apurva Singh
  • 411
  • 3
  • 14
0

There are different approaches, like:

  • Using random strings
  • Using a 3rd-party library like faker
  • List credentials in the same Python script
  • List credentials in an external data source like CSV file or Database

Unfortunately your question doesn't have explicit requirements of the "unique" parameters so for the time being I would suggest getting familiarized with How to Run Locust with Different Users article

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

Check out the medium link for load test with python with locust - An opensource load testing tool.

Raoof Naushad
  • 526
  • 1
  • 5
  • 7