This article is the second day of Brainpad Advent Calender 2019
Hello @ is nissy0409240 I'm an engineer at BrainPad It's been less than a month in 2019 How is everyone doing
I was able to go to Tokyo Dome privately this year It was a moving year Speaking of Tokyo Dome, baseball Speaking of baseball, pitchers and batters In this entry, regardless of such a preface I will introduce the basic functions of Locust, a load testing tool that allows you to write scenarios in Python.
Locust is a Python load testing tool that also has a GUI interface. The biggest feature compared to other load testing tools is that you can write scenarios in Python. It sounds like a play on words, but it also means that you have to write a scenario in Python.
Click here for various links such as official documents
By the way, Locust is an English word meaning grasshopper or locust.
The illustration is Migratory locust
I will install it immediately To install, just execute the following command
$ brew install libev
$ python3 -m pip install locustio
Execute the $ locust --help
command after execution, and if help is displayed, the installation is successful.
The illustration is a locust
The scenario is written in a Python script.
There are no restrictions on the file name itself.
However, if you do not specify the file name at startup, the one with the file name locustfile.py
will be executed.
If there are no file name restrictions, it is recommended to use the file name locustfile.py
.
Below is a sample
from locust import HttpLocust, TaskSet, task, between
class WebsiteTasks(TaskSet):
@task
def index(self):
self.client.get('/')
class WebsiteUser(HttpLocust):
task_set = WebsiteTasks
wait_time = between(5, 15)
By the way, the API server you receive is like this
from wsgiref.simple_server import make_server
import json
def api(environ, start_response):
status = '200 OK'
headers = [
('Content-type', 'application/json; charset=utf-8'),
('Access-Control-Allow-Origin', '*'),
]
start_response(status, headers)
return [json.dumps({'message':'hoge'}).encode("utf-8")]
with make_server('', 3000, api) as httpd:
print("Serving on port 3000...")
httpd.serve_forever()
The illustration is a grasshopper
Start with the following command
$ locust
[2019-12-01 00:56:13,884] locust.main: Starting web monitor at *:8089
[2019-12-01 00:56:13,885] locust.main: Starting Locust 0.9.0
In the above example, I'm not running with any options
File with the scenario you want to run with the -f option You can specify the endpoint you want to hit with the -H option
By default the Locust process starts on port 8089 It is possible to change the startup port with the -P option.
If you specify it properly Start like this
$ locust -f locustfile.py -H http://localhost:3000 -P 8089
[2019-12-02 03:38:42,717] locust.main: Starting web monitor at *:8089
[2019-12-02 03:38:42,718] locust.main: Starting Locust 0.13.2
The illustration is Inago no Tsukudani
When you access http: // localhost: 8089
from your browser after startup
You can access the following GUI
Since there are setting items that are blank, enter the following values 100 to "Number of users to simulate" 1 for "Hatch rate"
The meaning of each is as follows
Number of users to simulate:How many clients to create(Synonymous with how many requests are sent per second at the maximum)
Hatch rate:Client creation speed(Every second)
Host:Destination endpoint
This time, send a request to http: // localhost: 3000
, increasing by 1 client every second.
At this time, the maximum number of clients will be increased to 100.
Execute under the condition
After inputting, press the "Start Swarming" button to execute the request
If you want to stop the execution, press the STOP button on the upper right of the browser After pressing, STATUS becomes STOPPED, and you can confirm that it is finished.
The illustration is a horde of grasshoppers and locusts
You can also see the metrics on the Charts tab This is a metric when you increase by 1 client every second, and if you increase up to 100 clients, continue to request with 100 clients as it is
Next, let's see what happens when the request fails. This time I will try to stop the API server process in the middle
You can check like this on each tab How to increase the number of failed requests and the ratio of failures to the total number of requests You can also see why it failed on the Failures tab.You can also download the results from the Downloads Data tab.
The contents of the downloaded file are like this
The illustration is a grasshopper character
As mentioned above, I have introduced Locust, although it has only basic functions. Farewell with the batter's illustration Thank you for staying with us until the end
https://qiita.com/yamionp/items/17ffcc465272ad83c490 https://inside.pixiv.blog/east/5407 https://blog.htmlhifive.com/2015/08/14/web-server-load-testing-tools-2-locust/ https://docs.locust.io/en/stable/installation.html https://docs.locust.io/en/stable/quickstart.html https://co3k.org/blog/load-test-by-locust
Recommended Posts