fix langsmith load test

This commit is contained in:
Ishaan Jaff 2024-09-11 21:16:16 -07:00
parent a1f8fcfeed
commit b01a42ef4f

View file

@ -13,46 +13,56 @@ import pytest
def test_langsmith_logging_async(): def test_langsmith_logging_async():
# this tests time added to make langsmith logging calls, vs just acompletion calls
try: try:
os.environ["LANGSMITH_API_KEY"] = "lsv2_anything" os.environ["LANGSMITH_API_KEY"] = "lsv2_anything"
os.environ["LANGSMITH_PROJECT"] = "pr-b" os.environ["LANGSMITH_PROJECT"] = "pr-b"
os.environ["LANGSMITH_BASE_URL"] = "http://0.0.0.0:8090" os.environ["LANGSMITH_BASE_URL"] = "http://0.0.0.0:8090"
# Make 5 calls with an empty success_callback percentage_diffs = []
litellm.success_callback = []
litellm.callbacks = []
litellm._async_success_callback = []
litellm._async_failure_callback = []
litellm._async_failure_callback = []
litellm.failure_callback = []
start_time_empty_callback = asyncio.run(make_async_calls())
print("done with no callback test")
print("starting langsmith test") for run in range(3):
# Make 5 calls with success_callback set to "langsmith" print(f"\nRun {run + 1}:")
litellm.success_callback = ["langsmith"]
start_time_langsmith = asyncio.run(make_async_calls())
print("done with langsmith test")
# Compare the time for both scenarios # Test with empty success_callback
print(f"Time taken with success_callback='langsmith': {start_time_langsmith}") litellm.success_callback = []
print(f"Time taken with empty success_callback: {start_time_empty_callback}") litellm.callbacks = []
litellm._async_success_callback = []
litellm._async_failure_callback = []
litellm.failure_callback = []
start_time_empty_callback = asyncio.run(make_async_calls())
print("Done with no callback test")
# Calculate the percentage difference # Test with langsmith callback
percentage_diff = ( print("Starting langsmith test")
abs(start_time_langsmith - start_time_empty_callback) litellm.success_callback = ["langsmith"]
/ start_time_empty_callback start_time_langsmith = asyncio.run(make_async_calls())
* 100 print("Done with langsmith test")
)
# Assert that the difference is not more than 10% # Compare times and calculate percentage difference
print(f"Time with success_callback='langsmith': {start_time_langsmith}")
print(f"Time with empty success_callback: {start_time_empty_callback}")
percentage_diff = (
abs(start_time_langsmith - start_time_empty_callback)
/ start_time_empty_callback
* 100
)
percentage_diffs.append(percentage_diff)
print(f"Performance difference: {percentage_diff:.2f}%")
print("percentage_diffs", percentage_diffs)
# Calculate average percentage difference
avg_percentage_diff = sum(percentage_diffs) / len(percentage_diffs)
print(f"\nAverage performance difference: {avg_percentage_diff:.2f}%")
# Assert that the average difference is not more than 10%
assert ( assert (
percentage_diff < 10 avg_percentage_diff < 10
), f"Performance difference of {percentage_diff:.2f}% exceeds 10% threshold" ), f"Average performance difference of {avg_percentage_diff:.2f}% exceeds 10% threshold"
print(f"Performance difference: {percentage_diff:.2f}%") except litellm.Timeout as e:
pass
except Exception as e:
pytest.fail(f"An exception occurred - {e}")
except litellm.Timeout as e: except litellm.Timeout as e:
pass pass
@ -61,52 +71,23 @@ def test_langsmith_logging_async():
async def make_async_calls(metadata=None, **completion_kwargs): async def make_async_calls(metadata=None, **completion_kwargs):
tasks = [] total_tasks = 300
for _ in range(100): batch_size = 100
tasks.append(create_async_task()) total_time = 0
# Measure the start time before running the tasks for batch in range(3):
start_time = asyncio.get_event_loop().time() tasks = [create_async_task() for _ in range(batch_size)]
# Wait for all tasks to complete start_time = asyncio.get_event_loop().time()
responses = await asyncio.gather(*tasks) responses = await asyncio.gather(*tasks)
# Print the responses when tasks return for idx, response in enumerate(responses):
for idx, response in enumerate(responses): print(f"Response from Task {batch * batch_size + idx + 1}: {response}")
print(f"Response from Task {idx + 1}: {response}")
await asyncio.sleep(1) await asyncio.sleep(1)
for _ in range(100): batch_time = asyncio.get_event_loop().time() - start_time
tasks.append(create_async_task()) total_time += batch_time
# Measure the start time before running the tasks
start_time = asyncio.get_event_loop().time()
# Wait for all tasks to complete
responses = await asyncio.gather(*tasks)
# Print the responses when tasks return
for idx, response in enumerate(responses):
print(f"Response from Task {idx + 1}: {response}")
await asyncio.sleep(1)
for _ in range(100):
tasks.append(create_async_task())
# Measure the start time before running the tasks
start_time = asyncio.get_event_loop().time()
# Wait for all tasks to complete
responses = await asyncio.gather(*tasks)
# Print the responses when tasks return
for idx, response in enumerate(responses):
print(f"Response from Task {idx + 1}: {response}")
# Calculate the total time taken
total_time = asyncio.get_event_loop().time() - start_time
return total_time return total_time