【发布时间】:2021-09-17 22:56:17
【问题描述】:
我使用的是 locust 版本:1.5.3;一个 locust-graphql-client:https://github.com/DesignrKnight/locust-graphql-client
我的目标是使用 locust 进行 graphql 调用来评估性能。 我有以下蝗虫代码:
from locust import HttpUser, TaskSet, task
from locustgraphqlclient import GraphQLLocust
class UserBehavior(TaskSet):
def on_start(self):
""" on_start is called when a Locust start before any task is scheduled """
self.login()
def on_stop(self):
""" on_stop is called when the TaskSet is stopping """
self.logout()
def login(self):
query = '''
mutation login($username: String!, $password: String!) {
login(username: $username, password: $password) {
access_token
}
}'''
variables = {
'username': 'gm',
'password': 'centric8'
}
result = self.client.execute("login", query, variables)
# Inject the Access Token in the Client, so subsequent requests can be made
self.client.inject_token(result['data']['login']['access_token'])
def logout(self):
# Reset the Access Token in the Client, so no subsequent requests can be made
self.client.inject_token('')
@task(2)
def index(self):
query = '''
query products {
products {
id
name
image
}
}'''
result = self.client.execute("products", query)
@task(1)
def profile(self):
query = '''
query me {
me {
id
username
firstName
lastName
}
}'''
result = self.client.execute("me", query)
class WebsiteUser(GraphQLLocust):
tasks = [UserBehavior]
min_wait = 5000
max_wait = 9000
我得到如下错误
Traceback (most recent call last):
File "~/venv/lib/python3.8/site-packages/locust/user/task.py", line 285, in run
self.schedule_task(self.get_next_task())
File "/~/venv/lib/python3.8/site-packages/locust/user/task.py", line 417, in get_next_task
raise Exception(
Exception: No tasks defined on GraphQLLocust. use the @task decorator or set the tasks property of the User (or mark it as abstract = True if you only intend to subclass it)
如您所见,我对此处提到的任务使用了正确的语法:locust 0.9 to 1.3 Exception: No tasks defined. use the @task decorator or set the tasks property of the User 和此处https://github.com/locustio/locust/issues/1537
我使用的 locust graphql 客户端有问题吗?或者是代码中的东西? 还有我可以在 locust 中用于 graphql 的任何标准客户端吗?
【问题讨论】: