U kf'@sdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl mZz ddlZWnek rdZYnXdZdad d ZGd d d ZdddZejdddZdS)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. ) annotationsN)config) gc_collect)has_compiled_extcCs|atjrtdSN) _current_test_profile_stats force_write reset_count)Zid_r L/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/testing/profiling.py_start_current_test6src@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 rQz+ProfileStatsFile.__init__..)roptionsZforce_write_profilesr Zwrite_profileswriteospathabspathfnamesplit short_fnamerrdatadumpsort_read_write)selffilenamer"r!r r r__init__GszProfileStatsFile.__init__cCstjjdtjj}tjjdkr:tjjtjjr:|d7}dddtj ddD}t t t  ||g}|d t}||rd pd d|S) N_Zsqlite_file.cSsg|] }t|qSr str).0vr r r esz1ProfileStatsFile.platform_key..rrZ dbapiunicodeZ cextensionsZ nocextensions)rdbnameZdriverdialectZ_is_url_file_dburljoinsys version_infoplatformmachinesystemlowerpython_implementationappendr)r%Z dbapi_key py_versionZplatform_tokensZ _has_cextr r r platform_key[s    zProfileStatsFile.platform_keycCst}||jko|j|j|kSrr r r>)r%test_keyr r r has_statstszProfileStatsFile.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)r r r>lenr<rr$) r% callcountr@per_fn per_platformrBrCZ has_countresultr r rrIzs$    zProfileStatsFile.resultcCsPt}||jkrdS|j|}|j|kr*dS||j}d|krLg|ddd<dS)NrBr?)r%r@rGrHr r rr s    zProfileStatsFile.reset_countcCs\t}|j|}||j}|d}|d}|t|krB|||d<n||d<|jrX|dS)NrBrCrr)r r r>rErr$)r%rFr@rGrHrBrCr r rreplaces   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 rr/sz*ProfileStatsFile._read..,rBrrDrrC) openrOSError enumeratestrip startswithrr close) r% profile_frDliner@r>rBrGrHcr r rr#s    zProfileStatsFile._readcCstd|jt|jd}||t|jD]^}|j|}|d|t|D]8}||}ddd|dD}|d|||fqVq2|dS) NzWriting profile file %swz # TEST: %s rPcss|]}t|VqdSrr+rNr r r sz*ProfileStatsFile._write..rBz %s %s %s ) printrrQrrKsortedr r4rV)r%rWr@rGr>rHrYr r rr$s   zProfileStatsFile._write)rN)__name__ __module__ __qualname____doc__r'propertyr>rArIr rJrKr#r$r r r rr>s    r皙?cs$ddlm}|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) decoratorc sZtD]}|||qt}td&|D]}|||}q0|W5QRSQRXdS)N)variance)rangecount_functions)fnargskwZwarmZ timerangetimervtimesrewarmupr rwraps    z!function_call_count..wrap)Zsqlalchemy.utilrd)rernrordrpr rmrfunction_call_counts  rqc 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 rArZ skip_testrrZProfileenabledisablepstatsZStatsr5stdoutZ total_callsrIr\Z sort_statsrerr"Z print_statsr!rrsplitextr Z dump_statsr rJrMabsAssertionErrorr>) reprstatsrFexpectedZexpected_countline_nobaseextZ test_nameZdumpfileZdevianceZfailedr r rrgsZ       rg)rcrr)rc)ra __future__rr contextlibrr7rwryr5rutilrrrt ImportErrorr r rrrqcontextmanagerrgr r r r s.       &