ó
H`¾Tc           @   s  d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z d d l m	 Z	 d d l
 Z
 d d l Z d d l Z d d l m Z y d d l Z Wn e k
 r± d Z n Xd d	 l m Z m Z m Z m Z d Z d d
 „ Z d e f d „  ƒ  YZ d d „ Z d „  Z d S(   s§   Profiling support for unit and performance tests.

These are special purpose profiling methods which operate
in a more fine-grained way than nose's profiling plugin.

iÿÿÿÿNi   (   t
   gc_collectt	   decorator(   t   config(   t   SkipTesti   (   t   util(   t   jythont   pypyt   win32t   update_wrapperc            sn   i t  ƒ  d 6t d 6t d 6t d 6t d 6d d 6d d	 6‰ ˆ  d k rO d
 ‰  n  t ‡ ‡ ‡  f d †  ƒ } | S(   s   Function profiling.

    @profiled()
    or
    @profiled(report=True, sort=('calls',), limit=20)

    Outputs profiling info for a decorated function.

    t   targetst   reportt   print_callerst   print_calleest   graphict   timet   callst   sortt   limitt   anonymous_targetc            s.  t  |  | | Ž \ } } } ˆ  j d ˆ d ƒ } | rK t j d t ƒ nß ˆ  j d ˆ d ƒ } | r*ˆ  j d ˆ d ƒ } ˆ  j d ˆ d ƒ }	 d ˆ f GH| ƒ  }
 |
 j | Œ  |	 rË |
 j |	 ƒ n
 |
 j ƒ  ˆ  j d ˆ d ƒ } | rþ |
 j ƒ  n  ˆ  j d ˆ d ƒ } | r*|
 j ƒ  q*n  | S(	   NR   s   runsnake %sR
   R   R   s   Profile report for target '%s'R   R   (	   t   _profilet   gett   ost   systemt   filenamet
   sort_statst   print_statsR   R   (   t   fnt   argst   kwt   elapsedt
   load_statst   resultR   R
   t   sort_R   t   statsR   R   (   t   target_optst   profile_configt   target(    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   decorate7   s2    		
(   s   timeR   N(   t   sett   Truet   Falset   NoneR   (   R$   R"   R%   (    (   R$   R"   R#   sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   profiled"   s    
	#t   ProfileStatsFilec           B   s\   e  Z d  Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(	   s°   "Store per-platform/fn profiling results in a file.

    We're still targeting Py2.5, 2.4 on 0.7 with no dependencies,
    so no json lib :(  need to roll something silly

    c         C   sˆ   t  j d  k	 o t  j j |  _ t j j | ƒ |  _ t j j	 |  j ƒ d |  _
 t j d „  ƒ |  _ |  j ƒ  |  j r„ |  j ƒ  n  d  S(   Niÿÿÿÿc           S   s   t  j t ƒ S(   N(   t   collectionst   defaultdictt   dict(    (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   <lambda>m   s    (   R   t   optionsR)   t   write_profilest   writeR   t   patht   abspatht   fnamet   splitt   short_fnameR,   R-   t   datat   _readt   _write(   t   selfR   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   __init__e   s    
	c         C   sÙ   t  j j d t  j j } d j g  t j d d !D] } t | ƒ ^ q1 ƒ } | g } | j | ƒ t	 rx | j d ƒ n  t
 rŽ | j d ƒ n  t r¤ | j d ƒ n  t  j j ƒ  } | j | rÅ d pÈ d	 ƒ d j | ƒ S(
   Nt   _t   .i    i   R   R   t   wint   cextensionst   nocextensions(   R   t   dbt   namet   drivert   joint   syst   version_infot   strt   appendR   R   R   t   requirementst   _has_cextensions(   R;   t	   dbapi_keyt   vt
   py_versiont   platform_tokenst	   _has_cext(    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   platform_keyt   s    2	c         C   s)   t  } | |  j k o( |  j |  j | k S(   N(   t   _current_testR8   RQ   (   R;   t   test_key(    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt	   has_statsˆ   s    c   	      C   sÚ   t  } |  j | } | |  j } d | k r= g  | d <} n
 | d } d | k rd d | d <} n
 | d } t | ƒ | k } | s² | j | ƒ |  j r© |  j ƒ  n  d  } n | d | | f } | d c d 7<| S(   Nt   countst   current_counti    t   linenoi   (   RR   R8   RQ   t   lenRI   R2   R:   R)   (	   R;   t	   callcountRS   t   per_fnt   per_platformRU   RV   t	   has_countR   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyR      s$    

		c         C   sN   t  } |  j | } | |  j } | d } | | d <|  j rJ |  j ƒ  n  d  S(   NRU   iÿÿÿÿ(   RR   R8   RQ   R2   R:   (   R;   RY   RS   RZ   R[   RU   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   replaceª   s    

	c         C   s   d |  j  S(   Ns  # %s
# This file is written out on a per-environment basis.
# For each test in aaa_profiling, the corresponding function and 
# environment is located within this file.  If it doesn't exist,
# the test is skipped.
# If a callcount does exist, it is compared to what we received. 
# assertions are raised if the counts do not match.
# 
# To add a new callcount test, apply the function_call_count 
# decorator and re-run the tests using the --write-profiles 
# option - this file will be rewritten including the new count.
# 
(   R5   (   R;   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   _header³   s    c         C   sñ   y t  |  j ƒ } Wn t k
 r' d  SXx¸ t | ƒ D]ª \ } } | j ƒ  } | s5 | j d ƒ ri q5 n  | j ƒ  \ } } } |  j | } | | } g  | j d ƒ D] }	 t |	 ƒ ^ q¥ }
 |
 | d <| d | d <d | d <q5 W| j	 ƒ  d  S(   Nt   #t   ,RU   i   RW   i    RV   (
   t   openR5   t   IOErrort	   enumeratet   stript
   startswithR6   R8   t   intt   close(   R;   t	   profile_fRW   t   lineRS   RQ   RU   RZ   R[   t   countt   c(    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyR9   Ã   s     
(
c         C   sÏ   d |  j  GHt |  j  d ƒ } | j |  j ƒ  ƒ x t |  j ƒ D]| } |  j | } | j d | ƒ xU t | ƒ D]G } | | } d j d „  | d Dƒ ƒ } | j d | | | f ƒ qr WqA W| j ƒ  d  S(   Ns   Writing profile file %st   ws   
# TEST: %s

R`   c         s   s   |  ] } t  | ƒ Vq d  S(   N(   RH   (   t   .0Rj   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pys	   <genexpr>à   s    RU   s	   %s %s %s
(   R5   Ra   R2   R^   t   sortedR8   RE   Rg   (   R;   Rh   RS   RZ   RQ   R[   Rk   (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyR:   Ö   s    
"(   t   __name__t
   __module__t   __doc__R<   t   propertyRQ   RT   R   R]   R^   R9   R:   (    (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyR+   ]   s   							gš™™™™™©?c            s   ‡  f d †  } | S(   s©  Assert a target for a test case's function call count.

    The main purpose of this assertion is to detect changes in
    callcounts for various functions - the actual number is not as important.
    Callcounts are stored in a file keyed to Python version and OS platform
    information.  This file is generated automatically for new tests,
    and versioned so that unexpected changes in callcounts will be detected.

    c            s   ‡  ‡ f d †  } t  | ˆ  ƒ S(   Nc             sD  t  d  k r t d ƒ ‚ n  t j ƒ  rU t j rU ˆ  |  | Ž  t d t j ƒ ‚ n  t ƒ  t ˆ  |  | Ž \ } } } | ƒ  } | j	 } t j
 | ƒ } | d  k r­ d  } n | \ }	 } d | | f GH| j ƒ  | r@t | ˆ ƒ }
 t | | ƒ |
 k } | r@t j rt j | ƒ q=t d | ˆ d | f ƒ ‚ q@n  | S(   Ns   cProfile is not installedsŒ   No profiling stats available on this platform for this function.  Run tests with --write-profiles to add statistics to %s for this platform.s   Pstats calls: %d Expected %ssy   Adjusted function call count %s not within %s%% of expected %s. Rerun with --write-profiles to regenerate this callcount.id   (   t   cProfileR)   R   t   _profile_statsRT   R2   R7   R    R   t   total_callsR   R   Rf   t   absR]   t   AssertionError(   R   R   t	   timespentR   t	   fn_resultR!   RY   t   expectedt   expected_countt   line_not   deviancet   failed(   R   t   variance(    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   wrapñ   s<    				
	
(   R   (   R   R€   (   R   (   R   sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyR%   ð   s    4(    (   R   R%   (    (   R   sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   function_call_countå   s    6c            sk   d |  j  ‰  ‡  f d †  } t j ƒ  } t j d t ƒ  t ƒ  d ˆ  ƒt j ƒ  } | | | t ƒ  d f S(   Ns   %s.profc             s    t  j ˆ  ƒ }  t j ˆ  ƒ |  S(   N(   t   pstatst   StatsR   t   unlink(   t   st(   R   (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyR   ,  s    s   result = fn(*args, **kw)R   R   (   Ro   R   Rs   t   runctxt   globalst   locals(   R   R   R   R   t   begant   ended(    (   R   sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyR   )  s    (   Rq   R   RF   R   R    R   t    R   t   plugin.plugin_baseR   R‚   R   R,   Rs   t   ImportErrorR)   R   R   R   R   RR   R*   t   objectR+   R   R   (    (    (    sb   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/sqlalchemy/testing/profiling.pyt   <module>   s&   
";ˆD