Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the option to add config when using kafka #204

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ env
_version.py
examples/screenshot_*
output.txt
.idea
1 change: 1 addition & 0 deletions examples/kafka_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class MyUser(KafkaUser):
bootstrap_servers = os.environ["LOCUST_KAFKA_SERVERS"]
configs = {"compression.type": "gzip"}

@task
def t(self):
Expand Down
8 changes: 5 additions & 3 deletions locust_plugins/users/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class KafkaUser(User):
abstract = True
# overload these values in your subclass
bootstrap_servers: str = None # type: ignore
configs: dict[str, str | int] = None

def __init__(self, environment):
super().__init__(environment)
self.client: KafkaClient = KafkaClient(environment=environment, bootstrap_servers=self.bootstrap_servers)
self.client: KafkaClient = KafkaClient(environment=environment, bootstrap_servers=self.bootstrap_servers,
configs=self.configs)

def on_stop(self):
self.client.producer.flush(5)
Expand All @@ -35,9 +37,9 @@ def _on_delivery(environment, identifier, response_length, start_time, start_per


class KafkaClient:
def __init__(self, *, environment, bootstrap_servers):
def __init__(self, *, environment, bootstrap_servers, configs=None):
self.environment = environment
self.producer = Producer({"bootstrap.servers": bootstrap_servers})
self.producer = Producer({"bootstrap.servers": bootstrap_servers}, **configs)

def send(self, topic: str, value: bytes, key=None, response_length_override=None, name=None, context={}):
start_perf_counter = time.perf_counter()
Expand Down