o
    `j                     @   sv  d Z ddlZddlZddlZddlZddlZddlmZ ddl	m
Z dd edD Zdd edD Zd	d ed
D ZdZdd edD Zdd Zdd Zdd Zedkreejdkrdejd ndZeejdkrteejd ndZedkree dS edkree dS edkree dS edkree ed ee ed ee dS ed ed dS dS )a  Per-function and per-line profiling of new protocol encode/decode hot methods.

Uses cProfile for call-level breakdown and a targeted manual timer
for line-level insight into the key hot methods.

Usage:
    python kafka/benchmarks/profile_protocol.py [encode|decode|create] [N]

    encode  - profile encode path (default)
    decode  - profile decode path
    create  - profile object creation path
    N       - number of iterations (default: 1000)
    N)MetadataResponse)FetchResponsec                 C   s   g | ]}|d | d| fqS )zbroker-%d.example.comi#   ).0ir   r   _/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/benchmarks/profile_protocol.py
<listcomp>   s    r      c                 C   s&   g | ]}d ||d g dd dgfqS )r   r	   )r         r
   r   r   pr   r   r   r      s   & c                 C   s   g | ]	}d d| t fqS )r   test-topic-%d)_PARTITIONSr   tr   r   r   r      s       s                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   c                 C   s&   g | ]}d | dd t dD fqS )r   c                 S   s$   g | ]}|d d| d| dt fqS )r     i  N)_RECORDS_BYTESr   r   r   r   r       s    z<listcomp>.<listcomp>r	   )ranger   r   r   r   r      s    r   c                 C   s   t dttd}tddtd}|jdd |jdd t }|  t	| D ]}|jdd |jdd q&|
  td td|   td t|}|d |d	 t  |d
 |d	 dS )z?Profile the encode path for MetadataResponse and FetchResponse.r   versionbrokerstopics   r   throttle_time_ms	responsesr   P================================================================================zFENCODE PROFILE (%d iterations: MetadataResponse v0 + FetchResponse v4)
cumulative(   tottimeN)NewMetadataResp_BROKERS_TOPICSNewFetchResp_FETCH_TOPICSencodecProfileProfileenabler   disableprintpstatsStats
sort_statsprint_stats)nobj_meta	obj_fetchpr_statsr   r   r   profile_encode(   s&   



r8   c                 C   s  t dttd}tddtd}|jdd}|jdd}t jt|dd tjt|dd t	
 }|  t| D ]}t jt|dd tjt|dd q:|  td td|   td t|}|d |d	 t  |d
 |d	 dS )z?Profile the decode path for MetadataResponse and FetchResponse.r   r   r   r   r   r   zFDECODE PROFILE (%d iterations: MetadataResponse v0 + FetchResponse v4)r    r!   r"   N)r#   r$   r%   r&   r'   r(   decodeioBytesIOr)   r*   r+   r   r,   r-   r.   r/   r0   r1   )r2   r3   r4   encoded_metaencoded_fetchr5   r6   r7   r   r   r   profile_decodeC   s*   



r>   c                 C   s   t dttd tddtd t }|  t| D ]}t dttd tddtd q|	  t
d t
d|   t
d t|}|d |d t
  |d	 |d d
S )z!Profile the object creation path.r   r   r   r   r   zFCREATE PROFILE (%d iterations: MetadataResponse v0 + FetchResponse v4)r    r!   r"   N)r#   r$   r%   r&   r'   r)   r*   r+   r   r,   r-   r.   r/   r0   r1   )r2   r5   r6   r7   r   r   r   profile_create`   s"   



r?   __main__r
   r(   r   r9   createallz

z@Usage: python profile_protocol.py [encode|decode|create|all] [N])__doc__r)   r:   r.   systime kafka.protocol.metadata.metadatar   r#   kafka.protocol.consumer.fetchr   r&   r   r$   r   r%   r   r'   r8   r>   r?   __name__lenargvmodeint
iterationsr-   exitr   r   r   r   <module>   sH   	 