a DOgr@sdZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z gdZGdd d eeZe d d Gd d d Ze d d GdddZGdddZGdddZddZddZddZddZddZddZd d!Zed"krddlZz ddlZWney,Yn0Gd#d$d$ej Z!e"ej#d%kr\ej#d%Z$ndZ$zPe!e$Z%ej#d&dD]Z&e%'e&qxe(d'e%j)d(e%*e(d)e%j)d(Wne+yYn0dS)*z3Class for printing reports on profiled python code.N)Enum) cmp_to_key) dataclass)Dict)StatsSortKeyFunctionProfile StatsProfilec@s8eZdZdZdZdZdZdZdZdZ dZ d Z d d Z d S) r)callsncalls) cumulativecumtime)filenamemodulelinenamenflpcallsstdname)timetottimecGs@|d}t||}||_|ddD]}||j|<q&||_|SNr)str__new___value__value2member_map_Z _all_values)clsvaluesvalueobjZ other_valuer!+/opt/alt/python39/lib64/python3.9/pstats.pyr/s  zSortKey.__new__N) __name__ __module__ __qualname__ZCALLSZ CUMULATIVEZFILENAMEZLINENAMEZNFLZPCALLSZSTDNAMEZTIMErr!r!r!r"r$srT)Z unsafe_hashc@sFeZdZUeed<eed<eed<eed<eed<eed<eed<dS) rr rpercall_tottimer percall_cumtime file_name line_numberN)r#r$r%int__annotations__floatrr!r!r!r"r9s rc@s*eZdZUdZeed<eeefed<dS)r z0Class for keeping track of an item in inventory.total_tt func_profilesN) r#r$r%__doc__r-r,rrrr!r!r!r"r Cs r c@seZdZdZddddZddZdd Zd d Zd d ZddZ dddddddddddddd Z ddZ ddZ ddZ d d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd9d3d4Zd5d6Zd7d8ZdS):raThis class is used for creating reports from data generated by the Profile class. It is a "friend" of that class, and imports data either by direct access to members of Profile class, or by reading in a dictionary that was emitted (via marshal) from the Profile class. The big change from the previous Profiler (in terms of raw functionality) is that an "add()" method has been provided to combine Stats from several distinct profile runs. Both the constructor and the add() method now take arbitrarily many file names as arguments. All the print methods now take an argument that indicates how many lines to print. If the arg is a floating point number between 0 and 1.0, then it is taken as a decimal percentage of the available lines to be printed (e.g., .1 means print 10% of all available lines). If it is an integer, it is taken to mean the number of lines of data that you wish to have printed. The sort_stats() method now processes some additional options (i.e., in addition to the old -1, 0, 1, or 2 that are respectively interpreted as 'stdname', 'calls', 'time', and 'cumulative'). It takes either an arbitrary number of quoted strings or SortKey enum to select the sort order. For example sort_stats('time', 'name') or sort_stats(SortKey.TIME, SortKey.NAME) sorts on the major key of 'internal function time', and on the minor key of 'the name of the function'. Look at the two tables in sort_stats() and get_sort_arg_defs(self) for more examples. All methods return self, so you can string together commands like: Stats('foo', 'goo').strip_dirs().sort_stats('calls'). print_stats(5).print_callers(5) N)streamcGsF|ptj|_t|sd}n|d}|dd}|||j|dSr)sysstdoutr1leninitadd)selfr1argsargr!r!r"__init__ks   zStats.__init__cCsd|_g|_d|_d|_d|_d|_d|_t|_i|_ i|_ | |z | Wn6t ytd|jrv|jdnd|jdYn0dS)NrzInvalid timing data %sfile) all_calleesfilesfcn_listr. total_calls prim_calls max_name_lenset top_levelstats sort_arg_dict load_statsget_top_level_stats Exceptionprintr1)r7r9r!r!r"r5us(   z Stats.initcCs|duri|_dSt|trt|d}t||_Wdn1sH0Yz"t|}t |j d|}Wn Yn0|g|_ n t |dr| |j|_i|_|jstd|j|fdS)Nrbz create_statsz.Cannot create or construct a %r object from %r)rG isinstanceropenmarshalloadosstatrctimest_mtimer@hasattrrN TypeError __class__)r7r9fZ file_statsr!r!r"rIs*  *   zStats.load_statscCs|jD]p\}\}}}}}|j|7_|j|7_|j|7_d|vrZ|j|tt||j kr tt||_ q dS)N)ZjprofilerZprofiler) rGitemsrBrCr.rFr6r4func_std_stringrD)r7funcccncttctcallersr!r!r"rJs zStats.get_top_level_statscGs|s|St|D]}t|t|kr,t|}|j|j7_|j|j7_|j|j7_|j|j7_|jD]}|j|qr|j |j kr|j |_ d|_ |j D]<\}}||j vr|j |}nddddif}t |||j |<qq|SNr)reversedtyperr@rBrCr.rFr6rDrArGr[add_func_stats)r7arg_listitemr]rTZ old_func_statr!r!r"r6s(     z Stats.addcCs<t|d}t|j|Wdn1s.0YdS)z:Write the profile data to a file we know how to load back.wbN)rPrQdumprG)r7rrZr!r!r" dump_statss zStats.dump_stats)))rr;z call count)))r;zcumulative time))rz file name))rz line number))rz function name))rqrmrozname/file/line)))rr;zprimitive call count)))rz standard name)))r;z internal time) r r r r rrrrrrrrrcCst|jsni|_}i}|jD]>\}}|}|r|s4q||vrFd||<q|||<|dd}q*q|D] }||=qb|jS)z)Expand all abbreviations that are unique.rNr;)rHsort_arg_dict_defaultr[)r7dictZbad_listwordtupZfragmentr!r!r"get_sort_arg_defss  zStats.get_sort_arg_defscGs\|sd|_|St|dkrBt|dtrBddddd|dg}n:t|dkr||ddD] }t|t|dkrZtd qZ|}d }d |_d }|D]B}t|tr|j }|||d}|j|||d7_d }qg}|j D]4\}\} } } } } | | | | | f|t ||fq|jtt|jd g|_}|D]}| |dqB|S)Nrrrr rr )r;rrrtrtzCan't have mixed argument typer!r<z, )keyr;)rAr4rOr+rerXry sort_typerrrGr[appendr\sortr TupleCompcompare)r7Zfieldr9Z sort_arg_defsZ sort_tupleZ connectorrwZ stats_listr]r^r_r`rarbrAtupler!r!r" sort_statssF     zStats.sort_statscCs|jr|j|SN)rAreverser7r!r!r" reverse_orders zStats.reverse_ordercCs|j}i|_}d}|D]\}\}}}}} t|} tt| |krRtt| }i} | D]\} } | | t| <q^| |vrt|| ||||| f|| <q||||| f|| <q|j}t|_}|D]}|t|q||_ d|_ d|_ |Src) rGr[func_strip_pathr4r\rfrFrEr6rDrAr?)r7ZoldstatsZnewstatsrDr]r^r_r`rarbZnewfuncZ newcallersfunc2callerZold_topZnew_topr!r!r" strip_dirss0    zStats.strip_dirsc Cst|jr dSi|_}|jD]P\}\}}}}}||vr@i||<|D]$\}} ||vr`i||<| |||<qHqdSr)r?rGr[) r7r?r]r^r_r`rarbrrr!r!r" calc_callees5s zStats.calc_calleescCs|}t|trnzt|}Wn(tjyD|d|7}||fYS0g}|D]}|t|rN||qNnzt|}t|t rd|krdkrnnt ||d}|d|}n2t|t rd|kr|krnn|}|d|}t|t|kr|dt|t||f7}||fS)Nz# gg?g?rz6 List reduced from %r to %r due to restriction <%r> ) rOrrecompileerrorsearchr\r|r4r-r+)r7ZsellistmsgZnew_listZrexr]countr!r!r"eval_print_amountHs.  "" zStats.eval_print_amountc Cs|jr|jddn t|j}|s0tdiStt|j}i}t||}|D]}|j|\}}}} } |\} } } ||krt|nt|dt|}tt|}|dkrdntt||}tt| }|dkrdntt| |}t |||||| | }||| <qP|S)a?This method returns an instance of StatsProfile, which contains a mapping of function names to instances of FunctionProfile. Each FunctionProfile instance holds information related to the function's profile such as how long the function took to run, how many times it was called, etc... Nr/r;) rArrGkeysr r-f8r.rr)r7Z func_listr.r/Z stats_profiler]r^r_r`rarbr)r* func_namer rr'r r(Z func_profiler!r!r"get_stats_profilebs2"   $   zStats.get_stats_profilecCs|j}|jr*|jdd}d|jd}nt|j}d}|D]}||||\}}q@t|}|sld|fSt||j d|t|jkrd}|D] }tt ||krtt |}q|d|fS)Nz Ordered by:  z! Random listing order was used rr=rt) rDrAr{rrGrrr4rLr1r\)r7Zsel_listwidthZ stat_listrZ selectionrr]r!r!r"get_print_lists$zStats.get_print_listcGs|jD]}t||jdq|jr,t|jdd}|jD]}t|t||jdq6t||jdd|jd|j|jkrtd|jd|jdtd|j|jdt|jd||\}}|r| |D]}| |qt|jdt|jd|S)Nr= zfunction calls endr>z(%d primitive calls)zin %.3f seconds) r@rLr1rFfunc_get_function_namerBrCr.r print_title print_line)r7amountrindentr]rrr!r!r" print_statss(        zStats.print_statscGsz||\}}|rv|||d|D]2}||jvrN||||j|q*|||iq*t|jdt|jd|S)Nz called...r=)rrprint_call_headingr?print_call_linerLr1)r7rrrr]r!r!r" print_calleess    zStats.print_calleesc Gsh||\}}|rd||d|D](}|j|\}}}}} |||| dq"t|jdt|jd|S)Nzwas called by...z<-r=)rrrGrrLr1) r7rrrr]r^r_r`rarbr!r!r" print_callerss   zStats.print_callersc Csvtd|||jdd}|jD]0\}}}}}|r&tt|} t| t}qXq&|rrtd|d|jddS)Nz Function r=Frz ncalls tottime cumtime) rLljustr1rGrnextiterrOr) r7 name_sizeZ column_titleZ subheaderr^r_r`rarbrr!r!r"rs zStats.print_call_heading->cCstt|||d|jd|s2t|jddSt|}d}|D]}t|}||} t| tr| \} } } } | | krd| | f}n d| f}d|dd t |t | t | |f}|d }n$d || t |j |d f}|d }t||||jdd}qFdS) Nrrr=r<z%d/%dz%dz %s %s %s %srsrtrz %s(%r) %srl) rLr\rr1sortedrrOrrjustr4rrG)r7rsourceZ call_dictZarrowZclistrr]rrr_r^r`raZsubstatsZ left_widthr!r!r"rs,      zStats.print_call_linecCs"tdd|jdtd|jddS)Nz- ncalls tottime percall cumtime percallrrzfilename:lineno(function)r=rLr1rr!r!r"rszStats.print_titlecCs|j|\}}}}}t|}||kr4|dt|}t|dd|jdtt|d|jd|dkrxtdd|jdntt||d|jdtt|d|jd|dkrtdd|jdntt||d|jdtt||jddS)Nr rrrrr=)rGrrLrr1rr\)r7r]r^r_r`rarbcr!r!r"rszStats.print_line)r)r#r$r%r0r:r5rIrJr6rkruryrrrrrrrrrrrrrrr!r!r!r"rIsH!  '#  rc@s eZdZdZddZddZdS)r~aThis class provides a generic function for comparing any two tuples. Each instance records a list of tuple-indices (from most significant to least significant), and sort direction (ascending or decending) for each tuple-index. The compare functions can then be used as the function argument to the system sort() function when a list of tuples need to be sorted in the instances order.cCs ||_dSrcomp_select_list)r7rr!r!r"r:szTupleComp.__init__cCsF|jD]:\}}||}||}||kr0| S||kr|SqdSrcr)r7leftrightindex directionlrr!r!r"rs  zTupleComp.compareN)r#r$r%r0r:rr!r!r!r"r~ sr~cCs|\}}}tj|||fSr)rSpathbasename)rrrrr!r!r"r"s rcCs|dS)Nrtr!)r]r!r!r"r&srcCsN|dddkrB|d}|dr<|drz{%s}rr;z %s:%d(%s)) startswithendswith)rrr!r!r"r\)s r\c Cs@|\}}}}}|\}}} } } |||||| || t| |fS)z3Add together all the stats for two profile entries.) add_callers) targetrr^r_r`rarbZt_ccZt_ncZt_ttZt_ctZ t_callersr!r!r"rf:s rfcCsi}|D]\}}|||<q |D]V\}}||vrtt|trbtddt|||D||<q||||7<q&|||<q&|S)z*Combine two caller lists in a single list.css|]\}}||VqdSrr!).0ijr!r!r" Jzadd_callers..)r[rOrzip)rrZ new_callersr]rr!r!r"rAs  " rcCsd}|D] }||7}q |S)z@Sum the caller statistics to get total number of calls received.r)r)rbr_r r!r!r" count_callsRs  rcCsd|S)Nz%8.3fr!)xr!r!r"r]sr__main__c@seZdZd6ddZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdS)7ProfileBrowserNcCs6tj|d|_d|_tj|_|dur2||dS)N% ) cmdCmdr:promptrGr2r3r1do_read)r7profiler!r!r"r:ls  zProfileBrowser.__init__c Cs|}g}|D]}z|t|WqWnty<Yn0zAdd profile info from given file to current statistics object.r=rrr!r!r"help_addszProfileBrowser.help_addcCs |d|S)Nrrr7rr!r!r" do_calleesszProfileBrowser.do_calleescCstd|jd|dS)Nz6Print callees statistics from the current stat object.r=rLr1rrr!r!r" help_calleesszProfileBrowser.help_calleescCs |d|S)Nrrrr!r!r" do_callersszProfileBrowser.do_callerscCstd|jd|dS)Nz6Print callers statistics from the current stat object.r=rrr!r!r" help_callersszProfileBrowser.help_callerscCstd|jddS)Nr<r=rrrr!r!r"do_EOFszProfileBrowser.do_EOFcCstd|jddSNzLeave the profile browser.r=rrr!r!r"help_EOFszProfileBrowser.help_EOFcCsdS)Nrr!rr!r!r"do_quitszProfileBrowser.do_quitcCstd|jddSrrrr!r!r" help_quitszProfileBrowser.help_quitc Cs|rzt||_WnvtyL}z"t|jd|jdWYd}~dSd}~0ty}z&t|jjd||jdWYd}~dSd}~00|d|_ n6t |j dkr|j dd}| |ntd|jddS) Nrr=:rrtz1No statistics object is current -- cannot reload.r) rrGrrLr8r1rKrYr#rr4r)r7rerrr!r!r"rs  zProfileBrowser.do_readcCs td|jdtd|jddS)Nz+Read in profile data from a specified file.r=z*Without argument, reload the current file.rrr!r!r" help_readszProfileBrowser.help_readcCs$|jr|jntd|jddS)Nrr=r)rGrrLr1rr!r!r" do_reverses zProfileBrowser.do_reversecCstd|jddS)Nz/Reverse the sort order of the profiling report.r=rrr!r!r" help_reverseszProfileBrowser.help_reversecs|jstd|jddS|j|rRtfdd|DrR|jj|n.z/Valid sort keys (unique prefixes are accepted):z%s -- %srr) rGrLr1ryallrrrrur[)r7rrzrr!rr"do_sorts zProfileBrowser.do_sortcCs td|jdtd|jddS)Nz.Sort profile data according to specified keys.r=z3(Typing `sort' without arguments lists valid keys.)rrr!r!r" help_sortszProfileBrowser.help_sortcsfddtjDS)Ncsg|]}|r|qSr!)r)ratextr!r" rz0ProfileBrowser.complete_sort..)rru)r7rr8r!rr" complete_sortszProfileBrowser.complete_sortcCs |d|S)Nrrrr!r!r"do_statsszProfileBrowser.do_statscCstd|jd|dS)Nz.Print statistics from the current stat object.r=rrr!r!r" help_statsszProfileBrowser.help_statscCs$|jr|jntd|jddS)Nrr=)rGrrLr1rr!r!r"do_strips zProfileBrowser.do_stripcCstd|jddS)Nzs^     C