Page MenuHomePhabricator
Paste P55186

[locust] article descriptions isvc load test script with process_payload()
ActivePublic

Authored by kevinbazira on Jan 22 2024, 11:15 AM.
import os
import re
import pandas as pd
from datetime import datetime
from locust import FastHttpUser, task, between, events
from locust_plugins.csvreader import CSVReader
inputs_reader = CSVReader("article_descriptions_inputs.csv")
def lw_stats_analysis():
df = pd.read_csv("lw_stats_history.csv")
df = df.sort_values(by=["Timestamp"], ascending=True)
deltas = df.diff().iloc[1:]
latest_deltas = deltas.iloc[-1]
is_worse = latest_deltas["Average Response Time"] > 0 or latest_deltas["Failure Count"] > 0
performance, difference = ("worse", "regression") if is_worse else ("better", "improvement")
analysis_data = latest_deltas[["Average Response Time", "Failure Count"]].to_string().replace("Failure Count", " Failure Count")
analysis_report = """
LIFTWING LOAD TEST COMPLETED
The most recent load test for this isvc has been analyzed and presents {difference} in performance:
{analysis_data}
Compared with the prior load test, the performance is {performance}.
Detailed results of the current and past load tests are located in: lw_stats_history.csv.
""".format(performance=performance, difference=difference, analysis_data=analysis_data)
print(analysis_report)
@events.test_stop.add_listener
def lw_stats_history(**kwargs):
df = pd.read_csv("article_descriptions_stats.csv")
aggregated_row = df[df["Name"] == "Aggregated"]
if not aggregated_row.empty:
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
aggregated_data = aggregated_row.drop(columns=["Type", "Name"])
aggregated_data.insert(0, "Timestamp", current_time)
if not os.path.isfile("lw_stats_history.csv"):
header = ["Timestamp"] + [col for col in df.columns if col not in ["Type", "Name"]]
aggregated_data.to_csv("lw_stats_history.csv", mode="w", index=False, header=header)
first_report = """
LIFTWING LOAD TEST COMPLETED
The first load test for this isvc has been successfully executed and recorded.
At present, a comparative analysis is not available due to the absence of preceding test data.
Future tests will enable us to present a comparative performance analysis.
For load test results, please refer to: lw_stats_history.csv.
"""
print(first_report)
else:
aggregated_data.to_csv("lw_stats_history.csv", mode="a", index=False, header=False)
lw_stats_analysis()
class ArticleDescriptionsUser(FastHttpUser):
wait_time = between(1, 2) # Adjust wait time as needed
host = "https://inference-staging.svc.codfw.wmnet:30443"
def process_payload(self, data):
payload = {}
pattern = r"(\w+)\s(.*?)\s(\d+)$"
match = re.search(pattern, data)
if match:
payload = {
"lang": match.group(1),
"title": match.group(2),
"num_beams": int(match.group(3))
}
else:
payload = {
"lang": None,
"title": None,
"num_beams": None
}
return payload
@task
def predict(self):
data = next(inputs_reader)
payload = self.process_payload(data[0])
headers = {
"Host": "article-descriptions.experimental.wikimedia.org",
"Content-Type": "application/json"
}
self.client.post("/v1/models/article-descriptions:predict",
json=payload,
headers=headers)