-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Introduce data_synchronizer (and iterator-based SimpleMongoReader)
- Loading branch information
Showing
4 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from locust_plugins.mongoreader import SimpleMongoReader | ||
from locust_plugins.csvreader import CSVDictReader | ||
from locust_plugins.synchronizer import data_synchronizer, getdata | ||
from locust import HttpUser, task, events, runners, run_single_user | ||
import time | ||
|
||
|
||
@events.init.add_listener | ||
def on_locust_init(environment, **kwargs): | ||
# ssn_reader = CSVDictReader("ssn.tsv", delimiter="\t") | ||
ssn_reader = SimpleMongoReader({"env": "test", "tb": False, "lb": True}, "last_login") | ||
data_synchronizer(ssn_reader) | ||
|
||
|
||
class MyUser(HttpUser): | ||
@task | ||
def my_task(self): | ||
start_time = time.time() | ||
start_perf_counter = time.perf_counter() | ||
user = getdata(self) | ||
self.environment.events.request.fire( | ||
request_type="fake", | ||
# name=user["ssn"], | ||
name="fake", | ||
start_time=start_time, | ||
response_time=(time.perf_counter() - start_perf_counter) * 1000, | ||
response_length=0, | ||
context={**self.context()}, | ||
exception=None, | ||
) | ||
|
||
host = "http://localhost:8089" | ||
|
||
|
||
if __name__ == "__main__": | ||
run_single_user(MyUser) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import json | ||
from typing import Dict, Iterator | ||
|
||
from gevent.event import AsyncResult | ||
from locust import User, events | ||
from locust.exception import StopUser | ||
from locust.env import Environment | ||
from locust.runners import MasterRunner, WorkerRunner | ||
import bson.objectid | ||
|
||
test_data: Dict[int, AsyncResult] = {} | ||
datasource_iterator: Iterator[Dict] | ||
|
||
|
||
def data_synchronizer(f: Iterator[dict]): | ||
@events.test_start.add_listener | ||
def test_start(environment, **_kw): | ||
global datasource_iterator | ||
datasource_iterator = f | ||
runner = environment.runner | ||
if runner: | ||
# called on master | ||
def user_request(environment: Environment, msg, **kwargs): | ||
data = next(f) | ||
# data["_id"] = str(data["_id"]) # this is an ObjectId, msgpack doesnt know how to serialize it | ||
print(data) | ||
environment.runner.send_message( | ||
"user_response", | ||
{"payload": data, "user_id": msg.data["user_id"]}, | ||
client_id=msg.data["client_id"], | ||
) | ||
|
||
# called on worker | ||
def user_response(environment: Environment, msg, **kwargs): | ||
assert test_data | ||
test_data[msg.data["user_id"]].set(msg.data) | ||
|
||
if not isinstance(runner, WorkerRunner): | ||
runner.register_message("user_request", user_request) | ||
if not isinstance(runner, MasterRunner): | ||
runner.register_message("user_response", user_response) | ||
|
||
|
||
def getdata(u: User): | ||
# try: | ||
if not u.environment.runner: # no need to do anything clever if there is no runner | ||
return next(datasource_iterator) | ||
|
||
test_data[id(u)] = AsyncResult() | ||
runner = u.environment.runner | ||
runner.send_message("user_request", {"user_id": id(u), "client_id": runner.client_id}) | ||
data = test_data[id(u)].get()["payload"] | ||
del test_data[id(u)] | ||
return data | ||
# except KeyboardInterrupt: | ||
# # probably we're just shutting down but lets try to be as graceful as possible | ||
# logging.debug("Caught SIGINT" | ||
# raise StopUser() |