o
    Šåg¤'  ã                   @  sÐ   d Z ddlmZ 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	Z	ddl
mZ ddlmZ ddlmZ zddlZW n eyG   dZY nw dZ	 da	 d	d
„ ZG dd„ dƒZddd„Zejddd„ƒZdS )z§Profiling support for unit and performance tests.

These are special purpose profiling methods which operate
in a more fine-grained way than nose's profiling plugin.

é    )ÚannotationsNé   )Úconfig)Ú
gc_collecté   )Úhas_compiled_extc                 C  s   | a tjrt ¡  d S d S ©N)Ú_current_testÚ_profile_statsÚforce_writeÚreset_count)Úid_© r   úX/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/testing/profiling.pyÚ_start_current_test6   s   ÿr   c                   @  s^   e Zd ZdZddd„Zedd„ ƒZdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚProfileStatsFilezøStore per-platform/fn profiling results in a file.

    There was no json module available when this was written, but now
    the file format which is very deterministically line oriented is kind of
    handy in any case for diffs and merges.

    Ú
cumulativeNc                 C  sŠ   t jd uot jj| _| jpt jd uot jj| _tj |¡| _	tj 
| j	¡d | _t dd„ ¡| _|| _|| _|  ¡  | jrC|  ¡  d S d S )Néÿÿÿÿc                   S  s
   t  t¡S r   )ÚcollectionsÚdefaultdictÚdictr   r   r   r   Ú<lambda>Q   s   
 z+ProfileStatsFile.__init__.<locals>.<lambda>)r   ÚoptionsÚforce_write_profilesr   Úwrite_profilesÚwriteÚosÚpathÚabspathÚfnameÚsplitÚshort_fnamer   r   ÚdataÚdumpÚsortÚ_readÚ_write)ÚselfÚfilenamer$   r#   r   r   r   Ú__init__G   s    ÿÿÿýzProfileStatsFile.__init__c                 C  s¦   t jjd t jj }t jjdkrt jj t jj¡r|d7 }d dd„ tj	dd… D ƒ¡}t
 ¡ t
 ¡  ¡ t
 ¡  ¡ ||g}| d	¡ tƒ }| |rKd
pLd¡ d |¡S )NÚ_ÚsqliteÚ_fileÚ.c                 S  ó   g | ]}t |ƒ‘qS r   ©Ústr)Ú.0Úvr   r   r   Ú
<listcomp>e   ó    z1ProfileStatsFile.platform_key.<locals>.<listcomp>r   r   ÚdbapiunicodeÚcextensionsÚnocextensions)r   ÚdbÚnameÚdriverÚdialectÚ_is_url_file_dbÚurlÚjoinÚsysÚversion_infoÚplatformÚmachineÚsystemÚlowerÚpython_implementationÚappendr   )r'   Ú	dbapi_keyÚ
py_versionÚplatform_tokensÚ	_has_cextr   r   r   Úplatform_key[   s    ÿ

û

zProfileStatsFile.platform_keyc                 C  s   t }|| jv o| j| j| v S r   ©r	   r"   rK   )r'   Útest_keyr   r   r   Ú	has_statst   s   ÿzProfileStatsFile.has_statsc           	      C  s¦   t }| j| }|| j }d|vrg  |d< }n|d }d|vr&d |d< }n|d }t|ƒ|k}|sA| |¡ | jr>|  ¡  d }n|d || f}|d  d7  < |S )NÚcountsÚcurrent_countr   Úlinenor   )r	   r"   rK   ÚlenrF   r   r&   )	r'   Ú	callcountrM   Úper_fnÚper_platformrO   rP   Ú	has_countÚresultr   r   r   rW   z   s$   


zProfileStatsFile.resultc                 C  sT   t }|| jvr	d S | j| }| j|vrd S || j }d|v r(g |d d d …< d S d S )NrO   rL   )r'   rM   rT   rU   r   r   r   r   •   s   



ÿzProfileStatsFile.reset_countc                 C  s`   t }| j| }|| j }|d }|d }|t|ƒk r!|||d < n||d< | jr.|  ¡  d S d S )NrO   rP   r   r   )r	   r"   rK   rR   r   r&   )r'   rS   rM   rT   rU   rO   rP   r   r   r   Úreplace¢   s   

ÿzProfileStatsFile.replacec                 C  s
   d| j  S )Na  # %s
# This file is written out on a per-environment basis.
# For each test in aaa_profiling, the corresponding function and 
# environment is located within this file.  If it doesn't exist,
# the test is skipped.
# If a callcount does exist, it is compared to what we received. 
# assertions are raised if the counts do not match.
# 
# To add a new callcount test, apply the function_call_count 
# decorator and re-run the tests using the --write-profiles 
# option - this file will be rewritten including the new count.
# 
)r   )r'   r   r   r   Ú_header¯   s   ðzProfileStatsFile._headerc           
      C  sª   zt | jƒ}W n
 ty   Y d S w t|ƒD ]8\}}| ¡ }|r%| d¡r&q| ¡ \}}}| j| }|| }dd„ | d¡D ƒ}	|	|d< |d |d< d|d	< q| ¡  d S )
Nú#c                 S  r.   r   )Úint©r1   Úcountr   r   r   r3   Ï   r4   z*ProfileStatsFile._read.<locals>.<listcomp>ú,rO   r   rQ   r   rP   )	Úopenr   ÚOSErrorÚ	enumerateÚstripÚ
startswithr    r"   Úclose)
r'   Ú	profile_frQ   ÚlinerM   rK   rO   rT   rU   Úcr   r   r   r%   Â   s"   ÿ

zProfileStatsFile._readc                 C  sž   t d| j ƒ t| jdƒ}| |  ¡ ¡ t| jƒD ]/}| j| }| d| ¡ t|ƒD ]}|| }d dd„ |d D ƒ¡}| d|||f ¡ q+q| ¡  d S )	NzWriting profile file %sÚwz
# TEST: %s

r^   c                 s  s   � | ]}t |ƒV  qd S r   r/   r\   r   r   r   Ú	<genexpr>Þ   s   € z*ProfileStatsFile._write.<locals>.<genexpr>rO   z	%s %s %s
)	Úprintr   r_   r   rY   Úsortedr"   r>   rd   )r'   re   rM   rT   rK   rU   rg   r   r   r   r&   Õ   s   
ýzProfileStatsFile._write)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   ÚpropertyrK   rN   rW   r   rX   rY   r%   r&   r   r   r   r   r   >   s    

r   çš™™™™™©?c                   s$   ddl m} |‡ ‡‡fdd„ƒ}|S )a©  Assert a target for a test case's function call count.

    The main purpose of this assertion is to detect changes in
    callcounts for various functions - the actual number is not as important.
    Callcounts are stored in a file keyed to Python version and OS platform
    information.  This file is generated automatically for new tests,
    and versioned so that unexpected changes in callcounts will be detected.

    r   )Ú	decoratorc                   sn   t ˆƒD ]	}| |i |¤Ž qt ˆ ƒ}tˆd�� |D ]	}| |i |¤Ž}q|W  d   ƒ S 1 s0w   Y  d S )N)Úvariance)ÚrangeÚcount_functions)ÚfnÚargsÚkwÚwarmÚ	timerangeÚtimeÚrv©Útimesrs   Úwarmupr   r   Úwrapô   s   $ýz!function_call_count.<locals>.wrap)Úsqlalchemy.utilrr   )rs   r~   r   rr   r€   r   r}   r   Úfunction_call_countã   s   
r‚   c                 c  st  � t d u r
t d¡‚t ¡ stjst dtj ¡ tƒ  t  	¡ }| 
¡  d V  | ¡  tj|tjd�}|j}t |¡}|d u rBd }n|\}}td||f ƒ |jt dtj¡Ž  | ¡  tjrƒtj tj¡\}}t d¡d }	d||	|pud	f }
| |
¡ td
|
 ƒ tjr�t |¡ d S |r¶t ||  ƒ}t!|| ƒ|k}|r¸tjr©t |¡ d S t"d|| d |tj#f ƒ‚d S d S )NzcProfile is not installedzŒNo profiling stats available on this platform for this function.  Run tests with --write-profiles to add statistics to %s for this platform.)ÚstreamzPstats calls: %d Expected %sz[, ]r-   r   z%s_%s%sz.profilezDumped stats to file %sz†Adjusted function call count %s not within %s%% of expected %s, platform %s. Rerun with --write-profiles to regenerate this callcount.éd   )$ÚcProfiler   Ú_skip_test_exceptionr
   rN   r   Ú	skip_testr!   r   ÚProfileÚenableÚdisableÚpstatsÚStatsr?   ÚstdoutÚtotal_callsrW   rj   Ú
sort_statsÚrer    r$   Úprint_statsr#   r   r   Úsplitextr	   Ú
dump_statsr   rX   r[   ÚabsÚAssertionErrorrK   )rs   ÚprÚstatsrS   ÚexpectedÚexpected_countÚline_noÚbaseÚextÚ	test_nameÚdumpfileÚdevianceÚfailedr   r   r   ru     s`   €
ýÿ

üüÿøru   )rq   r   r   )rq   )ro   Ú
__future__r   r   Ú
contextlibr   rA   r‹   r�   r?   Ú r   Úutilr   r   r…   ÚImportErrorr
   r	   r   r   r‚   Úcontextmanagerru   r   r   r   r   Ú<module>   s6   	ÿ 
&