Skip to content

Commit 9fd27d9

Browse files
committed
a slightly gentler adjustment to locust
1 parent 4972c53 commit 9fd27d9

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/load-generator/locustfile.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,32 @@ class WebsiteUser(HttpUser):
6060
wait_time = between(1, 10)
6161

6262
def on_start(self):
63-
# Disable keep-alive to prevent connection reuse
64-
self.client.headers.update({"Connection": "close"})
6563
session_id = str(uuid.uuid4())
6664
logging.info(f"Starting user session: {session_id}")
6765
self.index()
66+
67+
def reset_connection(self):
68+
"""Force a new connection by closing the existing session pool"""
69+
# Close all adapters in the session to force new connections
70+
self.client.close()
71+
# Locust will automatically create a new session on next request
6872

6973
@task(1)
7074
def index(self):
75+
self.reset_connection()
7176
logging.info("User accessing index page")
7277
self.client.get("/")
7378

7479
@task(10)
7580
def browse_product(self):
81+
self.reset_connection()
7682
product = random.choice(products)
7783
logging.info(f"User browsing product: {product}")
7884
self.client.get("/api/products/" + product)
7985

8086
@task(3)
8187
def get_recommendations(self):
88+
self.reset_connection()
8289
product = random.choice(products)
8390
logging.info(f"User getting recommendations for product: {product}")
8491
params = {
@@ -88,6 +95,7 @@ def get_recommendations(self):
8895

8996
@task(3)
9097
def get_ads(self):
98+
self.reset_connection()
9199
category = random.choice(categories)
92100
logging.info(f"User getting ads for category: {category}")
93101
params = {
@@ -97,11 +105,13 @@ def get_ads(self):
97105

98106
@task(3)
99107
def view_cart(self):
108+
self.reset_connection()
100109
logging.info("User viewing cart")
101110
self.client.get("/api/cart")
102111

103112
@task(2)
104113
def add_to_cart(self, user=""):
114+
# Don't reset connection here since this is called by other tasks
105115
if user == "":
106116
user = str(uuid.uuid1())
107117
product = random.choice(products)
@@ -119,6 +129,7 @@ def add_to_cart(self, user=""):
119129

120130
@task(1)
121131
def checkout(self):
132+
self.reset_connection()
122133
user = str(uuid.uuid1())
123134
self.add_to_cart(user=user)
124135
checkout_person = random.choice(people)
@@ -128,6 +139,7 @@ def checkout(self):
128139

129140
@task(1)
130141
def checkout_multi(self):
142+
self.reset_connection()
131143
user = str(uuid.uuid1())
132144
item_count = random.choice([2, 3, 4])
133145
for i in range(item_count):
@@ -139,6 +151,7 @@ def checkout_multi(self):
139151

140152
@task(5)
141153
def flood_home(self):
154+
self.reset_connection()
142155
flood_count = get_flagd_value("loadGeneratorFloodHomepage")
143156
if flood_count > 0:
144157
logging.info(f"User flooding homepage {flood_count} times")

0 commit comments

Comments
 (0)