Performance testing tools——Locust

Common performance testing tools:LoadRunner、Jmeter、Locust。这几种工具都有各自的优点,我们先分析一下他们的特性:

/ LoadRunner Jmeter Locust
授权方式 收费 Open Source Open Source
development language C/Java Java Python
测试脚本 C/Java GUI Python
concurrent requests thread thread 协程
单机并发能力
分布式压力 支持 支持 支持
资源监控 支持 不支持 不支持
报告与分析 完善 简单图表 简单图表
secondary development 不支持 支持 支持

我个人是Python栈的,so I like Locust

What is Locust?

Locust is an easy to use, sciptable and scalable performance testing tool.
You define the behaviour of your users in regular Python code, instead of being stuck in a UI or restrictive domain specific language.
This makes Locust infinitely expandable and developer friendly.

  • Write test scenarios in plain old Pthon
  • Distributed and scalable - supports hundreds of thousands of concurrent users
  • Web-based UI
  • Can test any system
  • Hackable

Installation

  • Install Python
  • Install the package
    pip install locust
  • Validate your installation
    locust -V

Writing a locustfile

from locust import HttpUser,TaskSet,task


class Login(TaskSet):

    def on_start(self):
        url = "/Login"
        headers = {"Content-Type": "xxx; charset=utf-8",
                   "Connection": "keep-alive",
                   "Accept": "*/*"}
        data = {"client": "iOS",
                "data": {"xxx":"xxx","xxx":"xxx"},
                "deviceid": "xxx",
                "sign": "xxx",
                "timestamp": "1668409045569",
                "token": "xxx"}
        res = self.client.post(url=url, data=data, headers=headers)
        print("json:",res.json())
        token = res.json()['data']['token']
        return token

    @task
    def vehicle_base(self):
        url = "/xxx"
        headers = {"Connection": "keep-alive",
                   "Accept": "*/*"}
        data = {"client": "iOS",
                "data": {"xxx":"xxx","xxx":"xxx"},
                "deviceid": "xxx",
                "sign": "xxx",
                "timestamp": "1668409046165",
                "token": self.on_start()}
        res = self.client.post(url=url, data=data, headers=headers)
        print(res.json())

class WebsitUser(HttpUser):
    host = "https://xxx.xxxxx.com"
    tasks = [Login]
    min_wait = 3000  # 单位为毫秒,最小等待时间
    max_wait = 6000  # 单位为毫秒,最大等待时间

if __name__ == "__main__":
    import os
    os.system("locust -f basic(1).py")

User class

Our class defines a wait_time that will make the simulated users wait between 1 and 5 seconds after each task is executed.

  • wait_time attribute
    If no wait_time is specified, the next task will be executed as soon as one finishes.
    constant for a fixed amount of time
    between for a random time between a min and max value
    For example, to make each user wait between 0.5 and 10 seconds between every task execution:
from locust import User, task, between

class MyUser(User):
    @task
    def my_task(self):
        print("executing my_task")

    wait_time = between(0.5, 10)

constant_throughput for an adaptive time that ensures the task runs (at most) X times per second.
constant_pacing for an adaptive time that ensures the task runs (at most) once every X seconds (it is the mathematical inverse of constant_throughput).

  • weight and fixed_count attributes

  • host attribute

  • tasks attribute

  • environment attribute
    self.environment.runner.quit()

  • on_start and on_stop methods
    A User will call its on_start method when it starts running, and its on_stop method when it stops running.

Tasks
  • @task decorator
  • tasks attribute
  • @tag decorator
Events
  • test_start and test_stop
  • init
  • Other events
HttpUser class
  • client attribute / HttpSession
  • Validating responses
  • REST/JSON APIs
  • Grouping requests
  • HTTP Proxy settings
  • Connection pooling
TaskSets
How to structure your test code
posted @ 2022-11-15 10:41  、阿红吖  阅读(41)  评论(0编辑  收藏  举报