I am trying to run a load test that injects messages to rabbit mq, but I can get locust user class to get the custom client I created loaded. Has someone managed to do this?
import os
from locust import HttpUser, task, TaskSet, run_single_user
from kombu import Connection, Exchange, Queue
class KombuClient:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.connection = Connection(
hostname=os.getenv(
"LOCUST_AMQP_CONFIG", "amqp://guest:guest@localhost:5672//"
),
)
self.channel = self.connection.channel()
def send_message(self, message):
exchange = Exchange(
name=os.getenv("LOCUST_AMQP_EXCHANGE", "test_exchange"),
type="direct",
)
queue = Queue(
name=os.getenv("LOCUST_AMQP_QUEUE", "test_queue"),
exchange=exchange,
routing_key=os.getenv("LOCUST_AMQP_KEY", "test_key"),
)
queue.maybe_bind(self.connection)
queue.declare()
producer = self.connection.Producer(
exchange=exchange,
routing_key=os.getenv("LOCUST_AMQP_KEY", "test_key"),
)
producer.publish(message)
print("INFO: Sent message: '{}'".format(message))
def close_connection(self):
self.connection.release()
class UserBehavior(TaskSet):
@task
def send_hello_message(self):
I make it work by, instead of the line below but that is not 100% correct--> KombuClient().send_message("Hello World!")
self.client.send_message("Hello World!")
class RabbitMQUser(HttpUser):
host = "http://localhost"
tasks = [UserBehavior]
min_wait = 5000
max_wait = 9000
client = KombuClient
if __name__ == "__main__":
run_single_user(RabbitMQUser)
In general I can inject the messages but Locust does not recognize the transaction and does not shows any user running.
I have tried to inspect the Locust code to see what the client expected class is, but I haven’t found any useful information.
I guess the API changed as I am using Locust 2.15.1 and there is no HttpClient class no more
This question is covered in the Locust FAQ and examples can be found in the docs. What you need to do is fire the
requestevent with all your request info and stats. You're usingHttpUserbut overriding theclientso you're making it just a normalUserwhich doesn't know about what you're doing and you need to tell it what to record and when.Example taken from the docs for the gRPC scenario:
Call this with your own stats. See the linked doc page for explanations as to what the parameters are meant to be, but since you're doing a custom user you can make them be and mean whatever you want.