📝 chore(locustfile.py): add locustfile.py for load testing the API
🐛 fix(test_process.py): refactor test_load_langchain_object_with_cached_session to use session_manager 🐛 fix(test_process.py): refactor test_load_langchain_object_with_no_cached_session to use session_manager 🐛 fix(test_process.py): refactor test_load_langchain_object_without_session_id to use session_manager
This commit is contained in:
parent
a7ff7da778
commit
853648ae33
3 changed files with 73 additions and 801 deletions
47
tests/locust/locustfile.py
Normal file
47
tests/locust/locustfile.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
from locust import HttpUser, task, between
|
||||
import random
|
||||
|
||||
|
||||
class NameTest(HttpUser):
|
||||
host = "http://localhost:/api/v1"
|
||||
wait_time = between(1, 5)
|
||||
|
||||
# Read names from the file
|
||||
with open("names.txt", "r") as file:
|
||||
names = [line.strip() for line in file.readlines()]
|
||||
|
||||
@task
|
||||
def send_name_and_check(self):
|
||||
# Select a random name or in order from the list
|
||||
name = random.choice(self.names)
|
||||
flow_id = "0bc439e4-539c-4b18-9813-92729326b171" # Replace with the appropriate flow ID
|
||||
random_session_id_with_name = f"{name}-{random.randint(0, 1000000)}"
|
||||
session_id = None
|
||||
# First input
|
||||
payload1 = {
|
||||
"inputs": {"text": f"Hello, My name is {name}"},
|
||||
"session_id": random_session_id_with_name,
|
||||
}
|
||||
with self.client.post(
|
||||
f"/process/{flow_id}", json=payload1, catch_response=True
|
||||
) as response:
|
||||
if response.status_code != 200:
|
||||
response.failure(f"Error: {response.json()}")
|
||||
else:
|
||||
response.success()
|
||||
|
||||
session_id = response.json().get("session_id")
|
||||
print(f"Session ID: {session_id}")
|
||||
|
||||
if not session_id:
|
||||
raise ValueError("Session ID not found")
|
||||
|
||||
# Second input
|
||||
payload2 = {"inputs": {"text": "What is my name?"}, "session_id": session_id}
|
||||
with self.client.post(
|
||||
f"/process/{flow_id}", json=payload2, catch_response=True
|
||||
) as response2:
|
||||
if name not in response2.text:
|
||||
response2.failure(f"Error {name} not in response: {response2.json()}")
|
||||
else:
|
||||
response2.success()
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
from langflow.interface.run import build_sorted_vertices_with_caching
|
||||
from langflow.processing.process import load_langchain_object, process_tweaks
|
||||
from langflow.processing.process import process_tweaks
|
||||
from langflow.services.utils import get_session_manager
|
||||
|
||||
|
||||
def test_no_tweaks():
|
||||
|
|
@ -198,51 +198,50 @@ def test_tweak_not_in_template():
|
|||
|
||||
|
||||
def test_load_langchain_object_with_cached_session(client, basic_graph_data):
|
||||
# Build the langchain_object once and get the session_id
|
||||
langchain_object1, artifacts1, session_id1 = load_langchain_object(
|
||||
basic_graph_data, None
|
||||
# Provide a non-existent session_id
|
||||
session_manager = get_session_manager()
|
||||
session_id1 = "non-existent-session-id"
|
||||
langchain_object1, artifacts1 = session_manager.load_session(
|
||||
session_id1, basic_graph_data
|
||||
)
|
||||
# Use the same session_id to get the langchain_object again
|
||||
langchain_object2, artifacts2, session_id2 = load_langchain_object(
|
||||
basic_graph_data, session_id1
|
||||
# Use the new session_id to get the langchain_object again
|
||||
langchain_object2, artifacts2 = session_manager.load_session(
|
||||
session_id1, basic_graph_data
|
||||
)
|
||||
|
||||
assert session_id1 == session_id2
|
||||
assert id(langchain_object1) == id(langchain_object2)
|
||||
assert artifacts1 == artifacts2
|
||||
|
||||
|
||||
def test_load_langchain_object_with_no_cached_session(client, basic_graph_data):
|
||||
# Provide a non-existent session_id
|
||||
langchain_object1, artifacts1, session_id1 = load_langchain_object(
|
||||
basic_graph_data, "non_existent_session"
|
||||
session_manager = get_session_manager()
|
||||
session_id1 = "non-existent-session-id"
|
||||
langchain_object1, artifacts1 = session_manager.load_session(
|
||||
session_id1, basic_graph_data
|
||||
)
|
||||
# Clear the cache
|
||||
build_sorted_vertices_with_caching.clear_cache(session_id1)
|
||||
session_manager.clear_session(session_id1, basic_graph_data)
|
||||
# Use the new session_id to get the langchain_object again
|
||||
langchain_object2, artifacts2, session_id2 = load_langchain_object(
|
||||
basic_graph_data, session_id1
|
||||
langchain_object2, artifacts2 = session_manager.load_session(
|
||||
session_id1, basic_graph_data
|
||||
)
|
||||
|
||||
assert session_id1 == session_id2
|
||||
assert id(langchain_object1) != id(
|
||||
langchain_object2
|
||||
) # Since the cache was cleared, objects should be different
|
||||
|
||||
|
||||
def test_load_langchain_object_without_session_id(client, basic_graph_data):
|
||||
# Build the langchain_object without providing a session_id
|
||||
langchain_object1, artifacts1, session_id1 = load_langchain_object(
|
||||
basic_graph_data, None
|
||||
# Provide a non-existent session_id
|
||||
session_manager = get_session_manager()
|
||||
session_id1 = None
|
||||
langchain_object1, artifacts1 = session_manager.load_session(
|
||||
session_id1, basic_graph_data
|
||||
)
|
||||
# Build the langchain_object again without providing a session_id
|
||||
langchain_object2, artifacts2, session_id2 = load_langchain_object(
|
||||
basic_graph_data, None
|
||||
# Use the new session_id to get the langchain_object again
|
||||
langchain_object2, artifacts2 = session_manager.load_session(
|
||||
session_id1, basic_graph_data
|
||||
)
|
||||
|
||||
assert session_id1 == session_id2
|
||||
|
||||
assert id(langchain_object1) == id(
|
||||
langchain_object2
|
||||
) # Since no session_id was provided, the hash will be based on the graph_data
|
||||
assert artifacts1 == artifacts2
|
||||
assert id(langchain_object1) == id(langchain_object2)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue