Skip to content

Commit 56dd65e

Browse files
committed
perf: pyinstrument integration
1 parent 264c3d0 commit 56dd65e

File tree

5 files changed

+974
-837
lines changed

5 files changed

+974
-837
lines changed

agents-core/vision_agents/core/agents/agents.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -531,14 +531,13 @@ async def finish(self):
531531
Subscribes to the edge transport's `call_ended` event and awaits it. If
532532
no connection is active, returns immediately.
533533
"""
534-
# If connection is None or already closed, return immediately
535534
if not self._connection:
536535
self.logger.info(
537536
"🔚 Agent connection is already closed, finishing immediately"
538537
)
539538
return
540539

541-
540+
running_event = asyncio.Event()
542541
with self.span("agent.finish"):
543542
# If connection is None or already closed, return immediately
544543
if not self._connection:
@@ -549,15 +548,16 @@ async def finish(self):
549548

550549
@self.edge.events.subscribe
551550
async def on_ended(event: CallEndedEvent):
551+
running_event.set()
552552
self._is_running = False
553+
# TODO: add members count check (particiapnts left + count = 1 timeout 2 minutes)
553554

554-
while self._is_running:
555-
try:
556-
await asyncio.sleep(0.0001)
557-
except asyncio.CancelledError:
558-
self._is_running = False
555+
try:
556+
await running_event.wait()
557+
except asyncio.CancelledError:
558+
running_event.clear()
559559

560-
await asyncio.shield(self.close())
560+
await asyncio.shield(self.close())
561561

562562
async def close(self):
563563
"""Clean up all connections and resources.

examples/01_simple_agent_example/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "simple-agents-example"
33
version = "0.0.0"
44
requires-python = ">=3.13"
55

6-
# put only what this example needs
6+
# Dependencies that the example actually uses
77
dependencies = [
88
"python-dotenv>=1.0",
99
"vision-agents-plugins-deepgram",
@@ -19,6 +19,7 @@ dependencies = [
1919
"anthropic>=0.66.0",
2020
"google-genai>=1.33.0",
2121
"fal-client>=0.5.3",
22+
"pyinstrument>=5.1.1",
2223
]
2324

2425
[tool.uv.sources]
@@ -30,5 +31,4 @@ dependencies = [
3031
"vision-agents-plugins-smart-turn" = {path = "../../plugins/smart_turn", editable=true}
3132
"vision-agents-plugins-cartesia" = {path = "../../plugins/cartesia", editable=true}
3233
"vision-agents-plugins-gemini" = {path = "../../plugins/gemini", editable=true}
33-
3434
"vision-agents" = {path = "../../agents-core", editable=true}

examples/01_simple_agent_example/simple_agent_example.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
from uuid import uuid4
33
from dotenv import load_dotenv
4+
import pyinstrument
45

56
from vision_agents.core import User, Agent
67
from vision_agents.plugins import cartesia, deepgram, getstream, gemini, vogent
@@ -9,6 +10,8 @@
910

1011

1112
async def start_agent() -> None:
13+
profiler = pyinstrument.Profiler()
14+
profiler.start()
1215
llm = gemini.LLM("gemini-2.0-flash")
1316
# create an agent to run with Stream's edge, openAI llm
1417
agent = Agent(
@@ -56,6 +59,10 @@ async def start_agent() -> None:
5659
# run till the call ends
5760
await agent.finish()
5861

62+
profiler.stop()
63+
with open('profiled.html', 'w') as f:
64+
f.write(profiler.output_html())
65+
5966

6067
def setup_telemetry():
6168
import atexit

0 commit comments

Comments
 (0)