zoukankan      html  css  js  c++  java
  • 性能测试-Locust脚本加强篇(关联、检查点、集合点)

    1、关联:通常在业务流程中有很多一系列的接口调用,从后面的接口依赖前边接口的结果数据

    from lxml import etree
    from locust import TaskSet, task, HttpUser
    class UserBehavior(TaskSet):
        @staticmethod
        def get_session(html):
            tree = etree.HTML(html)
            return tree.xpath("//div[@class='btnbox']/input[@name='session']/@value")[0] 
        @task(10)
        def test_login(self):
            html = self.client.get('/login').text
            username = 'user@compay.com'
            password = '123456'
            session = self.get_session(html)
            payload = { 'username': username, 'password': password, 'session': session }
            self.client.post('/login', data=payload)
    
    class WebsiteUser(HttpUser):
        host = 'http://debugtalk.com'
        # task_set = UserBehavior
        tasks = [UserBehavior ]
        min_wait = 1000
        max_wait = 3000
    

    2、检查点:用来判断返回值是否符合要求

    from lxml import etree
    from locust import TaskSet, task, HttpUser
    class UserBehavior(TaskSet):
        @staticmethod
        def get_session(html):
            tree = etree.HTML(html)
            return tree.xpath("//div[@class='btnbox']/input[@name='session']/@value")[0] 
        @task(10)
        def test_login(self):
            html = self.client.get('/login').text
            assert "200" in html
            username = 'user@compay.com'
            password = '123456'
            session = self.get_session(html)
            payload = { 'username': username, 'password': password, 'session': session }
            self.client.post('/login', data=payload)
    
    class WebsiteUser(HttpUser):
        host = 'http://debugtalk.com'
        # task_set = UserBehavior
        tasks = [UserBehavior ]
        min_wait = 1000
        max_wait = 3000
    

    3、集合点:提高某个接口的并发度,当所有用户运行到指定位置后集合等待,同时向下执行

    from gevent._semaphore import Semaphore
    from locust import TaskSet, events
    from lxml import etree
    
    all_locusts_spawned = Semaphore()
    all_locusts_spawned.acquire()
    def on_hatch_complete(**kwargs):
        all_locusts_spawned.release() # 创建钩子方法
    
    events.hatch_complete += on_hatch_complete # 挂载到locust钩子函数(所有的Locust实例产生完成时触发)
    
    class TestTask(TaskSet):
        def on_start(self):
            """ on_start is called when a Locust start before any task is scheduled """
            all_locusts_spawned.wait() # 限制在所有用户准备完成前处于等待状态
            self.login()
    
        @staticmethod
        def get_session(html):
            tree = etree.HTML(html)
            return tree.xpath("//div[@class='btnbox']/input[@name='session']/@value")[0] 
    
        def login(self):
            html = self.client.get('/login').text
            username = 'user@compay.com'
            password = '123456'
            session = self.get_session(html)
            payload = {'username': username, 'password': password, 'session': session}
            self.client.post('/login', data=payload)
    

    个人博客 蜗牛

  • 相关阅读:
    Pandas提取数据存入excel
    获取页面元素二
    selenium如何解决IE自动填充表单问题
    IE浏览器相关的问题及解决方案
    TestNG入门教程(TestNG介绍、在Eclipse中安装TESTNG、测试小例子、基本注解、如何执行测试、按顺序执行Case、异常测试、组合测试、参数化测试、忽略测试、依赖测试、测试结果报告)
    selenium Webdriver 处理 —— 通过时间控件给文本框赋值
    列表翻页,选中目标记录
    bug统计
    在eclipse中安装TestNG
    selenium搭建、运行
  • 原文地址:https://www.cnblogs.com/codeobj/p/13714290.html
Copyright © 2011-2022 走看看