When developing a Web system, I think it is necessary to know in advance not only whether it behaves as expected, but also how much load it can withstand.
This time, I would like to build Locust, a load test tool, on Docker and try it first.
The client PC will be Windows 10 Pro.
The official documents are as follows.
Locust Official Document Locust github
Get the docker-compose.yml file Create a folder in any location and download the docker-compose.yml file from the following. https://github.com/locustio/locust/tree/master/examples/docker-compose
Change the -H part of the master command to the page you want to test.
version: "3"
services:
web:
build:
context: ./apache-php
ports:
- 80:80
privileged: true
links:
- db
volumes:
- "./lara-d/:/var/www/html"
- "./apache-php/apache.conf:/etc/httpd/conf/httpd.conf"
container_name: "apache-php"
version: '3'
services:
master:
image: locustio/locust
ports:
- "8089:8089"
volumes:
- ./:/mnt/locust
command: -f /mnt/locust/locustfile.py --master -H http://master:8089
worker:
image: locustio/locust
volumes:
- ./:/mnt/locust
command: -f /mnt/locust/locustfile.py --worker --master-host master
This time, we will create a script to make a simple HTTP request.
from locust import HttpUser, task, between
class MyUser(HttpUser):
wait_time = between(5, 10) #Specify the timing to launch user
@task(1)
def index(self):
url = '/api/test.php' #Specify the place to apply the load
params = {'id':'0001'} #Specify parameters as needed
headers = {'x-api-key':'***********'} #Add header items as needed
self.client.get(url=url,params=params,headers=headers) #Send GET request
Open a command prompt, change to the folder where you placed docker-compose.yml, and execute the following command.
docker-compose up -d
If you want to increase the number of workers and load them in parallel, specify them at startup. I think it's okay to write it in an yml file.
docker-compose up -d --scale worker=3
Enter the number of users and the speed at which the users will start up, and press Start to execute. (The host will be the one set in yml)
Recommended Posts