Why and how to do API performance testing.
Views:1046 |
By:
Kim
There are several types of testing you can do on an API, they include;
# Functional - These kind of testing answers whether your API does what it is meant to do.
# Security - To reveal the APIs vulnerabilities that can be exploited to gain access or manipulate it in any way.
# Reliability - To reveal whether your API responds appropriately during correct/ incorrect requests over time.
# Performance - To reveal how your API handles load, stress and its consistency over expected traffic over a given time span.
These tests should be continuous to enable you profile status, assess the threats and manage the risk that your application may be exposed to through those APIs.
If you ever wonder whether your APIs can handle millions of requests at a go, then you have been thinking about performance. Here is a way to find out in time and do the necessary changes to efficiently handle and accommodate the level of traffic you foresee.
#LOAD TESTING YOUR API USING LOCUST.
Locust enables you do define user behavior using python, in a few lines of code, you are able to swarm your API with as many requests and access the results on a dashboard. Assuming you have an API that submits user registration, this is how short your python code will be;
from locust import HttpUser, TaskSet, task, between
import os
import random
# Swarming Guide
# Strat terminal, enter - bash
# Get token from your app
# Enter - env TOKEN=19ad2d9be2d6b230df8cd5d23e5afefa46ac4326 locust --host=http://app/api
# Once the locust server is running, navigate to the web monitor at [127.0.0.1:8089](http://127.0.0.1:8089) to configure the swarm parameters and start swarming.
class AppBehaviour(TaskSet):
headers = {
'Authorization': 'Token {0}'.format(os.environ.get('TOKEN'))
}
# Generate random test data
phone_number = '+254721%06d' % random.randint(1, 999999)
# Group test data
user_data = {
"name": "test name",
"gender": "Female",
"phone_number": phone_number,
"County": "Farmland"
}
# Package data for submission
data = {
"user": user_data,
}
@task(1)
def signup_activity(self):
self.client.post('/signUp/', self.data, headers=self.headers)
class AppUser(HttpUser):
task_set = AppBehaviour
min_wait = 1000
max_wait = 2000
stop_timeout = 10
Find out how to set up locust here:
https://locust.io/