Python's implementation of automated performance testing is so simple

This article mainly introduces the method of implementing automated performance testing in Python. The combination of the example codes in this article introduces you in detail, which has a certain reference value, and friends who need it can refer to it.

 1. What is performance automation testing?

Performance system load capacity stability under overloaded operation system bottleneck automated testing using programs instead of manual to improve test efficiency performance automation using code to simulate a large number of users, allowing users to concurrently request multiple pages and multi-user concurrent requests to collect parameters, statistical system load capacity to generate reports.

2. Performance automation test library in Python?

locust library

Use Python to use code to create batches of users. Distributed performance can be tested on multiple servers. It is highly scalable and stable, widely used. It can withstand the test in various scenarios. Based on the web ui interface, it can test any system. .

1. Install locust

Use the official pypi source to install

pip install locustio

Use Douban pypi source to install (recommended)

pip install -i https://pypi.douban.com/simple locustio

After the installation is successful, a new command will be added to the cmd console, you can enter the following command to view:

locust --help

 2. Basic usage

Create a locustfile.py file in the project root directory

from locust import Locust, TaskSet, task
class MyTasks(TaskSet):
 """
 创建测试任务类,需要继承TaskSet
 可以添加多个测试任务
 """
 # 每个测试任务,往往会以实例方法的形式来呈现
 # 同时需要使用task装饰器来装饰测试任务
 @task
 def one_task(self):
  print("执行一个伟大的测试任务!")
class RunTasks(Locust):
 """
 创建运行测试类,需要继承Locust父类
 """
 task_set = MyTasks # 指定测试任务类,使用task_set覆盖父类的类属性
 min_wait = 2000  # 指定启动任务间隔的时间范围(单位毫秒):2~5秒之间
 max_wait = 5000  # 使用min_wait、max_wait覆盖父类的类属性

1. Write an automated test script and create a test_load.py file in the project root directory

from locust import HttpLocust, TaskSet, task
class AdminLoadTest(TaskSet):
 """
 创建后台管理站点压测类,需要继承TaskSet
 可以添加多个测试任务
 """
 def login(self):
  """
  登录实例方法
  :return: 
  """
  self.client.post("http://localhost:8088/users/login/",
       {"user_account": "admin", "password": "123456"})
 def logout(self):
  """
  登出实例方法
  :return:
  """
  self.client.get("http://localhost:8088/users/logout/")
 def on_start(self):
  """
  当任何一个task调度执行之前,
  on_start实例方法会被调用
  先登录
  :return:
  """
  self.login()
 def on_stop(self):
  """
  当任何一个task调度执行之后,
  on_stop实例方法会被调用
  后登出
  :return:
  """
  self.logout()
 @task
 def admin_index(self):
  """
  对后台主页进行压测
  :return:
  """
  self.client.get("http://localhost:8088/admin/")
class RunLoadTests(HttpLocust):
 """
 创建运行压测类
 """
 task_set = AdminLoadTest

locust is extremely powerful for stress testing. It supports distributed deployment and provides a simple interface for stress testing. It is very easy to write and provide a UI interface to configure beautiful and detailed chart statistics.

The above is how easy it is to implement automated performance testing in Python that the editor introduced to you. I hope it will be helpful to everyone.

I am a python development engineer, and I have compiled a set of the latest python system learning tutorials, including basic python scripts to web development, crawlers, data analysis, data visualization, machine learning, and interview books. Those who want these materials can pay attention to the editor, add Q skirt 851211580 to pick up Python learning materials and learning videos, and online guidance from the Great God!

Guess you like

Origin blog.csdn.net/pyjishu/article/details/105414065