• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞java代码 3年前 (2022-05-21) 11次浏览 已收录 0个评论

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

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

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

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

www#gaodaima.com来源gaodai#ma#com搞@代~码网搞代码

现在改成了:

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

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

os.system("locust -f load_test.py UserBehavior --host=http://xxxx.xxxx.xxxx.xxxx: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://xxxx.xxxx.xxxx.22:8090/YLAPI")

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

 reference: 

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

 


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:基于最新版本的locust代码post和get脚本编写

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址