python+locust to achieve simple performance testing


Locust performance test tool

Features of performance test tools




Install Locust

Performance test cases

Locust performance test tool


Features of performance test tools



loadrunner is a well-known commercial paid performance testing tool, which is very powerful and complex to use. At present, most books on performance testing are based on this tool, and even some books introduce the use of loadrunner as a whole



JMeter is also a famous open source performance testing tool with perfect functions. Most people use it for interface testing and performance testing



locust is also a performance testing tool, but it is quite different from the previous two tools. Compared with the previous two tools, it is much worse in function, but it is not without advantages;

Locust is completely based on Python to become a language, pure python is used to describe the test script, and http requests are completely based on requests library. In addition to the HTTP/HTTP protocol, locust can also test the system of other protocols, just use Python to call the corresponding library for request description;

loadrunner and jmeter use process and thread testing tools, which are difficult to simulate high concurrency pressure on a single machine. The concurrency mechanism of lockust abandons processes and threads and adopts the mechanism of gevent. Cooperative process avoids system level resource scheduling, so it can greatly improve the concurrency of single machine;

For this reason, we chose locust for performance testing. Another reason is that it allows us to understand performance testing in another way, which makes it easier to see the essence of performance testing.


Install Locust

     1. Although lockust can still be installed using pip, if you are Python 3, it is recommended to install from GitHub clone project:

     2. When we install lockust, we will check whether some dependent libraries are installed in our python environment. If not, they will be installed automatically, and there are requirements for the versions of these libraries, some are equal to a certain version, and some are greater than a certain version. We can install them all according to the requirements, so that we can install locust much faster.

The following are the main uses of these libraries:

Gevent: a third-party library that implements collaborative processes in python. Coroutine, also known as micro thread. High concurrency performance can be achieved with gevent

Flash: a lightweight web backend framework for python

requests: the third-party library used to request interfaces

Msgpack Python: a fast and compact binary serialization format suitable for JSON like data

six: provides some simple tools to encapsulate the differences between python2 and python3

pyzmq: if you plan to run locust on multiple processes / machines, it is recommended to install this

     3. Verify the installation through lockust -- help. Note that if there are errors between version numbers here, you can refer to my version number. I'm Python 3 Version 6. Of course, not all of the following packages need to be downloaded. If you report a version error in one of your locust packages, you can refer to and modify my version number. For example, jinja2 and locust have versions corresponding to each other



Performance test cases

     1. Create a py file for presentation

If importerror appears: the httplocate class has been renamed to HttpUser in version 1.0 An error is reported. The class HttpLocust has been renamed as HttpUser in version 1.0

          task_set is deprecated since version 1.0_ Set = userbehavior changed to task_create=UserBehavior, because instantiation is not supported after version 1.0

use the @task decorator or set the tasks property of the User_ Change create to tasks setting

from locust import HttpUser, TaskSet, task, between

# Define user behavior classes
class UserBehavior(TaskSet):

    @task#Task item
    def baidu_page(self):
        res = self.client.get("/")

class WebsiteUser(HttpUser):

    tasks = [UserBehavior]
    # min_wait=3000
    # max_wait=6000
    wait_time = between(3, 7)

if __name__ == '__main__':
    import os
    os.system("locust -f --host=")
    #-f specify the file
    #--host specifies the url to be tested

     2. Start lockust:

     4. adopt http://localhost:8088/ Access, Number of total users to simulate, set the number of simulated users, and the number of virtual users started per second by the span rate (users spawned/second)

     5. Enter the number of users and the number of users started per second. Set 1 here to access Baidu; The performance parameters are as follows:

Type: request type, such as Get/Post

Name: request path

Requests: current number of requests

Failures: number of failed requests

Median: the median value is milliseconds. Half of the server responses are lower than this value and half are higher than this value

90%: 90% request response time

Average: the average value, in milliseconds, and the average response time of all requests

Min: minimum response time of the requested server

Max: maximum response time of the requested server

Average size: the size of a single request, in bytes

RPS: number of requests that can be processed per second

     15. Some commonly used instruction parameters of lockust

-h, --help           
view help
-H HOST, --host=HOST 
Domain name of the server under test
locust Service web Interface
-P PORT, --port=PORT, --web-port=PORT
 appoint –web-host The default port is 8089
Script path. You can write relative paths or absolute paths. If the script is in the current directory, write the relative path. If not, write the Jedi path.
--csv=CSVFILEBASE, --csv-base-name=CSVFILEBASE
 with CSV Store the current requested test data in the format, csv File storage current directory
 Mark which host is used for pressure measurement. The host is only used for statistics, not for pressure. The task of pressure is left to slave Extension. If you want to put pressure on the host, you should also start one on the host slave. 
 When doing distributed pressure measurement, mark which are used as extensions. The main task of the extension is to exert pressure.
 When doing distributed pressure measurement, specify the of the host IP. Only for slave. If not specified, the default is "127".0.0.1". 
 When doing distributed pressure measurement, specify the of the host port. Only for slave. If it is not specified and the host is not modified, the default is 5557.
When doing distributed pressure measurement, specify the extension IP. Only for master. If not specified, the default is all available IP(All tagged hosts IP of slave)
 When doing distributed pressure measurement, specify the extension port. The default is 5557 and 5558.
set number of seconds before failed heartbeat from slave
set number of seconds delay between slave heartbeats to master
 How many secondary hosts need to be connected before starting the test(only --no-web use).
no-web Specify mode, requirements-c and-r.
 Number of concurrent users. Only used togetherwith --no-web
-r HATCH_RATE, --hatch-rate=HATCH_RATE
 The per second rate of users generated. and--no-web Use together
-t RUN_TIME, --run-time=RUN_TIME
 Stop after specified time, as (300s,20m, 3h, 1h30m, etc.). and--no-web Use together
The path to the log file. If not set, the log will go to stdout/stderr
Print statistics in the console
Print summary statistics only
 [DEPRECATED] Do not reset statistics once hatching has been completed. This is now the default behavior. See --reset-stats to disable
Reset statistics once hatching has been completed. Should be set on both master and slaves when running in distributed mode
-l, --list 
Show possible locust Class list and exit
 Print locust Class task execution rate table
 Print locust Implementation rate of class tasks JSON data
-V, --version       
locust edition


Well, locust is here first, because it's not a professional to do this. It's just to record a learning process. There's time later. I'll give a tutorial on the web, mobile UI and interface automation, PO framework, data-driven keywords, underlying code encapsulation and distributed multithreading, as well as the complete scheme of docking test platform that has been implemented, Continuous update maintenance

Added by stormszero on Wed, 09 Feb 2022 15:43:31 +0200