
    BeX!                         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
 ddlmZ ddlmZ dd	lmZ ddZd ZddZd ZddZdg fdZedd            ZddZdS )    N)contextmanager)Profilewraps   )config)escape   )stateFc                    ddl m} ddlm}  |||          }|r	| d         }n+| d         }| dd          D ]}|                    ||          }	 |                    |          }n# t          $ r d}Y nw xY wt          |          dfS )	Nr   )HTMLRenderer)Sessiontimelineshow_allr
   z5<h2><b>Rendering pyinstrument session failed</b></h2> )pyinstrument.renderersr   pyinstrument.sessionr   combinerender	Exceptionr	   )	sessionsr   r   r   r   rsessionsrendereds	            0lib/python3.11/site-packages/panel/io/profile.pyrender_pyinstrumentr      s    333333,,,,,,h:::A 22,1+!"" 	2 	2Aoogq11GGK88G$$ K K KJK(Rs   A' 'A65A6c                 j   ddl m} dd l}ddlm}m} ddlm} t          j	        
                    t          j	                            |j                  dd          }t          |          5 } ||                                          }	d d d            n# 1 swxY w Y    ||d                   }
|dd          D ]}|
                    |           |	                    |  ||
           ||
                                        d	                              d
d          }t'          |          dfS )Nr   )Stats)
json_stats
table_rows)Template	templateszviz.htmlr
   )profile_namer#   calleeszutf-8z/static/z/snakeviz/static/zbackground-color: white;)pstatsr!   snakevizsnakeviz.statsr"   r#   tornado.templater$   ospathjoindirname__file__openreadaddgeneratedecodereplacer	   )namer   r!   r)   r"   r#   r$   SNAKEVIZ_PATHfSNAKEVIZ_TEMPLATEr(   r   r   s                r   render_snakevizr;       s   OOO55555555))))))GLL1B!C!C[R\]]M	m		 /$HQVVXX../ / / / / / / / / / / / / / /U8A;FABB<  

7 ))jj&8&8**VBTBT *  fWooggj*=>>  (777s   +BBBTtreec                 v   ddl m} ddlm} ddlm} ddlm} ddlm	}	 ||||	d
                    |          }
|d         }t          j                    5 }|                    |           |                                  ||j                  }|r|                    ||nd	
          }n|                    ||nd	
          }d|j        j        i}|dv r$t)          |                                          |d<    |
j        |fi |}d d d            n# 1 swxY w Y   t/          j                    }|dk    r|                    ||j        ||           n9|dk    r|                    ||j        |           n|                    |           |                    d           |                                dfS )Nr   )
FileReader)FlameGraphReporter)StatsReporter)TableReporter)TreeReporter)
flamegraphstatstabler<   r   T)merge_threadsnative_tracesrC   rE   memory_recordsrC   rE   )filer   )memrayr>   memray.reporters.flamegraphr?   memray.reporters.statsr@   memray.reporters.tablerA   memray.reporters.treerB   gettempfileNamedTemporaryFilewriteflushr7   get_leaked_allocation_records%get_high_watermark_allocation_recordsmetadatahas_native_tracestupleget_memory_snapshotsfrom_snapshotioStringIOr   seekr2   )r7   r   show_memory_leaksrF   reporterr>   r?   r@   rA   rB   reporter_clsr   nfreadersnapshotkwargsreporter_instouts                     r   render_memrayrh   3   st   !!!!!!>>>>>>444444444444222222 )	 
 
c(mm  rlG		$	&	& 
"




BG$$ 	;;/</Hmmd <  HH CC/</Hmmd D  H "6?#DE...',V-H-H-J-J'K'KF#$22
 

 
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
* +--C<S&/3DmTTTT	W		S&/3DEEEE#&&&HHQKKK88::r>s   B5DDDc                 V   ddl m}m} g }|                                 D ]\  \  }}}|s|dk    ryt	          ||fi |\  }}	|                    dd          dvrAddlm}
  |
dd	d
          }|                    |           |	                    ||f           {t          |          }|dk    rt          |fi |\  }}	n|dk    rt          ||          \  }}	 |d| d|	 ddd	d          }|	                    ||f           |s |	                    d |d          f           |S )Nr   )HTMLMarkdownrK   r`   r<   rH   )Terminalstretch_bothr   iX  )sizing_modemargin
min_heightpyinstrumentr)   z<iframe srcdoc="z4" width="100%" height="100%" frameBorder="0" style="z"></iframe>i   r   zNo profiling output available)panerj   rk   itemsrh   rP   widgetsrl   rS   appendr	   r   r;   )	profilersre   rj   rk   profilesr-   enginer   srcstylerl   termhtmls                r   get_profilesr}   a   s   %%%%%%%%H$-OO$5$5 & & v 	X&tX@@@@JCzz*f--5LLL......xN1QTUUU

3t---Skk^##,X@@@@JCz!!(x88JCtjsjjX]jjj&	
 
 
 	t%%%% IXX&EFFGHHHO    c                 \      fdt           j                                        D             S )Nc                     i | ];\  \  }}}rt          j        |          rt          j        |          6||f|<S  )rematch).0r7   epsallowdenys       r   
<dictcomp>z get_sessions.<locals>.<dictcomp>   sw     ` ` `mitQ`XeT22`=A`IKRVX\I]I]`T1Ir ` ` `r~   )r   	_profilesrs   )r   r   s   ``r   get_sessionsr      sJ    ` ` ` ` `u/D/D/F/F ` ` ` `r~   c           
         ddl m}m}m}m} ddlm}m}  |t          t                              dddfd| j
                            d           t          j        d	k    rffd
}	 |dd          j
                            |	d            |dd          j
                            |	d            |d          }
nPt          j        dk    r4fd} |dg dd          j
                            |d           }
n |d          }
 | |d|
fg dddd          d          S )Nr   )	AccordionColumnRowTabs)CheckboxSelect)r      stretch_width)ro   rn   c                  H    t          t                    fi |d d <   d S )N)r}   r   )argsre   r   r   tabss     r   update_profilesz'profiling_tabs.<locals>.update_profiles   s8    %%
 
)/
 
QQQr~   r   rq   c                  6     j         j                    d S )Nr   value)r   r   r   r   s    r   update_pyinstrumentz+profiling_tabs.<locals>.update_pyinstrument   s"    OX^hnMMMMMMr~   zEnable timeline)r   r   )r7   ro   r   zShow All)rn   rK   c                  *     j                    d S )N)r`   r   )r   r`   r   s    r   update_memrayz%profiling_tabs.<locals>.update_memray   s    OX^444444r~   Reporter)rC   rE   r<   r<   )r7   optionsr   Configz#444444z#333333r   )activeactive_header_backgroundheader_backgroundrn   ro   )layoutr   r   r   r   rt   r   r   r}   r   paramwatchr   profiler)r   r   r   r   r   r   r   r   r   r   config_panelr   r`   r   r   r   r   s    ``         @@@@@r   profiling_tabsr      sH               +*******4	l5$//	0	0#  D

 
 
 
 
 
 

 
Ko{333.((	N 	N 	N 	N 	N 	N 	N8!26BBB0':::8F;;;0':::s'
 
 

 
H	$	$	5 	5 	5 	5 	5 	56z3R3R3RZ`aaa]G444s7776	|$%.''	
 	
 	
 	#   r~   rq   c              #   z  K   | dk    rVddl m} 	  |            }|                                 n# t          $ r#  |d          }|                                 Y nw xY w| dk    r#t	                      }|                                 ne| dk    r\ddl}t          j                     d	t          j
                    j         }|                    |          }|                                 n| 	 g }|V  | dk    r)|                    |                                           dS | dk    r+|                                 |                    |           dS | dk    rb|                    ddd           |                    t%          |d
                                                     t)          j        |           dS dS )ap  
    A context manager which profiles the body of the with statement
    with the supplied profiling engine and returns the profiling object
    in a list.

    Arguments
    ---------
    engine: str
      The profiling engine, e.g. 'pyinstrument', 'snakeviz' or 'memray'

    Returns
    -------
    sessions: list
      A list containing the profiling session.
    rq   r   )Profilerdisabled)
async_moder)   rK   Nz/tmprb)rq   r   startRuntimeErrorr   enablerK   rQ   
gettempdiruuiduuid4hexTracker	__enter__ru   stopdisable__exit__r1   r2   r,   remove)rx   r   profrK   tmp_filetrackerr   s          r   profile_ctxr      s     " ))))))	8::DJJLLLL 	 	 	8z222DJJLLLLL	 
:		yy	8		)++CC1ACC..**	H
NNN		$$$$$	:			8		tT***Xt,,1133444
	( 
	s   / *AAc                 ^     t           t                    st          d           fd}|S )a
  
    A decorator which may be added to any function to record profiling
    output.

    Arguments
    ---------
    name: str
      A unique name for the profiling session.
    engine: str
      The profiling engine, e.g. 'pyinstrument', 'snakeviz' or 'memray'
    zProfiler must be given a name.c                 B     t                      fd            }|S )Nc                  4   t           j        r t           j        t           j        v r | i |S t                    5 } | i |}d d d            n# 1 swxY w Y   t           j        fxx         |z  cc<   t           j                            d           |S )Nr   )r   curdoc
_launchingr   r   r   trigger)r   re   r   retrx   funcr7   s       r   wrappedz)profile.<locals>.wrapper.<locals>.wrapped   s    | -0@ @ @tT,V,,,V$$ ,dD+F++, , , , , , , , , , , , , , ,OT6N+++x7+++K,,,Js   	AAAr   )r   r   rx   r7   s   ` r   wrapperzprofile.<locals>.wrapper   s>    	t	 	 	 	 	 	 
	 r~   )
isinstancestr
ValueError)r7   rx   r   s   `` r   profiler      sL     dC   ;9:::
 
 
 
 
 
 Nr~   )FF)TTr<   )NN)rq   )r\   r,   r   rQ   r   
contextlibr   cProfiler   	functoolsr   r   utilr	   r   r   r;   rh   r}   r   r   r   r   r   r~   r   <module>r      sY   				 				 				   % % % % % %                                      8 8 8&, , , ,\  @` ` ` `
 !%2 0 0 0 0f , , , ,^     r~   