a
    ¦ˆcE  ã                   @   sä   d Z ddlZddlZdZdZe d¡Ze d¡Ze d¡Ze d¡Z	d	d
„ Z
dZee d¡ Ze d¡Ze d¡Ze d¡Ze d¡Zdd„ ZdZdZe d¡Zdd„ ZdZdZe d¡Zdd„ Zdd„ Zdd „ Zed!kràeƒ  dS )"a  
Perform timing tests to compare the performance of on_trait_change and observe.

For details on the varying scenarios see the description strings throughout
the file.

For the original motivation of this script, see the related issue:
enthought/traits#1325
é    Ni'  a)  
    Given a subclass of HasTraits with a trait defined, e.g. name = Str(),
    compare having a method with no decorator to one that is decorated with
    @on_trait_change("name"), one decorated with @observe("name"), and one
    with @observe(trait('name')).

    The timing we are interested in:
    (1) Time to import the module, i.e. the class construction.
    (2) Time to instantiate the HasTraits object.
    (3) Time for reassigning the trait to a new value.

    Times are reported as:
    (#): absolute_time  relative_time_to_control
        z×
    from traits.api import (
        cached_property,
        HasTraits,
        Instance,
        observe,
        on_trait_change,
        Property,
        Str
    )
    from traits.observation.api import trait
z€

    class Person(HasTraits):
        name = Str()
        {decorator}
        def some_method(self, event):
            pass

z
    a_person = Person()
z
    a_person.name = 'name'
c                 C   sR   t j| d�}tj|ttd�}tjtt| td�}tjtt| t td�}|||fS )aÔ   Time the cases described by the decorator with simple trait case, using
    the given method decorator.

    Parameters
    ----------
    decorator - Str
        The string defining the decorator to be used on the method of the
        HasTraits subclass.  e.g. "@observe('name')"

    Returns
    -------
    tuple
        A 3-tuple containing the time to construct the HasTraits subclass, the
        time to instantiate it, and the time to reassign the trait
    ©Ú	decorator©ÚstmtÚsetupÚnumber)ÚPERSON_CONSTRUCTION_TEMPLATEÚformatÚtimeitÚ
BASE_SETUPÚNÚINSTANTIATE_PERSONÚREASSIGN_NAME)r   Zconstruct_personÚconstruction_timeÚinstantiation_timeZreassign_person_name_time© r   úd/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/tests/check_observe_timing.pyÚ"get_decorated_method_simple_timingQ   s"    ýý
ýr   aÔ  
    Given a subclass of HasTraits with a trait referring to an instance of
    HasTraits, e.g. child = Instance(AnotherClass) where AnotherClass has a
    name trait, compare having a method with no decorator to one that is
    decorated with @on_trait_change("child.name"), one decorated with
    @observe("child.name"), and one with
    @observe(trait('child').trait('name')).

    The timing we are interested in:
    (1) Time to import the module, i.e. the class construction.
    (2) Time to instantiate the HasTraits object.
    (3) Time for reassigning child to a new value.
    (4) Time for reassigning child.name to a new value.

    Times are reported as:
    (#): absolute_time  relative_time_to_control
        z5

    class Person(HasTraits):
        name = Str()

zŒ

    class Parent(HasTraits):
        child = Instance(Person)
        {decorator}
        def some_method(self, event):
            pass

z'
    a_parent = Parent(child=Person())
z
    a_parent.child = Person()
z"
    a_parent.child.name = 'name'
c                 C   sl   t j| d�}tj|ttd�}tjtt| td�}tjtt| t td�}tjtt| t td�}||||fS )a	   Time the cases described by the decorated method with enxtended trait
    case, using the given method decorator.

    Parameters
    ----------
    decorator - Str
        The string defining the decorator to be used on the method of the
        HasTraits subclass.  e.g. "@observe('child.name')"

    Returns
    -------
    tuple
        A 4-tuple containing the time to construct the HasTraits subclass, the
        time to instantiate it, the time to reassign child, and the
        time to reassign child.name
    r   r   )ÚPARENT_CONSTRUCTION_TEMPLATEr	   r
   ÚCONSTRUCT_PARENT_SETUPr   ÚINSTANTIATE_PARENTÚREASSIGN_CHILDÚREASSIGN_CHILD_NAME)r   Zconstruct_parentr   r   Úreassign_child_timeÚreassign_child_name_timer   r   r   Ú$get_decorated_method_extended_timing±   s8    ÿýý
ý
ýür   a9  
    Given a subclass of HasTraits with a trait that is defined as Property
    that depends on a simple trait, compare having the Property be defined
    as Property(), Property(depends_on="name"), Property(observe="name"),
    and Property(observe=trait('name')).

    The timing we are interested in:
    (1) Time to import the module, i.e. the class construction.
    (2) Time to instantiate the HasTraits object.
    (3) Time for changing the trait being depended-on / observed.

    Times are reported as:
    (#): absolute_time  relative_time_to_control
        aª  
    Identical to the simple property case only using the @chached_property
    decorator on the property's getter method.

    The timing we are interested in:
    (1) Time to import the module, i.e. the class construction.
    (2) Time to instantiate the HasTraits object.
    (3) Time for changing the trait being depended-on / observed.

    Times are reported as:
    (#): absolute_time  relative_time_to_control
        z¡

    class Person(HasTraits):
        name=Str()
        a_property = Property({})

        {}
        def _get_a_property(self):
            return self.name

c                 C   sR   t  | |¡}tj|ttd�}tjtt| td�}tjtt| t td�}|||fS )aC   Time the cases described by the (cached) property depending on a simple
    trait scenario.  Whether or not the property is cached is based on the
    cached_property argument, and the given property_args argument is used in
    the Property trait definition.

    Parameters
    ----------
    property_args - Str
        The string defining the argument to be passed in the definition of the
        Property trait.  e.g. "depends_on='name'"
    cached_property - Str
        The string that will be used to decorate the getter method of the
        Property.  Expected to be either '' or '@cached_property'.

    Returns
    -------
    tuple
        A 3-tuple containing the time to construct the HasTraits subclass, the
        time to instantiate it, and the time to reassign the trait being
        depended-on / observed.
    r   )Ú*PERSON_WITH_PROPERTY_CONSTRUCTION_TEMPLATEr	   r
   r   r   r   r   )Úproperty_argsÚcached_propertyZconstruct_person_with_propertyr   r   Zreassign_dependee_name_timer   r   r   Úget_property_simple_timing  s*    þÿýý
ýr   a„  
    Given a subclass of HasTraits with a trait that is defined as Property
    that depends on an extended trait, compare having the Property be
    defined as Property(), Property(depends_on="child.name"),
    Property(observe="child.name"), and
    Property(observe=trait('child').trait('name')).

    The timing we are interested in:
    (1) Time to import the module, i.e. the class construction.
    (2) Time to instantiate the HasTraits object.
    (3) Time for reassigning child to a new value.
    (4) Time for reassigning child.name to a new value.

    Times are reported as:
    (#): absolute_time  relative_time_to_control
        aÔ  
    Identical to the extended property case only using the @cached_property
    decorator on the property's getter method.

    The timing we are interested in:
    (1) Time to import the module, i.e. the class construction.
    (2) Time to instantiate the HasTraits object.
    (3) Time for reassigning child to a new value.
    (4) Time for reassigning child.name to a new value.

    Times are reported as:
    (#): absolute_time  relative_time_to_control
        zµ

    class Parent(HasTraits):
        child = Instance(Person)
        a_property = Property({})

        {}
        def _get_a_property(self):
            return self.child.name

c                 C   sl   t  | |¡}tj|ttd�}tjtt| td�}tjtt| t td�}tjtt| t td�}||||fS )aO   Time the cases described by the (cached) property depending on an
    extended trait scenario. Wether or not the property is cached is determined
    by the cached_property argument, and the given property_args argument is
    used for the Property trait definition.

    Parameters
    ----------
    property_args - Str
        The string defining the argument to be passed in the definition of the
        Property trait.  e.g. "depends_on='child.name'"
    cached_property - Str
        The string that will be used to decorate the getter method of the
        Property.  Expected to be either '' or '@cached_property'.

    Returns
    -------
    tuple
        A 4-tuple containing the time to construct the HasTraits subclass, the
        time to instantiate it, the time to reassign child, and the time to
        reassign child.name
    r   )Ú*PARENT_WITH_PROPERTY_CONSTRUCTION_TEMPLATEr	   r
   r   r   r   r   r   )r   r   Zconstruct_parent_with_propertyr   r   r   r   r   r   r   Úget_property_extended_timingt  sL    þÿýýÿþù
ÿþùür!   c           	      C   sŒ   t | ƒ ||d Ž }t|ƒD ]b\}}|dkr:t dƒ |}nt |d d ƒ ||Ž }tdd„ t||ƒƒ}t |jg |¢|¢R Ž ƒ qt dƒ dS )a3   Prints a readable benchmark report.

    Parameters
    ----------
    descritption : str
        The description of the benchmarking scenario being reported.
    benchmark_template : str
        The format string used to print the times for the benchmark in a clean,
        formatted way
    get_time : function
        The function used to get the benchmark times for the current benchmark
        scenario
    get_time_args : list of tuples
        The list of tuples containing the arguments to be passed the the
        get_time function.  Note the first argument should give specifics about
        the case being timed and will be printed as part of the report.  e.g.
        ("@observe('name')"), or ("depends_on='name'", '@cached_property').
        The first item in the list will be used as a control.
    r   z	control -z -c                 S   s   | d | d  S )Né   r   r   )Útimesr   r   r   Ú<lambda>Ù  ó    zreport.<locals>.<lambda>zP--------------------------------------------------------------------------------N)ÚprintÚ	enumerateÚmapÚzipr	   )	ÚdescriptionÚbenchmark_templateÚget_timeÚget_time_argsZcontrol_timesÚindexÚargsZ
case_timesZrelative_timesr   r   r   Úreport·  s    ÿr0   c                  C   s„   d} d}t t| tg d¢d� t t|tg d¢d� t t| tg d¢d� t t| tg d¢d� t t|t	g d¢d� t t
|t	g d	¢d� d S )
NzD(1): {0:.5f} {3:>6.2f} (2): {1:.5f} {4:>6.2f} (3): {2:.5f} {5:>6.2f}z[(1): {0:.5f} {4:>6.2f} (2): {1:.5f} {5:>6.2f} (3): {2:.5f} {6:>6.2f} (4): {3:.5f} {7:>6.2f})©Ú )z@on_trait_change('name'))z@observe('name'))z@observe(trait('name')))r*   r+   r,   r-   )r1   )z@on_trait_change('child.name'))z@observe('child.name'))z&@observe(trait('child').trait('name')))©r2   r2   )údepends_on='name'r2   )úobserve='name'r2   )úobserve=trait('name')r2   )©r2   ú@cached_property)r4   r8   )r5   r8   )r6   r8   )r3   )údepends_on='child.name'r2   )úobserve='child.name'r2   )ú$observe=trait('child').trait('name')r2   )r7   )r9   r8   )r:   r8   )r;   r8   )r0   Ú!DECORATOR_SIMPLE_CASE_DESCRIPTIONr   Ú#DECORATOR_EXTENDED_CASE_DESCRIPTIONr   Ú PROPERTY_SIMPLE_CASE_DESCRIPTIONr   Ú'CACHED_PROPERTY_SIMPLE_CASE_DESCRIPTIONÚ"PROPERTY_EXTENDED_CASE_DESCRIPTIONr!   Ú)CACHED_PROPERTY_EXTENDED_CASE_DESCRIPTION)ZBENCHMARK_TEMPLATE_3TIMESZBENCHMARK_TEMPLATE_4TIMESr   r   r   Úmainá  sL    üüüüüürB   Ú__main__)Ú__doc__Útextwrapr
   r   r<   Údedentr   r   r   r   r   r=   r   r   r   r   r   r   r>   r?   r   r   r@   rA   r    r!   r0   rB   Ú__name__r   r   r   r   Ú<module>   s8   




&	



4
0
C*S