Locust.io顺序任务集未按顺序运行

时间:2020-07-29 21:24:11

标签: python locust

我是Python的新手。我本周将编写我的第一个Python类,以便在我们的API上运行Locust.io负载测试。 我已经在下面设置了代码。

import random
from locust import HttpUser, SequentialTaskSet, task, between
from datetime import datetime

class CredentialLoadTest(SequentialTaskSet):

    @task
    def post_credential(self):
        print("==========================")
        print("========== POST ==========")
        print("==========================")
        print("==========POST END===========")


    @task
    def get_credential(self):
        print("==========================")
        print("========== GET ===========")
        print("==========================")
        print("==========GET END===========")

    @task
    def put_credential(self):
        print("==========================")
        print("========== PUT ===========")
        print("==========================")
        print("==========PUT END===========")


class AwesomeUser(HttpUser):
    tasks = [CredentialLoadTest]
    host = "https://url.com"

    # wait time between tasks, 5 and 9 seconds
    wait_time = between(5, 9)

我在这里删除了我的API端点的所有实际测试代码。我只是想验证任务是否按顺序运行。当我运行此代码时,我得到的输出如下所示。

[2020-07-29 21:14:54,067] 8a7f07ddbe29/INFO/locust.main: Starting web
interface at http://:8089 [2020-07-29 21:14:54,077]
8a7f07ddbe29/INFO/locust.main: Starting Locust 1.1 [2020-07-29
21:15:01,123] 8a7f07ddbe29/INFO/locust.runners: Hatching and swarming
2 users at the rate 2 users/s (0 users already running)...
==========================
========== GET ===========
==========================
==========GET END=========== [2020-07-29 21:15:01,624] 8a7f07ddbe29/INFO/locust.runners: All users hatched: AwesomeUser: 2 (0
already running)
==========================
========== GET ===========
==========================
==========GET END===========
==========================
========== POST ==========
==========================
==========POST END===========
==========================
========== POST ==========
==========================
==========POST END===========
==========================
========== PUT ===========
==========================
==========PUT END===========
==========================
========== PUT ===========
==========================
==========PUT END===========
==========================
========== GET ===========
==========================
==========GET END===========
==========================
========== GET ===========
==========================
==========GET END===========

您可以看到,任务的运行顺序为 GETPOST PUT 我要的订单是POSTGETPUT

2 个答案:

答案 0 :(得分:2)

所以我仍然无法使顺序任务集正常工作。因此,我有点“破解”了一个对我有用的解决方案。我基本上以创建一个任务为起点,然后调用其他“任务”,它们只是普通的Python函数。 这对我来说是完美的,它按照我想要的顺序运行了我的每个API操作。

# GET newly created credential


def get_credential(self):
    print_log("<========== GET ===========>")
    print_log("<========== GET END ========>", True)

# PUT newly created credential


def put_credential(self):
    print_log("<========== PUT ===========>")
    print_log("<======== PUT END ========>", True)


# DELETE newly created credential

def delete_credential(self):
    print_log("<========= DELETE =========>")
    print_log("<======= DELETE END =======>", True)


class CredentialLoadTestPost(TaskSet):
    print_log("*** LOCUST API LOAD TEST Started ***", True)

    # Create new credentail
    @task
    def post_credential(self):
        print_log("<========== POST ==========>")
        print_log("<========= POST END========>", True)
        get_credential(self)
        put_credential(self)
        delete_credential(self)


class AwesomeUser(HttpUser):
    tasks = {CredentialLoadTestPost}

    # wait time between tasks, 5 and 9 seconds assume someone who is browsing the Locust docs,
    wait_time = between(5, 9)

答案 1 :(得分:0)

我看到你执行了不止一个用户的 locust 2 users at the rate 2 users/s 所以我假设我们在您的输出中看到了来自两个并行用户的打印件。 它以这种方式工作,因为任务序列在不同的线程中运行。