o
    `j                     @   s   d dl 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 G dd dZG dd dej	Z
dd Zed	krBe  Zee dS dS )
    N)KafkaProducerc                   @   s   e Zd Zedd ZdS )ProducerPerformancec              	      s  zi } j D ]1}|d\}}zt|}W n	 ty   Y nw |dkr&d }n|dkr-d}n|dkr3d}|||< qtd  j|d< tt j j	d	 |d
< t
di || D ]\}}td|| qZtd j td j td j	 td j t }t j	| jd}|  td t   fdd}t }|  t }	|  |  td|	| d W d S  ty   t }
tj|
  td Y d S w )N=NoneFalseFTrueTzInitializing producer...bootstrap_serversi  metrics_sample_window_msz---> {0}={1}z---> topic={0}z---> send {0} byte recordsz ---> report stats every {0} secsz---> raw metrics? {0})eventraw_metricsz-> OK!c            	         s   g } j }| j} j} j}t|D ]	}||| qtd     d\}}| D ]}| r9|d7 }q.|	 rB|d7 }q.t
|td||f  d S )NzSend complete...)r   r      z%d suceeded, %d failed)sendappendtopicnum_recordsrangeprintflushclose	succeededfailed
ValueError)	resultsr   r   r   r   _count_successcount_failurerargsproducerrecord c/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/benchmarks/producer_performance.py
_benchmark5   s$   

z+ProducerPerformance.run.<locals>._benchmarkzExecution time:secsr   r!   )producer_configsplitintr   r   r   bytes	bytearrayrecord_sizestats_intervalr   itemsformatr   r   	threadingEventStatsReporterstarttime	monotonicsetjoin	Exceptionsysexc_info	tracebackprint_exceptionexit)r   propspropkv
timer_stoptimerr#   
start_timeend_timer8   r!   r   r"   run   s^   



zProducerPerformance.runN)__name__
__module____qualname__staticmethodrD   r!   r!   r!   r"   r      s    r   c                       s6   e Zd Zd fdd	Zdd Zdd Zd	d
 Z  ZS )r0   NFc                    s&   t    || _|| _|| _|| _d S N)super__init__intervalr   r
   r   )selfrL   r   r
   r   	__class__r!   r"   rK   ]   s
   

zStatsReporter.__init__c                 C   sB   | j  }|s	d S | jrt| d S tdjdi |d  d S )Na+  {record-send-rate:.0f} records/sec ({byte-rate:.0f} B/sec), {request-rate:.0f} avg requests/sec, {request-latency-avg:.0f}ms avg latency, {throttle-time-max:.0f}ms max throttle, {record-size-avg:.0f} avg record size, {batch-size-avg:.0f} avg batch size, {records-per-request-avg:.0f} avg records/reqzproducer-metricsr!   )r   metricsr   pprintr   r-   )rM   rP   r!   r!   r"   print_statsd   s   
zStatsReporter.print_statsc                 C   s   |    d S rI   )rR   rM   r!   r!   r"   print_finalt   s   zStatsReporter.print_finalc                 C   s<   | j r| j | js|   | j r| j | jr
|   d S rI   )r
   waitrL   rR   rT   rS   r!   r!   r"   rD   w   s   zStatsReporter.run)NF)rE   rF   rG   rK   rR   rT   rD   __classcell__r!   r!   rN   r"   r0   \   s
    r0   c                  C   s   t jdd} | jddtdddd | jd	d
tddd | jdtddd | jdtddd | jddtdddd | jdtddd | jdddd | S )Nz5This tool is used to verify the producer performance.)descriptionz-bz--bootstrap-servers+r!   z&host:port for cluster bootstrap server)typenargsdefaulthelpz-tz--topicz:Topic name for test (default: kafka-python-benchmark-test)zkafka-python-benchmark-test)rY   r\   r[   z--num-recordsz0number of messages to produce (default: 1000000)i@B z--record-sizez$message size in bytes (default: 100)d   z-cz--producer-configzWkafka producer related configuaration properties like bootstrap_servers,client_id etc..z--stats-intervalz?Interval in seconds for stats reporting to console (default: 5)   z--raw-metrics
store_truez<Enable this flag to print full metrics dict on each interval)actionr\   )argparseArgumentParseradd_argumentstrr'   )parserr!   r!   r"   get_args_parser~   sH   

rf   __main__)ra   rQ   r7   r.   r2   r9   kafkar   r   Threadr0   rf   rE   
parse_argsr   rD   r!   r!   r!   r"   <module>   s   N"!
