B
    ‰KLcF)  ã               @   sÄ   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ZddlZddlm	Z	 ddl
mZ ddl
mZ yddlZW n ek
r„   dZY nX dZdadd	„ ZG d
d„ deƒ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.

é    Né   )Úconfig)Ú
gc_collecté   )Úhas_compiled_extc             C   s   | a tjrt ¡  d S )N)Ú_current_testÚ_profile_statsÚforce_writeÚreset_count)Zid_© r   ú;lib/python3.7/site-packages/sqlalchemy/testing/profiling.pyÚ_start_current_test2   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 k	ot jj| _| jp*t jd k	o*t jj| _tj |¡| _	tj 
| j	¡d | _t dd„ ¡| _|| _|| _|  ¡  | jr‚|  ¡  d S )Néÿÿÿÿc               S   s
   t  t¡S )N)ÚcollectionsÚdefaultdictÚdictr   r   r   r   Ú<lambda>M   ó    z+ProfileStatsFile.__init__.<locals>.<lambda>)r   ZoptionsZforce_write_profilesr	   Zwrite_profilesÚwriteÚosÚpathÚabspathÚfnameÚsplitÚshort_fnamer   r   ÚdataÚdumpÚsortÚ_readÚ_write)ÚselfÚfilenamer   r   r   r   r   Ú__init__C   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}| t jjjrŒd	nd
¡ tƒ }| |r¤dp¦d¡ d |¡S )NÚ_ZsqliteZ_fileÚ.c             S   s   g | ]}t |ƒ‘qS r   )Ústr)Ú.0Úvr   r   r   ú
<listcomp>b   s    z1ProfileStatsFile.platform_key.<locals>.<listcomp>r   r   ZnativeunicodeZdbapiunicodeZcextensionsZnocextensions)r   ZdbÚnameZdriverZdialectZ_is_url_file_dbZurlÚjoinÚsysÚversion_infoÚplatformÚmachineÚsystemÚlowerZpython_implementationÚappendZconvert_unicoder   )r"   Z	dbapi_keyZ
py_versionZplatform_tokensZ	_has_cextr   r   r   Úplatform_keyW   s     


zProfileStatsFile.platform_keyc             C   s   t }|| jko| j| j| kS )N)r   r   r4   )r"   Útest_keyr   r   r   Ú	has_statsu   s    zProfileStatsFile.has_statsc       	      C   s¦   t }| j| }|| j }d|kr.g  |d< }n|d }d|krLd |d< }n|d }t|ƒ|k}|s‚| |¡ | jr||  ¡  d }n|d || f}|d  d7  < |S )NÚcountsÚcurrent_countr   Úlinenor   )r   r   r4   Úlenr3   r   r!   )	r"   Ú	callcountr5   Úper_fnÚper_platformr7   r8   Z	has_countÚresultr   r   r   r>   {   s$    


zProfileStatsFile.resultc             C   sP   t }|| jkrd S | j| }| j|kr*d S || j }d|krLg |d d d …< d S )Nr7   )r   r   r4   )r"   r5   r<   r=   r   r   r   r
   –   s    



zProfileStatsFile.reset_countc             C   s\   t }| j| }|| j }|d }|d }|t|ƒk rB|||d < n||d< | jrX|  ¡  d S )Nr7   r8   r   r   )r   r   r4   r:   r   r!   )r"   r;   r5   r<   r=   r7   r8   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®   yt | jƒ}W n tk
r"   d S X x|t|ƒD ]p\}}| ¡ }|r.| d¡rNq.| ¡ \}}}| j| }|| }dd„ | d¡D ƒ}	|	|d< |d |d< d|d	< q.W | ¡  d S )
Nú#c             S   s   g | ]}t |ƒ‘qS r   )Úint)r(   Úcountr   r   r   r*   Ð   s    z*ProfileStatsFile._read.<locals>.<listcomp>ú,r7   r   r9   r   r8   )	Úopenr   ÚIOErrorÚ	enumerateÚstripÚ
startswithr   r   Úclose)
r"   Ú	profile_fr9   Úliner5   r4   r7   r<   r=   Úcr   r   r   r    Ã   s     
zProfileStatsFile._readc             C   s¦   t d| j ƒ t| jdƒ}| |  ¡ ¡ xpt| jƒD ]b}| j| }| d| ¡ xDt|ƒD ]8}|| }d dd„ |d D ƒ¡}| d|||f ¡ qZW q4W | ¡  d S )	NzWriting profile file %sÚwz
# TEST: %s

rD   c             s   s   | ]}t |ƒV  qd S )N)r'   )r(   rC   r   r   r   ú	<genexpr>à   s    z*ProfileStatsFile._write.<locals>.<genexpr>r7   z	%s %s %s
)	Úprintr   rE   r   r@   Úsortedr   r,   rJ   )r"   rK   r5   r<   r4   r=   rM   r   r   r   r!   Ö   s    
zProfileStatsFile._write)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   Úpropertyr4   r6   r>   r
   r?   r@   r    r!   r   r   r   r   r   :   s   
r   çš™™™™™©?c                sN   ddl m} ddl m‰  ddlm‰ 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   )Ú	decorator)Údeprecations)Úrow)Úmockc                s’   ˆj  ˆ dd¡x ˆj  ˆjdˆj¡X xtˆƒD ]}| ||Ž q2W tˆƒ}tˆd x|D ]}| ||Ž}q^W |S Q R X W d Q R X W d Q R X d S )NZSQLALCHEMY_WARN_20FZ_default_key_style)Úvariance)ZpatchÚobjectZ	LegacyRowZKEY_OBJECTS_NO_WARNÚrangeÚcount_functions)ÚfnÚargsÚkwZwarmZ	timerangeZtimeÚrv)rY   r[   rZ   Útimesr\   Úwarmupr   r   Úwrapù   s    
z!function_call_count.<locals>.wrap)Zsqlalchemy.utilrX   rY   Zsqlalchemy.enginerZ   Zsqlalchemy.testingr[   )r\   rd   re   rX   rf   r   )rY   r[   rZ   rd   r\   re   r   Úfunction_call_countå   s    rg   c             c   st  t d krt d¡‚t ¡ s0tjs0t dtj ¡ tƒ  t  	¡ }| 
¡  d V  | ¡  tj|tjd}|j}t |¡}|d kr‚d }n|\}}td||f ƒ |jt dtj¡Ž  | ¡  tjrtj tj¡\}}t d¡d }	d||	|pêd	f }
| |
¡ td
|
 ƒ tjrt |¡ nV|rpt ||  ƒ}t!|| ƒ|k}|rptjrVt |¡ nt"d|| d |tj#f ƒ‚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   Z_skip_test_exceptionr   r6   r   Z	skip_testr   r   ZProfileÚenableÚdisableÚpstatsZStatsr-   ÚstdoutZtotal_callsr>   rP   Z
sort_statsÚrer   r   Zprint_statsr   r   r   Úsplitextr   Z
dump_statsr	   r?   rB   ÚabsÚAssertionErrorr4   )r\   ZprZstatsr;   ZexpectedZexpected_countZline_noÚbaseZextZ	test_nameZdumpfileZdevianceZfailedr   r   r   r_     sP    



r_   )rW   r   r   )rW   )rU   r   Ú
contextlibr   r/   rm   ro   r-   Ú r   Úutilr   r   rj   ÚImportErrorr   r   r   r]   r   rg   Úcontextmanagerr_   r   r   r   r   Ú<module>   s,   
	 ,
(