a
    ze"                     @   s   d 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
mZmZmZ ddlmZ ddlZddlmZmZ ddlmZmZ G d	d
 d
ejZdd Zedd Zdd Zi Zded< ded< ded< edkre  dS )z"Test suite for the profile module.    N)unified_diff)StringIO)TESTFNunlinktemp_dir
change_cwd)contextmanager)testfunctimer)assert_python_failureassert_python_okc                   @   sn   e Zd ZejZeZg dZdZdd Z	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S )ProfileTest)print_statsprint_callersprint_calleesz:0(max)c                 C   s   t t d S N)r   r   self r   '/usr/lib/python3.9/test/test_profile.pytearDown   s    zProfileTest.tearDownc                 C   s   t S r   )_ProfileOutputr   r   r   r   get_expected_output   s    zProfileTest.get_expected_outputc                    s   g }|  td}t }|dt t  |t |  | jD ]r}t }tj	||d}|
 d t||  |  }tjddd   fdd|D }|d	| q>|S )
NMbP?
testfunc()streamZstdname.   c                    s   g | ]} |v r|  qS r   )rstrip).0lineZmod_namer   r   
<listcomp>/       z,ProfileTest.do_profiling.<locals>.<listcomp>
)profilerclassr
   runctxglobalslocalsappendmethodnamesr   pstatsStatsZ
strip_dirsZ
sort_statsgetattrgetvalue
splitlinesr	   
__module__rsplitjoin)clsresultsprofZstart_timerZ
methodnamesstatsoutputr   r"   r   do_profiling   s    
zProfileTest.do_profilingc                 C   s   |   }|  }| |d d g }t| jD ]`\}}|| }||d  }||kr.|d| d| jj d |t	|
d|
ddd	 q.|r| d| d S )
Nr   i  r   z
Stats.z output for z does not fit expectation:r%    )Zlineterm)r:   r   ZassertEqual	enumerater+   r*   r&   __name__extendr   splitfailr3   )r   r5   Zexpectedr@   imethodabr   r   r   test_cprofile3   s     

zProfileTest.test_cprofilec              	   C   sp   g d}|D ]^}t  }| td}||t t  tj||d}|  |	 }| 
| j|d|| qd S )N)zmax([0])zmax([0], key=int)zmax([0], **dict(key=int))zmax(*([0],))zmax(*([0],), key=int)zmax(*([0],), **dict(key=int))r   r   z&Profiling {0!r} didn't report max:
{1})r   r&   r
   r'   r(   r)   r,   r-   r   r/   ZassertInexpected_max_outputformat)r   ZstmtsZstmtr7   r6   r8   resr   r   r   test_calling_conventionsD   s    

z$ProfileTest.test_calling_conventionsc                 C   sX   t   | jd W d    n1 s(0    Y  | jjdtd | tjt d S )Nzint('1')filename)silentprofilermodulerunr   
assertTrueospathexistsr   r   r   r   test_runY   s    *zProfileTest.test_runc                 C   sh   t  $ | jdt t  W d    n1 s00    Y  | jjdt t td | tj	t d S )Nr   rJ   )
rL   rM   r'   r(   r)   r   rO   rP   rQ   rR   r   r   r   r   test_runctx_   s    2zProfileTest.test_runctxc                 C   s<   t d| jjd t d| jjdd td| jjdddd d S )N-mZrandom_module_xyzZtimeitz-n1)r   rM   r=   r   r   r   r   r   test_run_profile_as_modulef   s    

z&ProfileTest.test_run_profile_as_modulec              
   C   s   t  }t|t td tdd}|d W d    n1 sF0    Y  td| jjddd | 	tj
d W d    n1 s0    Y  W d    n1 s0    Y  d S )Ndestzdemo.pywzimport os; os.chdir("dest")rU   z-oz
out.pstats)r   r   rP   mkdiropenwriter   rM   r=   rO   rQ   rR   )r   Ztmpdirfr   r   r   (test_output_file_when_changing_directoryr   s    
(z4ProfileTest.test_output_file_when_changing_directoryN)r=   r1   __qualname__profileZProfiler&   rM   r+   rF   r   r   classmethodr:   rE   rI   rS   rT   rW   r^   r   r   r   r   r      s   
r   c              	   C   s   |  d} td|   | }g }t| d2}|D ]}|| |dr2 qPq2W d    n1 sd0    Y  t| d\}|| |d t|j	D ]"\}}|d|||d  f  q|d	 W d    n1 s0    Y  d S )
NcozRegenerating %s...rz#--cutrY   z_ProfileOutput = {}
z _ProfileOutput[%r] = """\
%s"""
r   z'
if __name__ == "__main__":
    main()
)
r   printr:   r[   r*   
startswith
writelinesr\   r<   r+   )rK   r4   r5   newfiler]   r!   rA   rB   r   r   r   regenerate_expected_output   s"    


$


rh   c                  c   s,   t j} zt t _d V  W | t _n| t _0 d S r   )sysstdoutr   )rj   r   r   r   rL      s
    rL   c                   C   s"   dt jvrt  n
ttt d S )Nz-r)ri   argvunittestmainrh   __file__r   r   r   r   r   rm      s    

rm   a         28   27.972    0.999   27.972    0.999 profilee.py:110(__getattr__)
        1  269.996  269.996  999.769  999.769 profilee.py:25(testfunc)
     23/3  149.937    6.519  169.917   56.639 profilee.py:35(factorial)
       20   19.980    0.999   19.980    0.999 profilee.py:48(mul)
        2   39.986   19.993  599.830  299.915 profilee.py:55(helper)
        4  115.984   28.996  119.964   29.991 profilee.py:73(helper1)
        2   -0.006   -0.003  139.946   69.973 profilee.py:84(helper2_indirect)
        8  311.976   38.997  399.912   49.989 profilee.py:88(helper2)
        8   63.976    7.997   79.960    9.995 profilee.py:98(subhelper)r   a  :0(append)                        <- profilee.py:73(helper1)(4)  119.964
:0(exc_info)                      <- profilee.py:73(helper1)(4)  119.964
:0(hasattr)                       <- profilee.py:73(helper1)(4)  119.964
                                     profilee.py:88(helper2)(8)  399.912
profilee.py:110(__getattr__)      <- :0(hasattr)(12)   11.964
                                     profilee.py:98(subhelper)(16)   79.960
profilee.py:25(testfunc)          <- <string>:1(<module>)(1)  999.767
profilee.py:35(factorial)         <- profilee.py:25(testfunc)(1)  999.769
                                     profilee.py:35(factorial)(20)  169.917
                                     profilee.py:84(helper2_indirect)(2)  139.946
profilee.py:48(mul)               <- profilee.py:35(factorial)(20)  169.917
profilee.py:55(helper)            <- profilee.py:25(testfunc)(2)  999.769
profilee.py:73(helper1)           <- profilee.py:55(helper)(4)  599.830
profilee.py:84(helper2_indirect)  <- profilee.py:55(helper)(2)  599.830
profilee.py:88(helper2)           <- profilee.py:55(helper)(6)  599.830
                                     profilee.py:84(helper2_indirect)(2)  139.946
profilee.py:98(subhelper)         <- profilee.py:88(helper2)(8)  399.912r   a  :0(hasattr)                       -> profilee.py:110(__getattr__)(12)   27.972
<string>:1(<module>)              -> profilee.py:25(testfunc)(1)  999.769
profilee.py:110(__getattr__)      ->
profilee.py:25(testfunc)          -> profilee.py:35(factorial)(1)  169.917
                                     profilee.py:55(helper)(2)  599.830
profilee.py:35(factorial)         -> profilee.py:35(factorial)(20)  169.917
                                     profilee.py:48(mul)(20)   19.980
profilee.py:48(mul)               ->
profilee.py:55(helper)            -> profilee.py:73(helper1)(4)  119.964
                                     profilee.py:84(helper2_indirect)(2)  139.946
                                     profilee.py:88(helper2)(6)  399.912
profilee.py:73(helper1)           -> :0(append)(4)   -0.004
profilee.py:84(helper2_indirect)  -> profilee.py:35(factorial)(2)  169.917
                                     profilee.py:88(helper2)(2)  399.912
profilee.py:88(helper2)           -> :0(hasattr)(8)   11.964
                                     profilee.py:98(subhelper)(8)   79.960
profilee.py:98(subhelper)         -> profilee.py:110(__getattr__)(16)   27.972r   __main__)__doc__ri   r,   rl   rP   Zdifflibr   ior   Ztest.supportr   r   r   r   
contextlibr   r`   Ztest.profileer	   r
   Ztest.support.script_helperr   r   ZTestCaser   rh   rL   rm   r   r=   r   r   r   r   <module>   s,   p
		
