o
    g#                     @   s   	 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	 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dd Zi Zded< ded< ded< edkr{e  dS dS )    N)unified_diff)StringIO)run_unittest)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      t t d S N)r   r   self r   G/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_profile.pytearDown      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 ]9}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mod_namer   r   
<listcomp>0   s    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 ]0\}}|| }||d  }||krG|d| d| jj d |t	|
d|
ddd	 q|rT| d| d S 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failr6   )r   r8   ZexpectedrC   imethodabr   r   r   test_cprofile4   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/   r0   r   r2   ZassertInexpected_max_outputformat)r   ZstmtsZstmtr:   r9   r;   resr   r   r   test_calling_conventionsE   s   

z$ProfileTest.test_calling_conventionsc                 C   sX   t   | jd W d    n1 sw   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_runZ   s
   zProfileTest.test_runc                 C   sh   t   | jdt t  W d    n1 sw   Y  | jjdt t td | tj	t d S )Nr   rM   )
rO   rP   r*   r+   r,   r   rR   rS   rT   rU   r   r   r   r   test_runctx`   s   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   rP   r@   r   r   r   r   r   test_run_profile_as_moduleg   s   

z&ProfileTest.test_run_profile_as_modulec              
   C   s   t  Y}t|< td tdddd}|d W d    n1 s%w   Y  td| jjdd	d | 	tj
d	 W d    n1 sGw   Y  W d    d S W d    d S 1 s_w   Y  d S )
Ndestzdemo.pywzutf-8)encodingzimport os; os.chdir("dest")rX   z-oz
out.pstats)r   r   rS   mkdiropenwriter   rP   r@   rR   rT   rU   )r   Ztmpdirfr   r   r   (test_output_file_when_changing_directorys   s   
Pz4ProfileTest.test_output_file_when_changing_directoryN)r@   r4   __qualname__profileZProfiler)   rP   r.   rI   r   r   classmethodr=   rH   rL   rV   rW   rZ   rb   r   r   r   r   r      s    
r   c              	   C   s   |  d} td|   | }g }t| d}|D ]}|| |dr' q(qW d    n1 s2w   Y  t| d/}|| |d t|j	D ]\}}|d|||d  f  qL|d	 W d    d S 1 snw   Y  d S )
NcozRegenerating %s...rz#--cutr\   z_ProfileOutput = {}
z _ProfileOutput[%r] = """\
%s"""
r!   z'
if __name__ == "__main__":
    main()
)
r"   printr=   r_   r-   
startswith
writelinesr`   r?   r.   )rN   r7   r8   newfilera   r$   rD   rE   r   r   r   regenerate_expected_output   s*   





"rl   c                  c   s,    t j} zt t _d V  W | t _d S | t _w r   )sysstdoutr   )rn   r   r   r   rO      s   rO   c                   C   r   r   )r   r   r   r   r   r   	test_main   r   ro   c                   C   s"   dt jvr
t  d S ttt d S )Nz-r)rm   argvro   rl   __file__r   r   r   r   r   main   s   

rr   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__) rm   r/   ZunittestrS   Zdifflibr   ior   Ztest.supportr   Ztest.support.os_helperr   r   r   r   
contextlibr	   rd   Ztest.profileer
   r   Ztest.support.script_helperr   r   ZTestCaser   rl   rO   ro   rr   r   r@   r   r   r   r   <module>   s4    p
	

