Assume API performance is one of your concerns, you may want to use some tools to run load testing against your APIs. In our scenario, we use Locust to run load testing against API Management. Locust is an open source load testing tool and you write your load tests in Python. In the following sections, we will first introduce how to write Locust tests and then we will show you how to run Locust tests locally and remotely via AKS.
To write a Locust test, you need to define two classes:
- A subclass of TaskSet
- A subclass of Locust. In our case, we use
FastHttpLocust
for better performance.
Let's take a look at load test against Echo API as example. When you create API Management, Echo API is created by default as a simple API example.
def Echo(self):
return self.client.get(
"echo/resource?param1=sample",
headers = {
"Ocp-Apim-Subscription-Key": Variables.sub_key
},
catch_response = True
)
class EchoTaskSet(TaskSet):
@task
def EchoTask(self):
with Echo(self) as response:
if response.status_code != 200:
response.failure(f"Code: {response.status_code} | Content: {response.text} | Task: EchoTask")
class EchoLocust(FastHttpLocust):
wait_time = Variables.wait_time
task_set = EchoTaskSet
There are three parts in the above code snippet.
Echo
function calls the Echo API in API Management and returns the response.EchoTaskSet
class defines the task set. It calls theEcho
function and checks the response. It will record the error if the returned HTTP status code is not200
.EchoLocust
class defines what Locust will attack. In this case, all Locusts will do theEchoTaskSet
and they will wait forwait_time
seconds between two tasks. Usually thewait_time
can be randomed in a range.
However, Echo API is not our target. In API Management, we implemented Patient API with several API operations e.g., patient creation, patient search, etc. and they are our load test target. So in our case we've written several Locust tests and some important files are listed below.
- variables.py defines some variables like
wait_time
,sub_key
(API Management subscription key) and so on. - locustfile.py defines all
TaskSet
andLocust
classes. We defined oneLocust
class for eachTaskSet
which enables users to run eachTaskSet
separately. In addition, there is oneLocust
class calledAllLocust
which contains allTaskSet
. - tasks.py defines the
Locust
class you want to use to run the load test. For example, usefrom locustscripts.locustfile import AllLocust
if you want to test all APIs. If you just want to test patient creation, then just usefrom locustscripts.locustfile import CreatePatientLocust
instead.
After writing Locust tests, you can first run those tests locally to check if your Locust tests work or not.
Install Locust with pip:
pip install locustio
- Assign your API Management subscription key to
sub_key
in variables.py - Go to LoadTest folder and run the following command.
locust -f tests/tasks.py
- Open your browser and go to localhost:8089. You need to input three things.
- Number of total users
- Hatch rate
- Host, in our case it should be the hostname of API Management
If everything works fine, you should be able to see the similar screenshot.
Although it's very easy to run Locust tests locally, your local machine may not have enough power to simulate huge amounts of users. In that case, you can run Locust tests remotely via AKS. The idea is that you deploy your Locust tests in AKS containers and attack the API Management. Since you use AKS, you have the full control of the infrastructure like how many nodes to provision, how many pods (Locust workers) you want to spin up, etc.
- Bash (to run script)
- Azure CLI latest version
- kubectl, you can use
az aks install-cli
to install it after you install Azure CLI - Helm 3
- Assign your API Management subscription key to
sub_key
in variables.py - Assign following variables in azure_cli_deploy_and_run_tests.sh
LOCUST_TARGET_HOST
: the hostname of API ManagementCLIENT_ID
andCLIENT_SECRET
: the service principal for AKS authenticationLOCATION
: the region for AKS and storage accountRESOURCE_GROUP
: the resource group for AKS and storage account
- Since all scripts are in
azure_cli_deploy_and_run_tests.sh
, just run this bash to run tests in AKS cluster. After running Locust tests, all resources will be deleted automatically. - Check the test results like sample test results
Below diagram shows how the azure_cli_deploy_and_run_tests.sh
script works.