如何在蝗虫脚本本身中传递要模拟的总用户数和生成率

How to pass the number of total users to simulate and spawn rate in the locust script itself

当我 运行 locust 文件时,如何在 Web UI 中传递要模拟的总用户数和生成率,相反,我想将它们作为变量传递到脚本本身?

class QuickstartUser(HttpUser):
    wait_time = between(1, 2.5)
    users = 10
    spawn_rate = 1

@task
    def on_start(self):
        filenumber="ABC"

        # Get file info
        response = self.client.get(f"/files/" + filenumber)
        json_var = response.json()
        print("response Json: ", json_var)
        time.sleep(1)

您可能可以通过在代码中访问 Runner 来完成此操作,但如果您使用 Load Shape.

会容易得多
class MyCustomShape(LoadTestShape):
    time_limit = 600
    spawn_rate = 20

    def tick(self):
        run_time = self.get_run_time()

        if run_time < self.time_limit:
            # User count rounded to nearest hundred.
            user_count = round(run_time, -2)
            return (user_count, spawn_rate)

        return None

tick 是自动调用的,你只需要 return 一个你想要的用户数和生成率的元组。你可以做任何你想做的工作来计算用户和费率应该是多少。有more examples in the GitHub repo.