我尝试使用蝗虫运行简单的负载测试时出现AttributeError

时间:2019-09-20 09:49:23

标签: python load-testing locust

我已经为redis创建了一个简单的负载测试文件,但是由于我正在尝试以以下方式执行文件:

locust -f load-test.py

我收到一个错误,说AttributeError: 'NoneType' object has no attribute '_catch_exceptions'。我不明白原因。我做错了什么事?

这是我写的小代码。

# Reference 
# https://docs.locust.io/en/stable/writing-a-locustfile.html
# https://github.com/andymccurdy/redis-py

from locust import Locust, TaskSet, task
import redis


class LoadTestTasks(TaskSet):

    def __init__(self, args):
        self.redis = redis.Redis(host='localhost', port=6379)

    @task
    def update_total_requests(self):
        if self.redis.exists("total-calls") == 1:
            self.redis.incr("total-calls")
        else:
            self.redis.set("total-calls", 1)


class Connection(Locust):
    task_set = LoadTestTasks
    min_wait = 500
    max_wait = 700

堆栈错误:

    <Greenlet at 0x109f01ae8: start_locust(<class 'load-test.Connection'>)> failed with AttributeError
[2019-09-20 15:11:26,551] my-PCs-MacBook-Pro.local/ERROR/stderr: Traceback (most recent call last):
[2019-09-20 15:11:26,551] my-PCs-MacBook-Pro.local/ERROR/stderr: 
[2019-09-20 15:11:26,551] my-PCs-MacBook-Pro.local/ERROR/stderr: File "/Users/my-pc/anaconda3/lib/python3.7/site-packages/locust/core.py", line 354, in run
    if self.locust.stop_timeout is not None and time() - self._time_start > self.locust.stop_timeout:

1 个答案:

答案 0 :(得分:0)

您必须向TaskSet的__init__方法添加超级调用,否则您的 TaskSet 不会被Locust初始化。

...
class LoadTestTasks(TaskSet):
    def __init__(self, args):
        super().__init__(parent)
        self.redis = redis.Redis(host='localhost', port=6379)
...