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: object

Number of calls of a method and the total time they took, in seconds. Calls that raise are counted too.

__init__()#
Return type:

None

record(elapsed)#

Adds a call that took elapsed seconds.

Parameters:

elapsed (float)

Return type:

None

property total_time: float#

Total time of all calls, in seconds.

property mean_time: float#

Mean time per call, in seconds. 0 if there were no calls.

class ProfiledQueue#

Bases: Queue[MIn, MOut]

Wraps a queue and profiles its push(), pop() and wait_pop().

Times include waiting: a wait_pop() or a pop() with a timeout lasts until a message arrives, or the timeout expires. Empty pops raising a NoMessagesException are counted too.

Parameters:

queue (Queue) – The profiled queue.

__init__(queue)#
Parameters:

queue (Queue[MIn, MOut])

Return type:

None

property queue: Queue[MIn, MOut]#

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:

MOut

Raises a NoMessagesException if 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.

Parameters:

timeout (float) – Seconds to wait for a message.

Return type:

MOut

Raises a NoMessagesException if 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:
Return type:

None