Skip to content

Commit eab03aa

Browse files
committed
optimizing
1 parent 74566b7 commit eab03aa

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

tests/performance2/Makefile

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ DEFAULT_SPAWN_RATE := 1
3333
DEFAULT_RUN_TIME := 5m
3434
DEFAULT_HOST := http://127.0.0.1:9081
3535

36+
# Default endpoint to test
37+
DEFAULT_ENDPOINT := /
38+
3639
# Function to generate Locust configuration file
3740
# Usage: $(call generate_locust_config,filename_suffix,processes,users,spawn_rate,run_time,description)
3841
define generate_locust_config
3942
@printf "$(YELLOW)Setting up $(2) configuration...$(NC)\n"
4043
@echo "# Locust configuration file - $(5)" > $(LOCUST_CONFIG_FILE)$(1); \
4144
echo "[locust]" >> $(LOCUST_CONFIG_FILE)$(1); \
42-
echo "locustfile = ./locustfiles" >> $(LOCUST_CONFIG_FILE)$(1); \
43-
echo "headless = true" >> $(LOCUST_CONFIG_FILE)$(1); \
45+
echo "locustfile = ./locustfiles/deployment_max_rps_single_endpoint.py" >> $(LOCUST_CONFIG_FILE)$(1); \
4446
if [ -f $(LOCUST_CONFIG_FILE) ]; then \
4547
echo "host = $$(grep "host = " $(LOCUST_CONFIG_FILE) | cut -d= -f2 | xargs)" >> $(LOCUST_CONFIG_FILE)$(1); \
4648
else \
@@ -69,11 +71,12 @@ define create_locust_config
6971
run_time=$${run_time:-$(DEFAULT_RUN_TIME)}; \
7072
read -p "Host to load test [$(DEFAULT_HOST)]: " host; \
7173
host=$${host:-$(DEFAULT_HOST)}; \
74+
read -p "Endpoint to test [$(DEFAULT_ENDPOINT)]: " endpoint; \
75+
endpoint=$${endpoint:-$(DEFAULT_ENDPOINT)}; \
7276
echo; \
7377
echo "# Locust configuration file - Autogenerated" > $(LOCUST_CONFIG_FILE); \
7478
echo "[locust]" >> $(LOCUST_CONFIG_FILE); \
75-
echo "locustfile = deployment_max_rps_test.py" >> $(LOCUST_CONFIG_FILE); \
76-
echo "headless = true" >> $(LOCUST_CONFIG_FILE); \
79+
echo "locustfile = ./locustfiles/deployment_max_rps_single_endpoint.py" >> $(LOCUST_CONFIG_FILE); \
7780
echo "host = $$host" >> $(LOCUST_CONFIG_FILE); \
7881
echo "users = $$users" >> $(LOCUST_CONFIG_FILE); \
7982
echo "spawn-rate = $$spawn_rate" >> $(LOCUST_CONFIG_FILE); \
@@ -104,22 +107,22 @@ define prompt_for_credentials
104107
endef
105108

106109

107-
test-deployment: _check_venv_active ## runs deployment test on deploy
110+
test-deployment: _check_venv_active ## runs deployment test on deploy, optionally pass endpoint=path to test specific endpoint
108111
@$(call prompt_for_credentials)
109112
@$(call create_locust_config)
110113
@printf "$(YELLOW)Starting Locust...$(NC)\n"
111114
@export $$(cat $(AUTH_CREDS_FILE) | xargs) && \
112-
locust --config $(LOCUST_CONFIG_FILE)
115+
locust --config $(LOCUST_CONFIG_FILE) $(if $(endpoint),--endpoint $(endpoint),--endpoint $(DEFAULT_ENDPOINT))
113116

114-
test-deployment-ci: _check_venv_active ## runs deployment test on CI
117+
test-deployment-ci: _check_venv_active ## runs deployment test on CI, optionally pass endpoint=path to test specific endpoint
115118
@$(call prompt_for_credentials)
116119
@$(call create_locust_config)
117120
@printf "$(YELLOW)Starting Locust headless...$(NC)\n"
118121
@export $$(cat $(AUTH_CREDS_FILE) | xargs) && \
119122
locust --config $(LOCUST_CONFIG_FILE) \
120-
--headless
123+
--headless $(if $(endpoint),--endpoint $(endpoint),--endpoint $(DEFAULT_ENDPOINT))
121124

122-
test-deployment-with-grafana: _check_venv_active grafana-dashboards-up ## runs deployment test with Grafana integration
125+
test-deployment-with-grafana: _check_venv_active grafana-dashboards-up ## runs deployment test with Grafana integration, optionally pass endpoint=path to test specific endpoint
123126
@$(call prompt_for_credentials)
124127
@$(call create_locust_config)
125128
@printf "$(YELLOW)Starting Locust with Grafana integration...$(NC)\n"
@@ -130,7 +133,7 @@ test-deployment-with-grafana: _check_venv_active grafana-dashboards-up ## runs d
130133
--pgport $(PG_PORT) \
131134
--pguser $(PG_USER) \
132135
--pgpassword=$(PG_PASSWORD) \
133-
--timescale
136+
--timescale $(if $(endpoint),--endpoint $(endpoint),--endpoint $(DEFAULT_ENDPOINT))
134137

135138

136139

@@ -164,11 +167,12 @@ update-locust-config: ## Update the Locust configuration file
164167
run_time=$${run_time:-$(DEFAULT_RUN_TIME)}; \
165168
read -p "Host to load test [$(DEFAULT_HOST)]: " host; \
166169
host=$${host:-$(DEFAULT_HOST)}; \
170+
read -p "Endpoint to test [$(DEFAULT_ENDPOINT)]: " endpoint; \
171+
endpoint=$${endpoint:-$(DEFAULT_ENDPOINT)}; \
167172
echo; \
168173
echo "# Locust configuration file - Autogenerated" > $(LOCUST_CONFIG_FILE); \
169174
echo "[locust]" >> $(LOCUST_CONFIG_FILE); \
170-
echo "locustfile = deployment_max_rps_test.py" >> $(LOCUST_CONFIG_FILE); \
171-
echo "headless = true" >> $(LOCUST_CONFIG_FILE); \
175+
echo "locustfile = ./locustfiles/deployment_max_rps_single_endpoint.py" >> $(LOCUST_CONFIG_FILE); \
172176
echo "host = $$host" >> $(LOCUST_CONFIG_FILE); \
173177
echo "users = $$users" >> $(LOCUST_CONFIG_FILE); \
174178
echo "spawn-rate = $$spawn_rate" >> $(LOCUST_CONFIG_FILE); \

tests/performance2/common/deploy_auth.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ class MonitoringBasicAuth(BaseSettings):
66
model_config = SettingsConfigDict(extra="ignore")
77
SC_USER_NAME: str = Field(default=..., examples=["<your username>"])
88
SC_PASSWORD: str = Field(default=..., examples=["<your password>"])
9+
10+
def to_auth(self) -> tuple[str, str]:
11+
return (self.SC_USER_NAME, self.SC_PASSWORD)
Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
11
#
22
# SEE https://docs.locust.io/en/stable/quickstart.html
33
#
4+
# This script allows testing the maximum RPS against a single endpoint.
5+
# Usage:
6+
# locust -f deployment_max_rps_single_endpoint.py --endpoint /v0/health
7+
#
8+
# If no endpoint is specified, the root endpoint ("/") will be used by default.
9+
#
410

511
import logging
612

713
import locust_plugins
814
from common.deploy_auth import MonitoringBasicAuth
9-
from locust import task
15+
from locust import events, task
1016
from locust.contrib.fasthttp import FastHttpUser
1117

1218
logging.basicConfig(level=logging.INFO)
1319

14-
1520
# NOTE: 'import locust_plugins' is necessary to use --check-fail-ratio
1621
# this assert is added to avoid that pycln pre-commit hook does not
1722
# remove the import (the tool assumes the import is not necessary)
1823
assert locust_plugins # nosec
1924

25+
# Use a mutable object to store endpoint
26+
_endpoint_holder = {"endpoint": "/"}
27+
28+
29+
def add_endpoint_argument(parser):
30+
parser.add_argument(
31+
"--endpoint",
32+
type=str,
33+
default="/",
34+
help="The endpoint to test (e.g., /v0/health)",
35+
)
36+
37+
38+
# Register the custom argument with Locust's parser
39+
@events.init_command_line_parser.add_listener
40+
def _(parser):
41+
add_endpoint_argument(parser)
42+
43+
44+
@events.init.add_listener
45+
def _(environment, **_kwargs):
46+
_endpoint_holder["endpoint"] = environment.parsed_options.endpoint
47+
logging.info("Testing endpoint: %s", _endpoint_holder["endpoint"])
48+
2049

2150
class WebApiUser(FastHttpUser):
2251
def __init__(self, *args, **kwargs):
2352
super().__init__(*args, **kwargs)
24-
_auth = MonitoringBasicAuth()
25-
self.auth = (
26-
_auth.SC_USER_NAME,
27-
_auth.SC_PASSWORD,
28-
)
53+
self.auth = MonitoringBasicAuth().to_auth()
54+
self.endpoint = _endpoint_holder["endpoint"]
2955

3056
@task
31-
def get_root(self):
32-
self.client.get("", auth=self.auth)
57+
def get_endpoint(self):
58+
self.client.get(self.endpoint, auth=self.auth)

0 commit comments

Comments
 (0)