• Locust 类的使用


     HttpLocust类

    可定义多个HttpLocust类,即多个用户可执行不同的任务或者相同的任务,但是执行频率不一样,用weight进行约定。

    # coding:utf-8
    from locust import HttpLocust, TaskSet, task
    import urllib3
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
    
    class UserTask1(TaskSet):
        @task
        def task1(self):
            self.client.get("/", headers=header, verify=False)
    
    class UserTask2(TaskSet):
        @task
        def task2(self):
            self.client.get("/belle-ls/", verify=False)
    
    class UserOne(HttpLocust):
        weight = 1
        task_set = UserTask1
    
    class UserTwo(HttpLocust):
        weight = 2
        task_set = UserTask2

     终端命令:

    1 $ locust -f locustDemo2.py UserOne UserTwo --host=https://www.cnblogs.com
    2 [2019-03-07 14:55:49,299] LiuShuangdeiMac.local/INFO/locust.main: Starting web monitor at *:8089
    3 [2019-03-07 14:55:49,300] LiuShuangdeiMac.local/INFO/locust.main: Starting Locust 0.9.0
    4 [2019-03-07 14:55:58,657] LiuShuangdeiMac.local/INFO/locust.runners: Hatching and swarming 10 clients at the rate 10 clients/s...
    5 [2019-03-07 14:55:59,678] LiuShuangdeiMac.local/INFO/locust.runners: All locusts hatched: UserTwo: 7, UserOne: 3

    运行结果如下,UserTwo 的执行频率是UserOne的两倍多

     

    TaskSet类:执行频率的约定及嵌套

    1. task修饰符 @task or @task(1)  1为权重,权重越高,执行比例越大

    2. taskSet嵌套,需要用 interrupt()跳出

     写法1:

    # coding:utf-8
    
    from locust import HttpLocust, TaskSet, task
    import urllib3
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
    class UserTask(TaskSet):
        @task(2)
        class stayMe(TaskSet):
            @task(2)
            def MyHome(self):
                self.client.get("/belle-ls/", verify=False)
          
            @task(4)
            def MyCote(self):
                self.client.get("/belle-ls/category/1412671.html", verify=False)
    
            @task(1)
            def Out(self):
                self.interrupt() #跳出当前TaskSet类,才有机会执行其他行为
    
        @task(1)
        def BlogHome(self):
            self.client.get("/", verify=False)
    class User(HttpLocust):
        task_set = UserTask

    写法2:

    class stayMe(TaskSet):
        @task(2)
        def MyHome(self):
            self.client.get("/belle-ls/", verify=False)
    
        @task(4)
        def MyCote(self):
         self.client.get("/belle-ls/category/1412671.html", verify=False)
    
        @task(1)
        def Out(self):
            self.interrupt()
    
    class UserTask(TaskSet):
        tasks= {stayMe:2}
        @task(1)
        def BlogHome(self):self.client.get("/", verify=False)class User(HttpLocust):
        task_set = UserTask

     

    ResponseContextManager类:

    class UserTask(TaskSet):
        @task(1)
        def BlogHome(self):
            with self.client.get("/", headers = header, catch_response = True, verify = False) as response:
                if response.status_code == 200:
                    response.failure('Failed!')
                else:
                    response.success()
    
    class User(HttpLocust):
        task_set = UserTask

    ResponseContextManger类是Response类的子类,多了两个failure()和success()方法。

    上面的例子:使用with语句及catch_response参数可以截获原始响应,把所有status_code是200的响应都当做失败响应。这里success()和failure(str)的调用会体现在结果的统计上。

     

    合并请求:

    比如进入博客分类,其实属于一条测试用例,但是由于参数的不同,会再测试结果中显示两行,比如:

    https://www.cnblogs.com/belle-ls/category/1412671.html

    https://www.cnblogs.com/belle-ls/category/1411809.html

    如何合并呢?可以在get请求参数中加个name参数来将统计叠加在一起name也可以用来对Name显示为路径进行重命名

    class UserTask(TaskSet):
        @task(1)
        def BlogHome(self):
            self.client.get("/belle-ls/category/1412671.html", headers=header, verify=False, name = "category")
            self.client.get("/belle-ls/category/1411809.html", headers=header, verify=False, name = "category")
    
    class User(HttpLocust):
        task_set = UserTask

    效果:

     

    参考文章:https://blog.csdn.net/a464057216/article/details/48394213

  • 相关阅读:
    同余 扩展欧几里得
    185. [USACO Oct08] 挖水井
    Dijkstra算法
    Floyed算法
    codves——1079 回家
    codves——1021 玛丽卡
    codves——5960 信使
    计算几何基础
    【正睿oi省选十连测】第一场
    [APIO2012]守卫
  • 原文地址:https://www.cnblogs.com/belle-ls/p/10490624.html
Copyright © 2020-2023  润新知