• 基于python的性能负载测试Locust-2-快速入门


    快速入门

    Example locustfile.py

    这是一个快速入门的小例子 locustfile.py:

    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

    我们这里定义了几个locust的任务,这些任务传入了一个参数(一个Locust的实例)并且可以被正常调用。这些任务被聚集在tasks属性里的名为TaskSet的类中。我们还定义了一个名为HttpLocust的类,它代表一个用户,我们定义了一个虚拟用户在执行不同任务之间的等待时间,就像TaskSet类应该定义用户的“行为”。TaskSets可以被嵌套。

    HttpLocust类继承了Locust类,并增加了一个从属性,这个属性是HttpSession的实例,用来发起Http请求。

    另外我们可以定义tasks,这个因为使用了@task装饰器显的更加方便。下面的代码和上面的代码等价。

    from locust import HttpLocust, TaskSet, task
    
    class UserBehavior(TaskSet):
        def on_start(self):
            """ on_start is called when 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

    Locust类(和HttpLocust一样,因为是它的子类)如同其他的用户行为,也可以定义最小和最大的等待时间-根据虚拟用户-任务之间的等待时间(最小等待和最大等待)

     

    启动 Locust

    如果上述文件命名为locustfile.py并且我们执行命令的路径和这个文件在同级目录下,那么久可以通过下面的命令来启动locust

    注意:这里的http://example.com需要改为被测对象的地址

    locust --host=http://example.com
    

    如果locust文件放置在其他地方:

    locust -f ../locust_files/my_locust_file.py --host=http://example.com
    

    如果需要在多个进程下分布式运行Locust,我们可以通过“-master”启动一个主进程:

    locust -f ../locust_files/my_locust_file.py --master --host=http://example.com
    

    然后可以启动任意数量的从进程:

    locust -f ../locust_files/my_locust_file.py --slave --host=http://example.com

    如果我们想在多个机器上分布式运行locust,也可以在启动从服务器的同时定义它归属的主服务器(如果只是在一台机器上分布式运行locust,就不需要这样做,因为主服务器默认为127.0.0.1)

    locust -f ../locust_files/my_locust_file.py --slave --master-host=192.168.0.100 --host=http://example.com
    

    PS:

    查看所有功能选项

    locust –help

     

    打开Locust的页面接口

    如果你已经通过上述任一命令启动了Locust,那么接下来应该打开浏览器并指向http://127.0.0.1:8089(如果在本地运行Locust)。如下图:

    _images/webui-splash-screenshot.png
  • 相关阅读:
    an optimal solution to the problem
    sql_action
    rgb转灰度 RGB To Gray php Adobe RGB (1998) [gamma=2.20]
    MTUTCP/IP协议栈linux kernelTCP丢包重传UDP高性能AI ip数据报 tcp数据报
    Metaheuristic
    a computercentered view of information systems to a databasecentered view
    算法 图像识别 搜索
    var wi = 0; wi < arr.length; wi++
    模拟信号 数字信号
    locations in main memory to be referenced by descriptive names rather than by numeric addresses
  • 原文地址:https://www.cnblogs.com/mu-shi-shi/p/locust2.html
Copyright © 2020-2023  润新知