U a*@sdZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z z ddl Z Wnek rdZ YnXdZdadd ZGd d d eZdd dZejdddZdS)zProfiling 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. N)config) gc_collect)has_compiled_extcCs|atjrtdSN) _current_test_profile_stats force_write reset_count)Zid_r ]C:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\testing\profiling.py_start_current_test2src@s^eZdZdZdddZeddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ dS)ProfileStatsFilezStore per-platform/fn profiling results in a file. There was no json module available when this was written, but now the file format which is very deterministically line oriented is kind of handy in any case for diffs and merges. cumulativeNcCstjdk otjj|_|jp*tjdk o*tjj|_tj||_ tj |j d|_ t dd|_||_||_||jr|dS)NcSs ttSr) collections defaultdictdictr r r r Mz+ProfileStatsFile.__init__..)roptionsZforce_write_profilesr Zwrite_profileswriteospathabspathfnamesplit short_fnamerrdatadumpsort_read_write)selffilenamer!r r r r __init__CszProfileStatsFile.__init__cCstjjdtjj}tjjdkr:tjjtjjr:|d7}dddtj ddD}t t t  ||g}|tjjjrd nd t}||rd pd d|S) N_Zsqlite_file.cSsg|] }t|qSr str).0vr r r bsz1ProfileStatsFile.platform_key..rrZ nativeunicodeZ dbapiunicodeZ cextensionsZ nocextensions)rdbnameZdriverdialectZ_is_url_file_dburljoinsys version_infoplatformmachinesystemlowerpython_implementationappendZconvert_unicoder)r$Z dbapi_key py_versionZplatform_tokensZ _has_cextr r r platform_keyWs(  zProfileStatsFile.platform_keycCst}||jko|j|j|kSrrrr=)r$test_keyr r r has_statsuszProfileStatsFile.has_statsc Cst}|j|}||j}d|kr.g|d<}n|d}d|krLd|d<}n|d}t||k}|s|||jr||d}n|d||f}|dd7<|S)Ncounts current_countrlinenor)rrr=lenr;rr#) r$ callcountr?per_fn per_platformrArBZ has_countresultr r r rH{s$    zProfileStatsFile.resultcCsPt}||jkrdS|j|}|j|kr*dS||j}d|krLg|ddd<dS)NrAr>)r$r?rFrGr r r r s    zProfileStatsFile.reset_countcCs\t}|j|}||j}|d}|d}|t|krB|||d<n||d<|jrX|dS)NrArBrr)rrr=rDrr#)r$rEr?rFrGrArBr r r replaces   zProfileStatsFile.replacecCs d|jS)Na # %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. # )r)r$r r r _headerszProfileStatsFile._headerc Cszt|j}Wntk r$YdSXt|D]p\}}|}|r.|drNq.|\}}}|j|}||}dd|dD} | |d<|d|d<d|d <q.|dS) N#cSsg|] }t|qSr )intr,countr r r r.sz*ProfileStatsFile._read..,rArrCrrB) openrIOError enumeratestrip startswithrrclose) r$ profile_frCliner?r=rArFrGcr r r r"s    zProfileStatsFile._readcCstd|jt|jd}||t|jD]^}|j|}|d|t|D]8}||}ddd|dD}|d|||fqVq2|dS) NzWriting profile file %swz # TEST: %s rOcss|]}t|VqdSrr*rMr r r sz*ProfileStatsFile._write..rAz %s %s %s ) printrrPrrJsortedrr3rU)r$rVr?rFr=rGrXr r r r#s   zProfileStatsFile._write)rN)__name__ __module__ __qualname____doc__r&propertyr=r@rHr rIrJr"r#r r r r r:s    r皙?csNddlm}ddlmddlmddlm|fdd}|S)aAssert 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. r) decorator) deprecations)row)mockc sjddjjdjttD]}|||q0t}td>|D]}|||}qX|W5QRW5QRW5QRSQRXW5QRXW5QRXdS)NZSQLALCHEMY_WARN_20FZ_default_key_style)variance)patchobjectZ LegacyRowZKEY_OBJECTS_NO_WARNrangecount_functions)fnargskwZwarmZ timerangetimervrdrfretimesrgwarmupr r wraps$    z!function_call_count..wrap)Zsqlalchemy.utilrcrdZsqlalchemy.enginereZsqlalchemy.testingrf)rgrrrsrcrtr rqr function_call_counts    ruc csttdkrtdts0tjs0tdtjtt }| dV| t j |tjd}|j}t|}|dkrd}n|\}}td||f|jtdtj|tjrtjtj\}}tdd} d|| |pd f} || td | tjrt|nV|rpt ||} t!||| k} | rptjrVt|nt"d ||d |tj#fdS) NzcProfile is not installedzNo profiling stats available on this platform for this function. Run tests with --write-profiles to add statistics to %s for this platform.)streamzPstats calls: %d Expected %sz[, ]r)rz%s_%s%sz.profilezDumped stats to file %szAdjusted function call count %s not within %s%% of expected %s, platform %s. Rerun with --write-profiles to regenerate this callcount.d)$cProfilerZ_skip_test_exceptionr r@rZ skip_testrrZProfileenabledisablepstatsZStatsr4stdoutZ total_callsrHr[Z sort_statsrerr!Z print_statsr rrsplitextrZ dump_statsr rIrLabsAssertionErrorr=) rgprstatsrEexpectedZexpected_countZline_nobaseextZ test_nameZdumpfileZdeviancefailedr r r rk sZ       rk)rbrr)rb)r`r contextlibrr6r{r}r4rutilrrrx ImportErrorr rrrirrucontextmanagerrkr r r r s,      , (