33from __future__ import annotations
44
55import inspect
6- from typing import TYPE_CHECKING
6+ from typing import TYPE_CHECKING , overload
77
88import logistro
99
1010from choreographer import protocol
1111
1212if TYPE_CHECKING :
1313 import asyncio
14- from typing import Any , Callable , Coroutine , MutableMapping
14+ from typing import Any , Callable , Coroutine , Literal , MutableMapping
1515
1616 from choreographer ._brokers import Broker
1717
@@ -56,11 +56,34 @@ def __init__(self, session_id: str, broker: Broker) -> None:
5656 self .message_id = 0
5757 self .subscriptions = {}
5858
59+ @overload
5960 async def send_command (
6061 self ,
6162 command : str ,
6263 params : MutableMapping [str , Any ] | None = None ,
63- ) -> protocol .BrowserResponse :
64+ * ,
65+ with_perf : Literal [False ] = False ,
66+ ) -> protocol .BrowserResponse : ...
67+
68+ @overload
69+ async def send_command (
70+ self ,
71+ command : str ,
72+ params : MutableMapping [str , Any ] | None = None ,
73+ * ,
74+ with_perf : Literal [True ],
75+ ) -> tuple [protocol .BrowserResponse , tuple [float , float , float ]]: ...
76+
77+ async def send_command (
78+ self ,
79+ command : str ,
80+ params : MutableMapping [str , Any ] | None = None ,
81+ * ,
82+ with_perf : bool = False ,
83+ ) -> (
84+ tuple [protocol .BrowserResponse , tuple [float , float , float ]]
85+ | protocol .BrowserResponse
86+ ):
6487 """
6588 Send a devtools command on the session.
6689
@@ -69,9 +92,12 @@ async def send_command(
6992 Args:
7093 command: devtools command to send
7194 params: the parameters to send
95+ with_perf (bool): Return the optional tuple.
7296
7397 Returns:
7498 A message key (session, message id) tuple or None
99+ (Optional) A tuple[float, float, float] representing
100+ perf_counters() for write start, end, and read end.
75101
76102 """
77103 current_id = self .message_id
@@ -92,6 +118,11 @@ async def send_command(
92118 f"sessionId '{ self .session_id } '" ,
93119 )
94120 _logger .debug2 (f"Full params: { str (params ).replace ('%' , '%%' )} " )
121+ if with_perf :
122+ return (
123+ await self ._broker .write_json (json_command ),
124+ self ._broker .get_perf (json_command ),
125+ )
95126 return await self ._broker .write_json (json_command )
96127
97128 def subscribe (
@@ -201,11 +232,34 @@ def get_session(self) -> Session:
201232 session = next (iter (self .sessions .values ()))
202233 return session
203234
235+ @overload
204236 async def send_command (
205237 self ,
206238 command : str ,
207239 params : MutableMapping [str , Any ] | None = None ,
208- ) -> protocol .BrowserResponse :
240+ * ,
241+ with_perf : Literal [False ] = False ,
242+ ) -> protocol .BrowserResponse : ...
243+
244+ @overload
245+ async def send_command (
246+ self ,
247+ command : str ,
248+ params : MutableMapping [str , Any ] | None = None ,
249+ * ,
250+ with_perf : Literal [True ],
251+ ) -> tuple [protocol .BrowserResponse , tuple [float , float , float ]]: ...
252+
253+ async def send_command (
254+ self ,
255+ command : str ,
256+ params : MutableMapping [str , Any ] | None = None ,
257+ * ,
258+ with_perf : bool = False ,
259+ ) -> (
260+ protocol .BrowserResponse
261+ | tuple [protocol .BrowserResponse , tuple [float , float , float ]]
262+ ):
209263 """
210264 Send a command to the first session in a target.
211265
@@ -214,12 +268,13 @@ async def send_command(
214268 Args:
215269 command: devtools command to send
216270 params: the parameters to send
271+ with_perf (bool): Also return perf tuple
217272
218273 """
219274 if not self .sessions .values ():
220275 raise RuntimeError ("Cannot send_command without at least one valid session" )
221276 session = self .get_session ()
222- return await session .send_command (command , params )
277+ return await session .send_command (command , params , with_perf = with_perf )
223278
224279 async def create_session (self ) -> Session :
225280 """Create a new session on this target."""
0 commit comments