Skip to content

Commit

Permalink
Merge pull request #156 from SvenskaSpel/distributor-dont-block-locus…
Browse files Browse the repository at this point in the history
…ts-client-listener-loop

Distributor dont block locusts client listener loop
  • Loading branch information
DennisKrone authored Dec 19, 2023
2 parents 1ea5a29 + 07236dd commit 54af68d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion locust_plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .wait_time import constant_ips, constant_total_ips
from .wait_time import constant_total_ips
import locust
from locust.user.task import DefaultTaskSet, TaskSet
from locust import events
Expand Down
17 changes: 11 additions & 6 deletions locust_plugins/distributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from gevent.event import AsyncResult
import greenlet
import gevent
from locust.env import Environment
from locust.runners import WorkerRunner

Expand All @@ -23,12 +24,8 @@ def __init__(self, environment: Environment, iterator: Optional[Iterator], name=
if self.runner:
# received on master
def _distributor_request(environment: Environment, msg, **kwargs):
item = next(self.iterator)
self.runner.send_message(
f"_{name}_response",
{"item": item, "gid": msg.data["gid"]},
client_id=msg.data["client_id"],
)
# do this in the background to avoid blocking locust's client_listener loop
gevent.spawn(self._master_next_and_send, msg.data["gid"], msg.data["client_id"])

# received on worker
def _distributor_response(environment: Environment, msg, **kwargs):
Expand All @@ -37,6 +34,14 @@ def _distributor_response(environment: Environment, msg, **kwargs):
self.runner.register_message(f"_{name}_request", _distributor_request)
self.runner.register_message(f"_{name}_response", _distributor_response)

def _master_next_and_send(self, gid, client_id):
item = next(self.iterator)
self.runner.send_message(
f"_{self.name}_response",
{"item": item, "gid": gid},
client_id=client_id,
)

def __next__(self):
"""Get the next data dict from iterator
Expand Down
2 changes: 1 addition & 1 deletion locust_plugins/mongoreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __next__(self) -> dict:
try:
with dblock:
doc: dict = next(self.cursor)
self.coll.find_one_and_update(
self.coll.update_one(
{"_id": doc["_id"]},
{"$set": {self.timestamp_field: datetime.now(tz=timezone.utc)}},
)
Expand Down
7 changes: 0 additions & 7 deletions locust_plugins/wait_time.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import logging
import time
from collections import deque, namedtuple
Expand Down Expand Up @@ -57,11 +56,5 @@ def func(user):
return func


def constant_ips(_ips):
"""this function has been removed, now that it is available in locust.wait_time.constant_throughput"""
logging.error("constant_ips has been removed, now that it is available in locust.wait_time.constant_throughput")
sys.exit(1)


def constant_total_pacing(seconds: float):
return constant_total_ips(1.0 / seconds)

0 comments on commit 54af68d

Please sign in to comment.