Locust: Probably the most underrated stress testing tool

01. Introduction to Locust

The open source performance testing tool https://www.locust.io/ , a Python-based performance stress testing tool, uses Python code to define user behavior and simulate millions of concurrent user visits . The behavior of each test user is defined by you, and the aggregation process is monitored in real time through the web UI.

As the core part of the performance testing tool, the pressure generator has two core points: one is to truly simulate user operations , and the other is to simulate effective concurrency

  • Simulate user request operation:
    as long as we can use Python to write the corresponding request client, we can easily use Locust to implement stress testing;
  • Simulate effective concurrency:
    Locust is completely event-based, using non-blocking IO and coroutine provided by gevent (coroutine) to implement concurrent requests at the network layer, avoiding system-level resource scheduling and greatly improving performance , so even a single press It can also generate thousands of concurrent requests.

Comparison of mainstream pressure measurement tools:

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

02. Basic principles

The main three classes: HttpLocust, TaskSet, task

The Locust class represents a group of locusts, and each locust is an instance of a class. The TaskSet class can represent the brain of the locust, that is, the task set corresponding to the actual business scenario test.

1. Locust class

A class inherited from Locust represents a user, and Locust will generate a locust class instance for each simulated user.

In the Locust class, there are several attributes that need attention:

Attributes meaning
client attribute The request capability that a virtual user possesses as a client is also known as the request method. For the HTTP(S) protocol, Locust has implemented the HttpLocust class. Its client attribute is bound to the HttpSession class, and HttpSession inherits from requests.Session. Therefore, in the Locust script for testing HTTP(S), we can use all methods of the Python requests library through the client attribute, including GET/POST/HEAD/PUT/DELETE/PATCH, etc., and the calling method is exactly the same as requests.
task_set Point to a TaskSet class, the TaskSet class defines the user's task information, this attribute is required
min_wait/max_wait The minimum and maximum time interval in ms that a simulated user will wait for each task to execute. The default setting is 1000. If not specified, Locust will wait 1 second between each task by default.
weight Multiple locust instances are added to a test case, and each locust instance executes the proportion. The larger the value, the higher the execution frequency.
host The host of the system under test, the prefix of the target URL to be loaded (such as: " http://google.com "), will be used when the --host parameter is not specified when starting locust in the terminal.

2. TaskSet class

TaskSet defines task information, which defines the set of task codes to be tested. Each Locust class contains a task_set attribute setting pointing to the TaskSet. After the test task starts, each Locust user will randomly select a task from the TaskSet to execute, and then randomly wait for a period of time between min_wait and max_wait defined in the HttpLocust class to execute the next task.

3、task

A method decorated with @task() is a transaction. The parameters of the method are used to specify the execution weight of the behavior. The larger the parameter, the higher the probability of being executed by a virtual user each time. Defaults to 1 if not set.

tasks = {classname:2} means that the frequency of each user executing classname is 2

4. Execution principle

After the test starts, the running logic of each virtual user (Locust instance) will follow the following rules:

  • First execute on_start in WebsiteTasks (execute if there is one, only once) as initialization;
  • Randomly select a task from WebsiteTasks (if the weight relationship between tasks is defined, then it is randomly selected according to the weight relationship) to execute;
  • According to the interval time range defined by min_wait and max_wait in the Locust class (if min_wait or max_wait is also defined in the TaskSet class, the one in the TaskSet takes precedence), randomly select a value in the time range, sleep and wait;
  • Repeat steps 2 to 3 until the test task is terminated.

03. Examples

example

V1.1 has some small changes, the principle is similar, QuickStartUser inherits HttpUser, defines the user behavior set.

The index() and search() methods access the Baidu homepage, and decorate the method with @task() as a task. 1 indicates the weight of a Locust instance being selected for execution. The larger the value, the higher the execution frequency.

operation result:

Startup performance:

locust -f demo_pt.py       #  --host=https://www.baidu.com   -f, --locustfile:指定执行的Locust脚本文件

Access: http://127.0.0.1:8089

  • Number of users to simulate: Set the number of simulated users
  • Hatch rate (users spawned/second): The number of users spawned (started) per second.

Display the result:

The following are supporting learning materials. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/132623751