o
    NK&h                     @   sf   d Z ddlZddlZddlZddlmZ ddlZddlm	Z	 dZ
dZi Zddd	ZG d
d deZdS )z;
Simple perf tests for aiosqlite and the asyncio run loop.
    N)IsolatedAsyncioTestCase   )setup_loggerz:memory:g       @c                    s   p j  fdd}|S )ab  
    Decorator for perf testing a block of async code.

    Expects the wrapped function to return an async generator.
    The generator should do setup, then yield when ready to start perf testing.
    The decorator will then pump the generator repeatedly until the target
    time has been reached, then close the generator and print perf results.
    c               
      s    | i |}| d I d H  d}t }	 |d7 }t | tk }z|r/| |I d H  n	| I d H  W n%W n" tyB   Y n ty[ } ztd|  W Y d }~d S d }~ww qt | }||ft< d S )Nr   Tr   zexception occurred: )asendtimeTARGETacloseStopAsyncIteration	ExceptionprintRESULTS)argskwargsgencountbeforevalueedurationfnname G/var/www/html/venv/lib/python3.10/site-packages/aiosqlite/tests/perf.pywrapper!   s0   ztimed.<locals>.wrapper)__name__)r   r   r   r   r   r   timed   s   

r   c                   @   s   e Zd Zedd Zedd Zedd Zedd Zed	d
 Z	edd Z
edd Zedd Zedd Zedd Zdd ZdS )PerfTestc                 C   s   t dtdd t  d S )Nz Running perf tests for at least z.1fz	s each...)r   r   r   )clsr   r   r   
setUpClassA   s   
zPerfTest.setUpClassc              
   C   sr   t dddddd ttD ]&}t| \}}|| }|dd}t |dd	|d
d|dd|dd qd S )N
z	Perf Testz<25z Iterations  Duration  Ratez>11test_  z>10z  z>7.1fzs  z>9.1fz/s)r   sortedr   replace)r   r   r   r   rater   r   r   tearDownClassF   s   *zPerfTest.tearDownClassc              	   C  sJ   	 d V  t t4 I d H  W d   I d H  n1 I d H sw   Y  qN)	aiosqliteconnectTEST_DB)selfr   r   r   test_connection_memoryO   s   (zPerfTest.test_connection_memoryc              
   C  s   t jddb}|j}|  t|4 I d H "}|dI d H  |dI d H  | I d H  W d   I d H  n1 I d H s@w   Y  	 d V  t|4 I d H  W d   I d H  n1 I d H scw   Y  qF1 slw   Y  d S )NF)delete8create table perf (i integer primary key asc, k integer)$insert into perf (k) values (2), (3))tempfileNamedTemporaryFiler   closer*   r+   executecommit)r-   tfpathdbr   r   r   test_connection_fileV   s$   
((zPerfTest.test_connection_filec              
   C  s   t t4 I d H C}|dI d H  |dI d H  | I d H  	 d V  |d4 I d H }| I d H  W d   I d H  n1 I d H sGw   Y  q#1 I d H sSw   Y  d S )Nr0   r1   Tselect last_insert_rowid()r*   r+   r,   r5   r6   fetchoner-   r9   cursorr   r   r   test_atomicsh   s   (zPerfTest.test_atomicsc              	   C  sv   t t4 I d H $}|dI d H  | I d H  	 d V  |dI d H  | I d H  q1 I d H s4w   Y  d S )Nr0   Tz)insert into perf (k) values (1), (2), (3))r*   r+   r,   r5   r6   r-   r9   r   r   r   test_insertst      zPerfTest.test_insertsc              	   C  s   t t4 I d H 3}|dI d H  | I d H  	 d V  |dI d H }|dI d H  | I d H  | I d H  q1 I d H sCw   Y  d S )Nr0   Tinsert into perf (k) values (1)r;   r<   r>   r   r   r   test_insert_ids   s   zPerfTest.test_insert_idsc              	   C  sv   t t4 I d H $}|dI d H  | I d H  	 d V  |dI d H  | I d H  q1 I d H s4w   Y  d S )Nr0   TrD   )r*   r+   r,   r5   r6   execute_insertrA   r   r   r   test_insert_macro_ids   rC   zPerfTest.test_insert_macro_idsc              	   C  s   t t4 I d H <}|dI d H  tdD ]}|d|f I d H  q| I d H  	 d V  |dI d H }t| I d H dksEJ q-1 I d H sLw   Y  d S Nr0   d   z insert into perf (k) values (%d)Tzselect i, k from perf)r*   r+   r,   r5   ranger6   lenfetchall)r-   r9   ir?   r   r   r   test_select   s   zPerfTest.test_selectc              	   C  s   t t4 I d H 5}|dI d H  tdD ]}|d|f I d H  q| I d H  	 d V  t|dI d H dks>J q-1 I d H sEw   Y  d S rH   )r*   r+   r,   r5   rJ   r6   rK   execute_fetchall)r-   r9   rM   r   r   r   test_select_macro   s   zPerfTest.test_select_macroc                    s   t t4 I d H ^  dI d H  tdD ]"}|d } dg dd t||d D I d H    I d H  qdtf fdd	}d
d tddD D ]}t|d| |I d H  qMW d   I d H  d S 1 I d H snw   Y  d S )Nz]create table ic_perf (i integer primary key asc, k integer, a integer, b integer, c char(16))   i   z3insert into ic_perf (k, a, b, c) values(?, 1, 2, ?)c                 S   s   g | ]}|t jfqS r   )stringascii_lowercase.0rM   r   r   r   
<listcomp>   s    z6PerfTest.test_iterable_cursor_perf.<locals>.<listcomp>
chunk_sizec              	     sd   	   d4 I d H }| |_|2 z	3 d H W }d V  q6 W d   I d H  n1 I d H s,w   Y  q)NTzSELECT * FROM ic_perf)r5   iter_chunk_size)rW   r?   _r9   r   r   	test_perf   s   (z5PerfTest.test_iterable_cursor_perf.<locals>.test_perfc                 S   s   g | ]}d | qS )   r   rT   r   r   r   rV      s          ziterable_cursor @ )	r*   r+   r,   r5   rJ   executemanyr6   intr   )r-   batchr_startr[   rW   r   rZ   r   test_iterable_cursor_perf   s*   

	.z"PerfTest.test_iterable_cursor_perfN)r   
__module____qualname__classmethodr   r(   r   r.   r:   r@   rB   rE   rG   rN   rP   rc   r   r   r   r   r   @   s,    











r   r)   )__doc__rR   r2   r   unittestr   TestCaser*   smoker   r,   r   r   r   r   r   r   r   r   <module>   s   
+