
    Yf!                       d dl m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 d dlmZ d dlmZ d dlmZmZ d d	lmZ  ed
d
       G d d             Z G d de      Ze G d d             Ze G d d             Ze G d d             Ze
eeeedf   f   Ze
eeeeef   f   Z G d dej>                        Z  G d de      Z!y)    )annotationsN)	dataclass)Enum)AnyDictUnion)
processors)Frame)FrameRendererProcessorList)SessionT)frozeneqc                  0    e Zd ZU dZded<   ded<   ded<   y)SpeedscopeFramea3  
    Data class to store data needed for speedscope's concept of a
    frame, hereafter referred to as a "speedscope frame", as opposed to
    a "pyinstrument frame". This type must be hashable in order to use
    it as a dictionary key; a dictionary will be used to track unique
    speedscope frames.
    z
str | Nonenamefilez
int | NonelineN__name__
__module____qualname____doc____annotations__     a/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/pyinstrument/renderers/speedscope.pyr   r      s     

r   r   c                      e Zd ZdZdZdZy)SpeedscopeEventTypez?Enum representing the only two types of speedscope frame eventsOCN)r   r   r   r   OPENCLOSEr   r   r   r   r       s    IDEr   r   c                  0    e Zd ZU dZded<   ded<   ded<   y)	SpeedscopeEventz
    Data class to store speedscope's concept of an "event", which
    corresponds to opening or closing stack frames as functions or
    methods are entered or exited.
    r   typefloatatintframeNr   r   r   r   r%   r%   '   s     IJr   r%   c                  Z    e Zd ZU dZded<   ded<   ded<   dZded	<   d
Zded<   dZded<   y)SpeedscopeProfilezB
    Data class to store speedscope's concept of a "profile".
    strr   list[SpeedscopeEvent]eventsr'   	end_value        start_valueeventedr&   secondsunitN)r   r   r   r   r   r2   r&   r5   r   r   r   r,   r,   4   s8     I!!KD#D#r   r,   c                  Z    e Zd ZU dZded<   ded<   ded<   dZded	<   d
Zded<   dZded<   y
)SpeedscopeFilezE
    Data class encoding fields in speedscope's JSON file schema
    r-   r   zlist[SpeedscopeProfile]profilesz dict[str, list[SpeedscopeFrame]]sharedz2https://www.speedscope.app/file-format-schema.jsonschemaNNoneactive_profile_indexpyinstrumentexporter)r   r   r   r   r   r:   r<   r>   r   r   r   r7   r7   B   s9     I%%,,FFCF!%$%"Hc"r   r7   c                      e Zd ZdZddZy)SpeedscopeEncoderz`
    Encoder class used by json.dumps to serialize the various
    speedscope data classes.
    c                   t        |t              rE|j                  |j                  |j                  |j
                  |j                  |j                  dS t        |t              rE|j                  |j                  |j                  |j                  |j                  |j                  dS t        |t        t        f      r|j                   }|S t        |t"              r|j$                  S t&        j(                  j+                  | |      S )N)z$schemar   activeProfileIndexr>   r8   r9   )r&   r   r5   
startValueendValuer/   )
isinstancer7   r:   r   r<   r>   r8   r9   r,   r&   r5   r2   r0   r/   r   r%   __dict__r   valuejsonJSONEncoderdefault)selfods      r   rJ   zSpeedscopeEncoder.defaultZ   s    a(88&'&<&<JJJJ((  a*+mmKK((  a/?;<CD::AHa,-77N''a00r   N)rL   r   returnr   )r   r   r   r   rJ   r   r   r   r@   r@   T   s    
1r   r@   c                  @     e Zd ZdZdZd fdZddZd	dZd
dZ xZ	S )SpeedscopeRenderera  
    Outputs a tree of JSON conforming to the speedscope schema documented at

    wiki: https://github.com/jlfwong/speedscope/wiki/Importing-from-custom-sources
    schema: https://www.speedscope.app/file-format-schema.json
    spec: https://github.com/jlfwong/speedscope/blob/main/src/lib/file-format-spec.ts
    example: https://github.com/jlfwong/speedscope/blob/main/sample/profiles/speedscope/0.0.1/simple.speedscope.json

    zspeedscope.jsonc                @    t        |   di | d| _        i | _        y )Nr1   r   )super__init___event_time_frame_to_index)rK   kwargs	__class__s     r   rS   zSpeedscopeRenderer.__init__   s'    "6" #& <>r   c                   |g S t        |j                  |j                  |j                        }|| j                  vr"t        | j                        | j                  |<   | j                  |   }t        t        j                  | j                  |      }|g}|j                  D ]"  }|j                  | j                  |             $ | xj                  |j                  z  c_	        |j                  r| xj                  |j                  z  c_	        t        t        j                   | j                  |      }|j#                  |       |S )a  
        Builds up a list of speedscope events that are used to populate the
        "events" array in speedscope-formatted JSON.

        This method has two notable side effects:

        * it populates the self._frame_to_index dictionary that matches
          speedscope frames with their positions in the "shared" array of
          speedscope output; this dictionary will be used to write this
          "shared" array in the render method

        * it accumulates a running total of time elapsed by
          accumulating the self_time spent in each pyinstrument frame;
          this running total is used by speedscope events to construct
          a flame chart.
        )r   function	file_pathline_norU   lenr%   r   r"   rT   childrenextendrender_frameabsorbed_timeis_synthetic_leaftimer#   append)rK   r*   sframesframe_index
open_eventevents_arraychildclose_events           r   r_   zSpeedscopeRenderer.render_frame   s   ( =I !%--P---+.t/C/C+DD  ( ++F3$%8%=%=t?O?OQ]^
/9l ^^ 	:E 1 1% 89	:" 	E///""

* &&9&?&?AQAQS_`K(r   c                   | j                  |j                               }t        j                  dt        j                  |j
                              }d|j                   d| }t        || j                  |      |j                        g}t        | j                        D cg c]  }| }}d|i}t        |||      }	dt        j                  |	t              z  S c c}w )Nz%Y-%m-%dT%H-%M-%SzCPU profile for z at framesz%s
)cls)
preprocess
root_framerb   strftime	localtime
start_timeprogramr,   r_   durationiterrU   r7   rH   dumpsr@   )
rK   sessionr*   id_r   sprofile_listrd   sframe_listshared_dictspeedscope_files
             r   renderzSpeedscopeRenderer.render   s     2 2 45==!4dnnWEWEW6XY&w&7tC5A dD$5$5e$<g>N>NO2
 DHH\H\C]-^f-^-^-(}kJ

?8IJJJ ._s   #	Cc                    t         j                  t         j                  t         j                  t         j                  t         j
                  t         j                  gS )z
        Default Processors for speedscope renderer; note that
        processors.aggregate_repeated_calls is removed because
        speedscope is a timeline-based format.
        )r	   remove_importlibremove_tracebackhidemerge_consecutive_self_time"remove_unnecessary_self_time_nodesremove_irrelevant_nodes*remove_first_pyinstrument_frames_processor)rK   s    r   default_processorsz%SpeedscopeRenderer.default_processors   sD     ''++2299..AA
 	
r   )rV   r   )r*   zFrame | NonerN   r.   )rv   r   )rN   r   )
r   r   r   r   output_file_extensionrS   r_   r|   r   __classcell__)rW   s   @r   rP   rP   u   s&     .>?BK(
r   rP   )"
__future__r   rH   rb   dataclassesr   enumr   typingr   r   r   r=   r	   pyinstrument.framer
   pyinstrument.renderers.baser   r   pyinstrument.sessionr   r   r   r%   r,   r7   r-   r)   SpeedscopeFrameDictTyper'   SpeedscopeEventDictTyperI   r@   rP   r   r   r   <module>r      s    "   !  # # # $ D (
 $4   !$  	 	 	 
 
 
 
# 
# 
# sE#sD.$99: sE*=uc*I$JJK 1(( 1B}
 }
r   