\drdZddlZddlZddlZddlZddlZddlmZmZddl m Z ddl m Z ddl mZgdZeeGdd Ze d Gd d Ze d GddZGddZGddZdZdZdZdZdZdZdZedkrddlZ ddlZn #e$rYnwxYwGddej Z!e"ej#dkrej#dZ$ndZ$ e!e$Z%ej#ddD]Z&e%'e&e(d e%j)!e%*e(d"e%j)!dS#e+$rYdSwxYwdS)#z3Class for printing reports on profiled python code.N)StrEnum _simple_enum) cmp_to_key) dataclass)Dict)StatsSortKeyFunctionProfile StatsProfilec8eZdZdZdZdZdZdZdZdZ dZ d Z d Z d S) r )callsncalls) cumulativecumtime)filenamemodulelinenamenflpcallsstdname)timetottimec|d}t||}||_|ddD] }||j|< ||_|SNr)str__new___value__value2member_map_ _all_values)clsvaluesvalueobj other_values ..\python\lib\pstats.pyrzSortKey.__new__0sXq kk#u%% !!"": 6 6K25C "; / /  N) __name__ __module__ __qualname__CALLS CUMULATIVEFILENAMELINENAMENFLPCALLSSTDNAMETIMErr(r'r r $sP E(J#H D D C FG Dr(r T) unsafe_hashcVeZdZUeed<eed<eed<eed<eed<eed<eed<dS) r rrpercall_tottimerpercall_cumtime file_name line_numberN)r)r*r+r__annotations__floatintr5r(r'r r :sZ KKK NNN NNNNNNr(r c8eZdZUdZeed<eeefed<dS)r z0Class for keeping track of an item in inventory.total_tt func_profilesN) r)r*r+__doc__r=r<rrr r5r(r'r r Ds7::OOO_,------r(r ceZdZdZdddZdZdZdZdZd Z d d d d d d d ddddddd Z dZ dZ dZ dZdZdZdZdZdZdZdZdZd$d!Zd"Zd#ZdS)%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)streamc|p tj|_t|sd}n|d}|dd}|||j|dSr)sysstdoutrDleninitadd)selfrDargsargs r'__init__zStats.__init__ls[*  4yy CCq'C8D #$r(cvd|_g|_d|_d|_d|_d|_d|_t|_i|_ i|_ | | | dS#t$r/td|jr |jdndz|jwxYw)NrzInvalid timing data %sfile) all_calleesfilesfcn_listr@ total_calls prim_calls max_name_lenset top_levelstats sort_arg_dict load_statsget_top_level_stats ExceptionprintrD)rKrMs r'rIz Stats.initvs        $ $ & & & & &    *%)Z74:b>>R9?C{ L L L L  s )A??9B8c| i|_dSt|trt|d5}t j||_dddn #1swxYwY t j|}tj |j dz|z}n#YnxYw|g|_ n7t|dr'| |j|_i|_|jstd|jd|dS)Nrbz create_statszCannot create or construct a z object from )r\ isinstanceropenmarshalloadosstatrctimest_mtimerUhasattrrd TypeError __class__)rKrMf file_statss r'r^zStats.load_statssF ;DJ F S ! ! c4 -A$\!__  - - - - - - - - - - - - - - - WS\\ j!455>D DJJ S. ) )       DJCIz 5)#~~~ss455 5sAAA#3BBcx|jD]\}\}}}}}|xj|z c_|xj|z c_|xj|z c_d|vr|j|tt||j kr!tt||_ dS)N)jprofilerprofiler) r\itemsrWrXr@r[rJrHfunc_std_stringrY)rKfuncccncttctcallerss r'r_zStats.get_top_level_statss/3z/?/?/A/A ? ? +D+2r2r7    "   OO "OO MM "MM*g55""4(((?4(())D,===$'(=(=$>$>! ? ?r(ct|s|St|D]"}t|t|krt|}|xj|jz c_|xj|jz c_|xj|jz c_|xj|jz c_|jD]}|j||j |j kr |j |_ d|_ |j D];\}}||j vr|j |}nddddif}t|||j |<<$|SNr)reversedtyperrUrWrXr@r[rJrYrVr\ruadd_func_stats)rKarg_listitemrwrj old_func_stats r'rJz Stats.addsX KX&& G GDDzzT$ZZ''T{{ JJ$* $JJ    0 0   OOt .OO MMT] *MM ) )""4(((( 4#444$($5! DM"j..00 G G d4:%%$(Jt$4MM%&1a$5M#1-#F#F 4   G  r(ct|d5}tj|j|ddddS#1swxYwYdS)z:Write the profile data to a file we know how to load back.wbN)rfrgdumpr\)rKrrps r' dump_statszStats.dump_statss (D ! ! (Q LQ ' ' ' ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s 9==)))rrPz call count)))rPzcumulative time))rz file name))rz line number))rz function name))rrrzname/file/line)))rrPzprimitive call count)))rz standard name)))rPz internal time) r rrrrrrrrrrrrc|jsTix|_}i}|jD]'\}}|}|r|sn||vrd||<n|||<|dd}|(|D]}||=|jS)z)Expand all abbreviations that are unique.rNrP)r]sort_arg_dict_defaultru)rKdictbad_listwordtupfragments r'get_sort_arg_defszStats.get_sort_arg_defss! (* *D H!7==?? - - c-#4''-.*%(DN'}H-!  JJ!!r(c|s d|_|St|dkr0t|dtrddddd|dg}nUt|dkrB|ddD]7}t |t |dkrt d 8|}d }d |_d }|D]P}t|tr|j }|||dz}|xj|||dzz c_d }Qg}|j D]9\}\} } } } } | | | | | f|zt||fz:|tt!|j gx|_}|D]}| |d|S)Nrrrr rr)rPrrrrzCan't have mixed argument typer5rQz, )keyrP)rVrHrer>rrnr sort_typer r$r\ruappendrvsortr TupleCompcompare)rKfieldrM sort_arg_defs sort_tuple connectorr stats_listrwrxryrzr{r|rVtuples r' sort_statszStats.sort_statss DMK u::??z%(C88?$"!'))*/q35EEZZ1__QRRy F F99U1X..#$DEEE/..00     D$(( "z#mD&9!&< gg?g?rz List reduced from z to z due to restriction ) rerrecompileerrorsearchrvrrHr=r>)rKsellistmsgnew_listrexrwcounts r'eval_print_amountzStats.eval_print_amountIs c3   ( %joo8 % % %=CC}$$$ %H * *::od3344*OOD))) *IIE#u%% (#*:*:*:*:s*:*:*:*:*:ECK",--<C%% (!s*:*:*:*:U*:*:*:*:*:< t99H % % CD 3x====###/ /C}s.A  A c |jr|jddn%t|j}|st diSt t |j}i}t ||}|D]}|j|\}}}} } |\} } } ||krt|n!t|dzt|z}t t |}|dkrdnt t ||z }t t | }|dkrdnt t | |z }t|||||| | }||| <|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/rP) rVrr\keysr r=f8r@rr )rK func_listr@rA stats_profilerwrxryrzr{r|r:r; func_namerrr8rr9 func_profiles r'get_stats_profilezStats.get_stats_profilecsv )- RDM!!!$$4 @Q@Q;R;R  '2&& &DM**++ $X}==  4 4D&*j&6 #BBG04 -I{I "bSWWWs2ww}s2ww/FFBrFFmmG$&!GGbbr"R%yy1A1AOBrFFmmG$&!GGbbr"R%yy1A1AO*L(4M) $ $r(c|j}|jr|jdd}d|jzdz}n(t|j}d}|D]}||||\}}t|}|sd|fSt||j |t|jkrCd}|D]>}tt||krtt|}?|dz|fS)Nz Ordered by:  z! Random listing order was used rrRr) rYrVrrr\rrrHrarDrv)rKsel_listwidth stat_listr selectionrrws r'get_print_listzStats.get_print_lists! = 7 aaa(I#dn4t;CCTZ__..//I6C! O OI!33Iy#NNNIssI i<  c $$$$ 3tz?? " "E! 7 7--..66 5 566EQw !!r(c|jD]}t||j|jrt|jd}|jD]&}t|t ||j't||jdd|j|j|jkrtd|jzd|jtd|jz|jt|j||\}}|rX| |D]}| |t|jt|j|S)NrR zfunction calls endrSz(%d primitive calls)zin %.3f seconds) rUrarDr[func_get_function_namerWrXr@r print_title print_line)rKamountrindentrwrrs r' print_statszStats.print_statss  . .H ( - - - - - : $ t{ # # # #N J JD &066T[ I I I I I fd&(8c TTTT  t . . (4?:$+ V V V V $-/dkBBBB 4;))&11 t  $       & &%%%% t{ # # # # t{ # # # # r(cr||\}}|r|||d|D]E}||jvr#||||j|.|||iFt |jt |j|S)Nz called...rR)rrprint_call_headingrTprint_call_linerarD)rKrrrrws r' print_calleeszStats.print_calleess))&11 t  $        # #E; 7 7 7 : :4+++((d6Ft6LMMMM((b9999 t{ # # # # t{ # # # # r(c||\}}|rp||d|D]-}|j|\}}}}} |||| d.t |jt |j|S)Nzwas called by...z<-rR)rrr\rrarD) rKrrrrwrxryrzr{r|s r' print_callerszStats.print_callerss))&11 t  $  # #E+= > > > A A*.*T*:'BB$$UD'4@@@@ t{ # # # # t{ # # # # r(cttd||z|jd}|jD]O\}}}}}|rEt t |} t| t}nP|rtd|zdz|jdSdS)Nz Function rRFrz ncalls tottime cumtime) raljustrDr\r#nextiterrer) rK name_size column_title subheaderrxryrzr{r|r$s r'rzStats.print_call_headings k **\9 LLLL '+z'8'8':':   #BBG T'.."2"23344&ue44    T #i-"@@t{ S S S S S S T Tr(->c tt|||zd|j|st|jdSt |}d}|D]}t|}||} t | trn| \} } } } | | krd| | fz}nd| fz}|ddt|zzdt| dt| d |}|d z}n-|d | d t|j |d }|d z}t||z|z|jd}dS)NrrrRrQz%d/%dz%drrz r(z) r) rarvrrDsortedrrerrjustrHrr\)rKrsource call_dictarrowclistrrwrr$ryrxrzr{substats left_widths r'rzStats.print_call_lines of%%++I66>CdkZZZZ  t{ # # # # Fy~~''((  D"4((DdOE%'' +!&BB88&"b1HH#re|H-5^^AaF mO-L-L-L-L-/VVVVRVVVVTTC&] *.$$r$*T:J1:M7N7N7NO&] &#h.T[ A A A AFF!  r(c`tdd|jtd|jdS)Nz- ncalls tottime percall cumtime percallrrzfilename:lineno(function)rRrarDrs r'rzStats.print_titles6 =3T[YYYY ) <<<<<z{%s}rrPz %s:%d(%s)) startswithendswith)rrs r'rvrv*sh!}  | ??3   DMM#$6$6 D2J& &KY&&r(cd|\}}}}}|\}}} } } ||z||z|| z|| zt| |fS)z3Add together all the stats for two profile entries.) add_callers) targetrrxryrzr{r|t_cct_nct_ttt_ct t_callerss r'rr;sR$BBG(.%D$dI tGRWbgr$w)W-- //r(c <i}|D] \}}|||< |D]e\}}||vrWt|tr1tdt|||D||<O||xx|z cc<`|||<f|S)z*Combine two caller lists in a single list.c3&K|] \}}||zV dSrr5).0ijs r' zadd_callers..Ks*)[)[DAq!a%)[)[)[)[)[)[r()rurerzip)rr new_callersrwrs r'rrBsK ## f" D  ' ' f ;  &%(( ,$))[)[C TXHY ValueErrorr=rarDr\getattr)rKfnrrL processedtermfracs r'genericzProfileBrowser.genericus/::<Add profile info from given file to current statistics object.rRrrs r'help_addzProfileBrowser.help_adds RY]Yd e e e e e er(c.|d|S)Nrr:rKrs r' do_calleeszProfileBrowser.do_callees<<66 6r(cZtd|j|dS)Nz6Print callees statistics from the current stat object.rRrarDr<rs r' help_calleeszProfileBrowser.help_callees2 JQUQ\ ] ] ] ]        r(c.|d|S)NrrDrEs r' do_callerszProfileBrowser.do_callersrGr(cZtd|j|dS)Nz6Print callers statistics from the current stat object.rRrIrs r' help_callerszProfileBrowser.help_callersrKr(c2td|jdS)NrQrRrrrEs r'do_EOFzProfileBrowser.do_EOFs "4; ' ' ' '1r(c2td|jdSNzLeave the profile browser.rRrrs r'help_EOFzProfileBrowser.help_EOF .T[ A A A A A Ar(cdS)Nrr5rEs r'do_quitzProfileBrowser.do_quits1r(c2td|jdSrSrrs r' help_quitzProfileBrowser.help_quitrUr(c|r t||_np#t$r,}t|jd|jYd}~dSd}~wt $r/}t|jjdz||jYd}~dSd}~wwxYw|dz|_ nSt|j dkr%|j dd}| |ntd|jdS) NrrR:r+rz1No statistics object is current -- cannot reload.r) rr\r>rarLrDr`ror)r.rHr/)rKrerrs r'r/zProfileBrowser.do_reads ]!&tDJJ#(1+DK8888FFFFF #-036$+NNNNFFFFF#Tk T[!!A%%{3B3' T""""IPTP[\\\\1s  B!A  B$BBc^td|jtd|jdS)Nz+Read in profile data from a specified file.rRz*Without argument, reload the current file.rrs r' help_readzProfileBrowser.help_reads4 ?dk R R R R >T[ Q Q Q Q Q Qr(ct|jr|jntd|jdS)Nr2rRr)r\rrarDrEs r' do_reversezProfileBrowser.do_reverses?z K ((****7dkJJJJ1r(c2td|jdS)Nz/Reverse the sort order of the profiling report.rRrrs r' help_reversezProfileBrowser.help_reverses C$+ V V V V V Vr(c|jstd|jdS|j|rOt fd|Dr"|jj|n[td|jtj D]&\}}t|d|d|j'dS)Nr2rRc3 K|]}|vV dSrr5)rr&abbrevss r'r z)ProfileBrowser.do_sort..s'AAqQ'\AAAAAAr(z/Valid sort keys (unique prefixes are accepted):z -- rr) r\rarDrallr3rrrru)rKrrr$rfs @r'do_sortzProfileBrowser.do_sorts: 7dkJJJJj2244G JAAAADJJLLAAAAA J% %tzz||444GdkZZZZ$)$?$E$E$G$GJJLS%U1XX6T[IIIII1r(c^td|jtd|jdS)Nz.Sort profile data according to specified keys.rRz3(Typing `sort' without arguments lists valid keys.)rrs r' help_sortzProfileBrowser.help_sorts4 B U U U U Gdk Z Z Z Z Z Zr(c4fdtjDS)Nc>g|]}||Sr5)r)ratexts r' z0ProfileBrowser.complete_sort..s*QQQ!all4>P>PQAQQQr()rr)rKrnrLs ` r' complete_sortzProfileBrowser.complete_sorts QQQQu:QQQ Qr(c.|d|S)NrrDrEs r'do_statszProfileBrowser.do_statss<< t44 4r(cZtd|j|dS)Nz.Print statistics from the current stat object.rRrIrs r' help_statszProfileBrowser.help_statss0 B U U U U        r(cv|jr|jdStd|jdS)Nr2rR)r\rrarDrEs r'do_stripzProfileBrowser.do_stripsCz K %%'''''7dkJJJJJJr(c2td|jdS)Nzrsl99,  &&&&&&&& !!!!!! A A A g* t t........ ~7~7~7~7~7~7~7~7@2222 ' ' '"///" zJJJ       PPPPPPPPd s38}}qhqk    .--x| $ $G NN7 # # # # :PPPP jw~......      Is% CCCA*E??FF