Skip to content

Commit b35e2f9

Browse files
authored
CORE-8311: Add locust_plugins dependency and parameters to export results to timescale db
2 parents 4dbb418 + a409dcb commit b35e2f9

File tree

5 files changed

+1098
-64
lines changed

5 files changed

+1098
-64
lines changed

chainbench/main.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ def cli(ctx: click.Context):
106106
"You may specify this option multiple times",
107107
multiple=True,
108108
)
109+
@click.option(
110+
"--timescale", is_flag=True, help="Export data to PG with timescale extension"
111+
)
112+
@click.option(
113+
"--pg-host", default=None, help="Hostname of PG instance with timescale extension"
114+
)
115+
@click.option(
116+
"--pg-port",
117+
default=5432,
118+
help="Port of PG instance with timescale extension",
119+
show_default=True,
120+
)
121+
@click.option(
122+
"--pg-username", default="postgres", help="PG username", show_default=True
123+
)
124+
@click.option("--pg-password", default=None, help="PG password")
109125
@click.pass_context
110126
def start(
111127
ctx: click.Context,
@@ -126,6 +142,11 @@ def start(
126142
notify: str | None,
127143
debug_trace_methods: bool,
128144
exclude_tags: list[str],
145+
timescale: bool,
146+
pg_host: str | None,
147+
pg_port: int,
148+
pg_username: str,
149+
pg_password: str | None,
129150
):
130151
if notify:
131152
click.echo(f"Notify when test is finished using topic: {notify}")
@@ -139,6 +160,15 @@ def start(
139160
click.echo("Target is required when running in headless mode")
140161
sys.exit(1)
141162

163+
if timescale and any(
164+
pg_arg is None for pg_arg in (pg_host, pg_port, pg_username, pg_password)
165+
):
166+
click.echo(
167+
"PG connection parameters are required "
168+
"when --timescale flag is used: pg_host, pg_port, pg_username, pg_password"
169+
)
170+
sys.exit(1)
171+
142172
if not profile_dir:
143173
profile_dir = get_base_path(__file__)
144174

@@ -180,6 +210,11 @@ def start(
180210
headless=headless,
181211
target=target,
182212
exclude_tags=custom_exclude_tags,
213+
timescale=timescale,
214+
pg_host=pg_host,
215+
pg_port=pg_port,
216+
pg_username=pg_username,
217+
pg_password=pg_password,
183218
)
184219
if headless:
185220
click.echo(f"Starting master in headless mode for {profile}")
@@ -203,6 +238,11 @@ def start(
203238
worker_id=worker_id,
204239
log_level=log_level,
205240
exclude_tags=custom_exclude_tags,
241+
timescale=timescale,
242+
pg_host=pg_host,
243+
pg_port=pg_port,
244+
pg_username=pg_username,
245+
pg_password=pg_password,
206246
)
207247
worker_args = shlex.split(worker_command, posix=is_posix)
208248
worker_process = subprocess.Popen(worker_args)

chainbench/user/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
from chainbench.util.event import setup_event_listeners
1010
from chainbench.util.rpc import generate_request
1111

12+
# importing plugins here as all profiles depend on it
13+
import locust_plugins # isort: skip # noqa
14+
15+
1216
setup_event_listeners()
1317

1418

chainbench/util/cli.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ def ensure_results_dir(
4040
return results_dir
4141

4242

43+
def get_timescale_args(
44+
pg_host: str | None,
45+
pg_port: int | None,
46+
pg_username: str | None,
47+
pg_password: str | None,
48+
) -> str:
49+
return (
50+
f" --timescale --pghost={pg_host} --pgport={pg_port}"
51+
f" --pgpassword={pg_password} --pguser={pg_username}"
52+
)
53+
54+
4355
def get_master_command(
4456
profile_path: Path,
4557
host: str,
@@ -53,6 +65,11 @@ def get_master_command(
5365
exclude_tags: list[str],
5466
target: str | None = None,
5567
headless: bool = False,
68+
timescale: bool = False,
69+
pg_host: str | None = None,
70+
pg_port: int | None = None,
71+
pg_username: str | None = None,
72+
pg_password: str | None = None,
5673
) -> str:
5774
"""Generate master command."""
5875
command = (
@@ -65,6 +82,9 @@ def get_master_command(
6582
f"--loglevel {log_level} --expect-workers {workers}"
6683
)
6784

85+
if timescale:
86+
command += get_timescale_args(pg_host, pg_port, pg_username, pg_password)
87+
6888
if target is not None:
6989
command += f" --host {target}"
7090

@@ -87,13 +107,21 @@ def get_worker_command(
87107
target: str | None = None,
88108
headless: bool = False,
89109
worker_id: int = 0,
110+
timescale: bool = False,
111+
pg_host: str | None = None,
112+
pg_port: int | None = None,
113+
pg_username: str | None = None,
114+
pg_password: str | None = None,
90115
) -> str:
91116
"""Generate worker command."""
92117
command = (
93118
f"locust -f {profile_path} --worker --master-host {host} --master-port {port} "
94119
f"--logfile {results_path}/worker_{worker_id}.log --loglevel {log_level}"
95120
)
96121

122+
if timescale:
123+
command += get_timescale_args(pg_host, pg_port, pg_username, pg_password)
124+
97125
if target is not None:
98126
command += f" --host {target}"
99127

0 commit comments

Comments
 (0)