Python-based performance testing tool –locust

Source: Internet
Author: User
Tags python script

Now there are a lot of performance testing tools, such as our familiar LoadRunner, JMeter, AB, Webbench, and so on, these tools if to a useless friend, learning is not easy, but if you can read the Python code, will write better, You can try today's protagonist Locust, a python-based performance testing tool, its advantages are relatively simple to learn, fully customizable, flexible use, support distributed.

All performance testing tools contain at least these 3 blocks:

1, pressure generator, that is, you can specify how much pressure to produce, how much concurrency;

2, data statistics, that is, the display of the results, to statistics TPS is how much, response time, and so on, these data;

3, Agent function, the function of the agent is plainly a word, share the pressure. For example, you need to use 1000 when the pressure test concurrency, but your computer (press) configuration is weaker, only support 500 concurrency, and then the big computer died, finished Duzi, pressure can not be measured. What to do, you have to share the pressure, and then your colleagues to borrow a computer, two computer pressure, then you can, everyone's computer on 500 concurrent, more people work well, agent is to do this, the above pressure spread to other computers.

Of course locust these 3 functions are there.

Locust is a third-party module in Python, easy to install, direct pip install locust , or download the installation package yourself and install it manually. After the installation is complete, there is a locust command, enter Locust--help in the command line, the Help information is installed successfully. Such as:

Locust's official website is Www.locust.io, there are also how to install and examples.

How to use it, very simple, just a few lines of code can be implemented, and have a beautiful web interface, you can set the number of concurrent, and view the results, first we write a few lines of code, write a script to open the Besttest home page.

12345678910111213141516 from locust import httplocust, TaskSet, task #HttpLocust The purpose of this class is to send HTTP requests to the#TaskSet This class is a script that defines the behavior of the user, equivalent to the Loadrunnerhttp protocol, jmeter the HTTP request inside, what to do#task This task is an adorner, which is used to decorate a function as a task or to specify their order of executionclass besttest(TaskSet): #自己定义的类, Inherit Taskset, which is the class that implements what we're going to ask for. @task#用task装饰器把这个函数装饰成一个咱们要执行的性能任务 def index(self):#这个函数里面定义的是咱们要具体做的操作 Self . Client. Get('/')#请求这个url里面的哪个路径, if it is an interface, which interface class besttestindexuser(httplocust): #这个类继承了HttpLocust, representing each user within each concurrency task_set = besttest #这个是每个用户都去干什么, specifying the Besttest class, which will each user to run Besttest the method inside this class

The code is written, run it down, look at the results, but it's not running like a normal Python script, you have to use the Locust command, run it on the command line.

123 locust -f besttest. Py --host=http:// www. Besttest. CN #-f is to specify a python file followed by the Python file we just wrote #--host is the site you want to visit, followed by the URL of the site

This can run, after the run, you can open the browser inside the Locust console, locust the default port number is 8089, after running, access to the time with ip:8090 can be accessed, such as:

Start Locust:

Locust Console page:

Page after starting the test:

TPS Map and Response time graph:

But locust not like LoadRunner and JMeter can set the start time and the end time, only you stop the test when the stop, this is more annoying, so in the process of use still need to pay attention to the test time.

Just above the example is a single interface pressure measurement, or even more of a single scene, if you want to do mixed scene of the pressure test, such as a business process, first login, and then order, shopping such as how to do?

Just write multiple tasks, that is, in the class to write multiple functions, who want to execute, the number on the line, 1,2,3,4 code as follows:

123456789101112131415 from locust import httplocust, TaskSet, task class besttest(TaskSet): @Task(1)#给task装饰器传一个参数, the representative first visit the homepage def index(self):#首页 Self . Client. Get('/') #发get请求 @Task(2) def login(self):#登录 Self . Client. Post('/login ',{' username ':' besttest ',' password ':' 123456 ' }) #发送post请求, the first one is the path, the second entry for this interface, the account number and the password     class besttestindexuser(httplocust): #这个类继承了HttpLocust, representing each user within each concurrency task_set = besttest #这个是每个用户都去干什么, specifying the class Besttest, It will each user to run Besttest This class inside the method

About Locust said so much, is not very simple, go to try it, the problem of small partners can leave a message oh ~

Python-based performance testing tool –locust

Related Article

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.