Stress Test with Locust written in Python

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 image.png 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.

What is Locust

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.

image.png The illustration is Migratory locust

Installation method

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.

image.png The illustration is a locust

Create a scenario

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()

image.png The illustration is a grasshopper

Start-up

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

image.png The illustration is Inago no Tsukudani

Test run

When you access http: // localhost: 8089 from your browser after startup You can access the following GUI

スクリーンショット 2019-12-02 3.39.14.png The endpoint you want to hit that you casually specified earlier is set by default スクリーンショット 2019-12-02 3.08.26.png * If Host is not specified at startup, Host will be blank.

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

スクリーンショット 2019-12-02 23.36.11.png

After inputting, press the "Start Swarming" button to execute the request

スクリーンショット 2019-12-02 21.40.38.png

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.

スクリーンショット 2019-12-02 21.42.49.png

image.png The illustration is a horde of grasshoppers and locusts

Metrics

You can also see the metrics on the Charts tab スクリーンショット 2019-12-02 22.00.39.png スクリーンショット 2019-12-02 22.01.34.png 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

スクリーンショット 2019-12-02 22.44.48.png スクリーンショット 2019-12-02 22.43.59.png スクリーンショット 2019-12-02 22.44.18.png スクリーンショット 2019-12-02 22.44.38.png 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. スクリーンショット 2019-12-02 23.23.27.png

The contents of the downloaded file are like this スクリーンショット 2019-12-02 23.24.55.png

image.png The illustration is a grasshopper character

Conclusion

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

image.png

reference

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

Stress Test with Locust written in Python
Post Test 3 (Working with PosgreSQL in Python)
Primality test with Python
Primality test with python
Scraping with selenium in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Algorithm in Python (primality test)
Debugging with pdb in Python
Working with sounds in Python
Scraping with Selenium in Python
Axisymmetric stress analysis in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Load test Websocket with Locust
Gacha written in Python -BOX gacha-
Set python test in jenkins
Design and test Verilog in Python only with Veriloggen and cocotb.
Squid Lisp written in Python: Hy
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Compatibility diagnosis program written in python
Use rospy with virtualenv in Python3
Unit test log output with python
Write selenium test code in python
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Statistical test (multiple test) in Python: scikit_posthocs
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Simple gacha logic written in Python
Get started with Python in Blender
Working with DICOM images in Python
Convert files written in python etc. to pdf with syntax highlighting
Prepare a distributed load test environment with the Python load test tool Locust
Python script written in PyTorch is converted to exe with PyInstaller
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
Fourier series verification code written in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try logging in to qiita with Python
Test Python non-functional programs with GitLab CI
Python3> in keyword> True with partial match?
Exclusive control with lock file in Python
Device monitoring with On-box Python in IOS-XE
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Write the test in a python docstring
Try working with binary data in Python
Generate Japanese test data with Python faker
Draw Nozomi Sasaki in Excel with python
Markov chain transition probability written in Python
Tips for dealing with binaries in Python
2D FEM stress analysis program with Python