Profiling#
Profiling decorators for queues and listeners.
They wrap an existing queue or listener, delegate every call to it, and measure how many times its main methods are called and how long they take:
queue = ProfiledQueue(InMemoryQueue())
listener = ProfiledListener(MyListener())
channel.register_connection(Connection(listener=listener, queue=queue))
...
print(queue.push_stats.calls, queue.push_stats.total_time, listener.on_message_stats.mean_time)
- class CallStats#
Bases:
objectNumber of calls of a method and the total time they took, in seconds. Calls that raise are counted too.
- __init__()#
- Return type:
None
- class ProfiledQueue#
-
Wraps a queue and profiles its
push(),pop()andwait_pop().Times include waiting: a
wait_pop()or apop()with a timeout lasts until a message arrives, or the timeout expires. Empty pops raising aNoMessagesExceptionare counted too.- Parameters:
queue (Queue) – The profiled queue.
- pop(timeout=0)#
Next message from the queue.
- Parameters:
timeout (float) – Seconds to wait for a message. With 0 it returns immediately.
- Return type:
Raises a
NoMessagesExceptionif no message is available, after waiting timeout seconds.
- async wait_pop(timeout)#
Waits for the next message without blocking the event loop.
The default implementation runs
pop()with timeout in a worker thread, so it takes one thread per waiting call. Override it with a native async wait when possible.Raises a
NoMessagesExceptionif no message arrives within timeout.
- class ProfiledListener#
Bases:
MessageQueueListener[M]Wraps a listener and profiles its
on_message().It delegates every other call to it.
- Parameters:
listener (MessageQueueListener) – The profiled listener.
- __init__(listener)#
- Parameters:
listener (MessageQueueListener[M])
- Return type:
None
- property listener: MessageQueueListener[M]#
The profiled listener.
- on_message(msg)#
Event handler. It executes when a message is delivered to client
- Parameters:
msg (M)
- Return type:
None
- on_registered(channel, connection_id)#
Hook called when the listener is registered to channel, through connection connection_id, see
register_connection(). Also called when a channel is loaded from persistence. Does nothing by default: override it if the listener needs its channel or its address, e.g. to dispatch messages.- Parameters:
channel (AbstractChannel[Any, M])
connection_id (str)
- Return type:
None