Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified load-test.zip
Binary file not shown.
23 changes: 11 additions & 12 deletions load-test/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def payment_create(self):
if response.status_code == 200:
self.payment_id = json.loads(response.text)["payment_id"]

# Capture sample log for successful payment create
if CAPTURE_LOGS and not sample_logs_captured["create"]:
# Capture sample log for successful payment create (only if both are not already captured)
if CAPTURE_LOGS and not sample_logs_captured["create"] and not sample_logs_captured["confirm"]:
global sample_payment_id
sample_payment_id = self.payment_id # Store the payment_id for matching
sample_logs["payment_create"] = {
Expand Down Expand Up @@ -133,7 +133,7 @@ def payment_confirm(self):
status = json.loads(response.text)["status"]
print("Payment status: ", status)

# Capture sample log for successful payment confirm only for the same payment_id
# If confirm succeeds and payment_id matches, capture log and mark confirm as true
if CAPTURE_LOGS and sample_logs_captured["create"] and not sample_logs_captured["confirm"] and self.payment_id == sample_payment_id:
sample_logs["payment_confirm"] = {
"api": "payment_confirm",
Expand All @@ -148,12 +148,12 @@ def payment_confirm(self):
error_message = response.json().get("error", "Unknown error")
print(error_message)

# If confirm fails for the tracked payment, reset and try next transaction
if CAPTURE_LOGS and sample_logs_captured["create"] and self.payment_id == sample_payment_id:
# If confirm fails and create is true and confirm is false, clear logs
if CAPTURE_LOGS and sample_logs_captured["create"] and not sample_logs_captured["confirm"]:
sample_logs.clear()
sample_logs_captured["create"] = False
sample_payment_id = None
print(f"❌ Payment confirm failed for {self.payment_id}, resetting sample logs to try next transaction")
print(f"❌ Payment confirm failed, resetting sample logs to try next transaction")


def calculate_and_save_p99():
Expand All @@ -169,13 +169,12 @@ def calculate_and_save_p99():
print("No x-hs-latency values collected")

def save_sample_logs():
"""Save sample logs to a single file if captured"""
if CAPTURE_LOGS and sample_logs:
# Create output directory if it doesn't exist
os.makedirs("output", exist_ok=True)
with open("output/sample_logs.json", "w") as f:
"""Save sample logs to a single file if both APIs are captured"""
if CAPTURE_LOGS and sample_logs_captured["create"] and sample_logs_captured["confirm"]:
# Use a temporary filename that script.py will rename appropriately
with open(".sample_logs.json", "w") as f:
json.dump(sample_logs, f, indent=2)
print(f"✅ Sample logs saved: output/sample_logs.json ({len(sample_logs)} APIs)")
print(f"✅ Sample logs saved: .sample_logs.json ({len(sample_logs)} APIs)")

class TestUser(HttpUser):
wait_time = constant(0)
Expand Down
12 changes: 10 additions & 2 deletions load-test/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async def run_locust(test_spec, users,run_time, file_name):

# Set environment variables for the locust process
env = os.environ.copy()
if CAPTURE_SAMPLE_LOGS and "normal" in file_name:
if CAPTURE_SAMPLE_LOGS:
env["CAPTURE_SAMPLE_LOGS"] = "true"

subprocess.Popen([
Expand All @@ -214,11 +214,19 @@ async def run_locust(test_spec, users,run_time, file_name):
except FileNotFoundError:
pass

# Store in appropriate global variable
# Store in appropriate global variable and handle sample logs
if "normal" in file_name:
NORMAL_APPLICATION_LATENCY = p99_value
test_type = "normal"
else:
SPIKE_APPLICATION_LATENCY = p99_value
test_type = "spike"

# Handle sample logs if they exist
if CAPTURE_SAMPLE_LOGS and os.path.exists(".sample_logs.json"):
output_log_file = f"output/sample_logs_{test_type}.json"
shutil.move(".sample_logs.json", output_log_file)
print(f" 📄 Sample logs saved: {output_log_file}")

output_file = Path(f'output/temp/{file_name}.pdf').resolve()
input_file = Path(f'output/temp/{file_name}.html').resolve()
Expand Down