Locust Performance Test Learning Summary

Source: Internet
Author: User
Tags install homebrew writing test scripts

Locust Learning Summary Sharing

Brief introduction:

Locust is a scalable, distributed, performance tested, open source, writing framework/tool in Python, it's very easy to use and very studious. Its main idea is to simulate a group of users who will visit your website. Each user's behavior is defined by the Python code you write, and the user's behavior can be observed in real time from the Web interface.

The Locust is fully event-driven, so it can support thousands of concurrent user access on a single machine. Compared to many other event-based applications, Locust does not use callbacks, but instead uses gevent, and Gevent is based on a co-process and can write code that executes asynchronously in a synchronous manner. Each user is actually running in their own greenlet.

Characteristics:

① writing test scripts in the simple Python language is very simple and lightweight. There is no need for cumbersome UIs and bloated XML code, based on synergies rather than callbacks.

② is distributed, scalable, and can simulate millions of users. Locust supports multi-machine performance testing, each machine can simulate thousands of users, of course, this can be controlled,

③locust has a neat html+js user interface that displays relevant test details in real time. Because the user interface is Web-based, it is cross-platform and easy to scale.

④ can test any system, although Locust is site-based, but it can test almost any system as soon as you write a client.

Installation:

Locust can be installed via PIP or Easy_install:

Pip Install Locustio

Or

Easy_install Locustio

After installing locust, the shell command locust is ready to use, and you can see what options are available for the Locust command:

(note, to ensure that you have installed Python, the version must be more than 2.6, but not 3.0 version above, more than 3.0 of the changes are very large, not compatible)

Locust--help

If you plan to run locust in distributed mode, we recommend that you install PYZMQ (a communication queue) at the same time:

Pip Install PYZMQ

Or

Easy_install PYZMQ

Install on Windows:

Download the binary installation package, and then follow the prompts to install

Website: http://www.lfd.uci.edu/~gohlke/pythonlibs/

It is important to note that when large-scale testing is required, the performance of the installation on Linux is better than on Windows.

Install on an Apple computer:

① First install Homebrew

② installation Libevent

Brew Install Libevent

③ refer to the installation process on Linux.

Increase the maximum number of open file limits:

Open a new file (technical file descriptor) on each HTTP connected machine. The operating system can set a lower limit for the maximum number of files that can be opened. If the limit is less than the number of impersonated users, a failure occurs during the test. Increase the default maximum number of files for the operating system to limit the number of users to a number above the number of Sims to achieve the test you want, and refer to the operating system of this computer.

Quick Start:

Here is a simple sample, keep it as a python file, the file name is arbitrary

From locust import Httplocust, TaskSet

def login (L):

L.client.post ("/login", {"username": "Ellen_key", "Password": "Education"})

def index (L):

L.client.get ("/")

Def profile (L):

L.client.get ("/profile")

Class Userbehavior (TaskSet):

tasks = {index:2, profile:1}

def on_start (self):

Login (self)

Class Websiteuser (Httplocust):

Task_set = Userbehavior

min_wait=5000

max_wait=9000

This few lines of code is just a completed test script. Defines two classes, a user behavior class, inherits the Taskset class, defines the test task, adds two tasks to the properties tasks, the index function and the profile function, the characters are executed, and then returns the execution time, normally is between the minimum and maximum time below, starting with On_Start, like the main function of Java, is the task opening, then randomly picking the task and executing the HTTP request via the client (equivalent to a httpsession) method. However, it is performed at a set rate. The Tasks property turns the function defined above into a task, which is a dict type. Corresponds to the Java map type.

A Websiteuser class that inherits the Httplocust class, which is used to represent the user, to generate an instance for each impersonated user, to send an HTTP request and to set the test parameters, the Task_set property, which is the only must have, it points to the Task set class, Defines the user's behavior, the request waits the minimum time min_wait and the request waits the maximum time max_wait property, in milliseconds. , the Weight property specifies the rate at which the user executes, and the host property is the URL of the app being tested.

Note the maximum time and minimum time properties can be defined in the Locust class or in the task set class, exactly the same

The following is a simpler way to define a task, using the @task constructor. The following code is the same as the above: But this is the sequence of tasks, the first is the random selection of tasks

From locust import httplocust, TaskSet, task

Class Userbehavior (TaskSet):

def on_start (self):

"" "On_Start is called if a locust start before any task is scheduled" ""

Self.login ()

def login (self):

Self.client.post ("/login", {"username": "Ellen_key", "Password": "Education"})

@task (2)

def index (self):

Self.client.get ("/")

@task (1)

def profile (self):

Self.client.get ("/profile")

Class Websiteuser (Httplocust):

Task_set = Userbehavior

min_wait=5000

max_wait=9000

Taskset can also be nested: Refer to the following code

The first type:

Class Forumpage (TaskSet):

@task (20)

def read_thread (self):

Pass

@task (1)

def new_thread (self):

Pass

@task (5)

def stop (self):

Self.interrupt ()

Class Userbehaviour (TaskSet):

tasks = {Forumpage:10}

@task

def index (self):

Pass

The first thing to note is that the interrupt function, if not the function locust will always perform formpage this task, only through this function, can jump out. Executes a function other than FormPage.

The second type:

Class Mytaskset (TaskSet):

@task

Class Subtaskset (TaskSet):

@task

def my_task (self):

Pass

Run Locust:

Locust-f. /locust_files/the file name above. PY--host=http://example.com

Code holds the name and application of the address host

Distributed multi-processor locust operation:

The main processor, which is responsible for distributing tasks

Locust-f. /locust_files/the file name above. PY--master--host=http://example.com

--master-port=8888 (default is port 8080)

From the processor, responsible for executing the code script

Llocust-f. /locust_files/the file name above. py--slave--master-host=192.168.0.100--host=http://example.com--master-bind-host=8888

Open the Locust Web interface

http://127.0.0.1:8089

Because the example is local, the IP is also local

You need to enter how many users to impersonate and how many users to start per second, which is the number of concurrent numbers.

Locust Performance Test Learning Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.