其他分享
首页 > 其他分享> > Locust 脚本开发入门(1)【四】

Locust 脚本开发入门(1)【四】

作者:互联网

脚本基本构成

一个 Locust 测试脚本就是一个普通的 python 文件,它的基本组成十分简单:

脚本开发入门

我们采用循序渐进的方式来,你不需要特别关注下面的每个范例所具备的实际意义,只需要关心脚本的结构

1、先按照最基本的脚本结构写一个范例

from locust import user, task, between

class MyUser(User):
    @task
    def my_task(self):
        print("executing my_task")

    wait_time = between(5, 15)

脚本实现:

把它保存为 locustfile.py,然后执行

locust -f locustfile.py

然后在本地浏览器访问 http://localhost:8089/,在“Start new Locust run”页面

点击“Start swarming”,可以命令行中看到返回:

PS E:\study.locust> locust -f .\locustfile.py
[2020-07-06 09:14:01,726] DESKTOP-FOCB2DV/WARNING/locust.main: System open file limit setting is not high enough for load testing, and the OS wouldnt allow locust to increase it by itself. See https://docs.locust.io/en/stable/installation.html#increasing-maximum-number-of-open-files-limit for more info.
[2020-07-06 09:14:01,726] DESKTOP-FOCB2DV/INFO/locust.main: Starting web interface at http://:8089
[2020-07-06 09:14:01,734] DESKTOP-FOCB2DV/INFO/locust.main: Starting Locust 1.1
[2020-07-06 09:14:11,992] DESKTOP-FOCB2DV/INFO/locust.runners: Hatching and swarming 10 users at the rate 10 users/s (0 users already running)...
executing my_task
executing my_task
executing my_task
executing my_task
executing my_task
executing my_task
executing my_task
executing my_task
executing my_task
executing my_task
executing my_task

功能实现:

2、把这个脚本修改为访问我的博客:)

import random
from locust import HttpUser, task, between

class cnblogUser(HttpUser):

    wait_time = between(5, 10)

    @task(2)
    def open_blog(self):
        self.client.get("/huanghaopeng/")

    @task(1)
    def open_links(self):
        self.client.get("/huanghaopeng/p/13100305.html")
        self.client.get("/huanghaopeng/p/13220749.html")
        self.client.get("/huanghaopeng/p/13187807.html")

    def on_start(self):
        self.client.get("/")

脚本实现:

把它保存为 locust_cnblogs.py,然后执行

locust -f locust_cnblogs.py

在 Web UI 中填入模拟用户数、加载速率、以及博客园的 Host 信息 https://www.cnblogs.com/huanghaopeng/

点击“Start swarming”,可见:

从上面的运行情况可以发现

现在我们对这个脚本进行一些修改,让它更像一个真实的用户访问行为:

 

标签:脚本,task,入门,locust,self,Locust,executing,等待时间,my
来源: https://www.cnblogs.com/East-fence/p/14962996.html