分享

基于最新版本的locust代码post和get脚本编写

 小样样样样样样 2021-06-09

最近在做压力测试,jemeter使用起来不稳定,而消耗电脑内存太大,loadrunner是收费的,虽有破解,但不太道德!后来使用了开源的locust

locust是基于协程的开源的压力自动化测试框架,网上我也找了大量的资料配置及示例,但都是基于旧版本的,在新版本一直报错,后来通过查找官方网站的示例,总算知道了原因——是因为locust升级之后,一些参数及变量发生了变化(具体如何从官方网站说明文档怎么研究本文不作赘述)

大部分网站上都有下述代码:

class WebsiteUser(HttpLocust):
    task_set = UserBehavior #调用自定义方法 UserBehavior
    min_wait = 3000 #最小等待时间
    max_wait = 6000 #最大等待时间

现在改成了:

class UserBehavior(HttpUser): #
    wait_time = between(5, 15) #直接把等待时间范围使用wait_time写在了自定义方法UserBehavior里

且调用的时候在python文件之后加上自定义方法的参数UserBehavior 即可

os.system("locust -f load_test.py UserBehavior --host=http://x.x:8090/YLAPI")

以下为完整代码:

from locust import User, task, between, HttpUser

class UserBehavior(HttpUser):
    wait_time = between(5, 15)
    @task(1) #Post请求
    def firstTest(self):

        header = {"Content-Type": "application/json"}
        payload = {
            "versionNum": "10",
            "platform": "android"
        }

        req = self.client.post("/Api/app/version/appCheck", data=payload, headers=header, verify=False)
        if req.status_code == 200:
            print("success")
        else:
            print("fails:" + str(req.status_code))

    # @task(1) #get请求
    # def about(self):
    #     self.client.get("/about/")



if __name__ == "__main__":
    import os
    #os.system("locust -f load_test.py UserBehavior")
    os.system("locust -f load_test.py UserBehavior --host=http://x.xxxx.22:8090/YLAPI")

  Enjoy your latest version locust script code on your hand :-)

 reference: 

1) https://www.cnblogs.com/lxmtx/p/12580031.html

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多