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 )zProfiling 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   ^/var/www/html/ecg_monitoring/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 )ProfileStatsFilezStore 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 )Nc                   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rtjtj\}}tdd }	d||	|pud	f }
||
 td
|
  tjrt| d S |rt ||  }t!|| |k}|rtjrt| d S t"d|| d |tj#f d S d S )NzcProfile is not installedzNo 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 %szAdjusted 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   	 
&