L i'dZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl mZdd l mZ ddlZdZ da d ZGd d Zdd Zej.ddZy#e$rdZY4wxYw)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) freethreading)has_compiled_extcR|atjrtjyyN) _current_test_profile_stats force_write reset_count)id_s b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/testing/profiling.py_start_current_testr7s!M!!""$"cReZdZdZd dZedZdZdZdZ dZ d Z d Z d Z y) 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. NcBtjduxrtjj|_|jxs.tjduxrtjj|_t jj||_ t jj|jd|_ tjd|_||_||_|j#|j r|j%yy)Nc4tjtSr ) collections defaultdictdictrrz+ProfileStatsFile.__init__..RsK++D1r)roptionsforce_write_profilesrwrite_profileswriteospathabspathfnamesplit short_fnamerrdatadumpsort_read_write)selffilenamer*r)s r__init__zProfileStatsFile.__init__Hs NN$ & N6>>+N+N %% NN$ & H6>>+H+H WW__X. 77==4R8++ 1     :: KKM rctjjdztjjz}tjjdk(rFtjjj tjj r|dz }djtjddDcgc] }t|c}}tr|dz }tjtjjtj j||g}|j#dt%}|j#|xrd xsd dj|Scc}w) N_sqlite_file.rrt dbapiunicode cextensions nocextensions)rdbnamedriverdialect_is_url_file_dburljoinsys version_infostrrplatformmachinesystemlowerpython_implementationappendr )r- dbapi_keyv py_versionplatform_tokens _has_cexts r platform_keyzProfileStatsFile.platform_key\s"IINNS(699+;+;; 99>>X %&))*;*;*K*K IIMM+   IXXs/?/?!/DE!s1vEF  # J     OO  # # %  * * , 2 2 4     ~.$& y:]MoNxx((Fs<E>cdt}||jvxr|j|j|vSr r r(rN)r-test_keys r has_statszProfileStatsFile.has_statsws1   ! Nd&7&7499X;N&N rc@t}|j|}||j}d|vrgx|d<}n|d}d|vrdx|d<}n|d}t||kD}|s0|j ||j r|j d}n |d||f}|dxxdz cc<|S)Ncounts current_countrlinenor)r r(rNlenrHr!r,) r- callcountrQper_fn per_platformrTrU has_countresults rr\zProfileStatsFile.result}s 8$d//0 < '.0 0L "V!(+F , .<= =L )M(9MK-/  MM) $zz F!(+VM-BBF_%*% rct}||jvry|j|}|j|vry||j}d|vr g|dddyy)NrTrP)r-rQrYrZs rrzProfileStatsFile.reset_countsc  499 $ 8$   F * d//0 | #(*L "1 % $rct}|j|}||j}|d}|d}|t|kr |||dz <n||d<|jr|j yy)NrTrUrr)r r(rNrWr!r,)r-rXrQrYrZrTrUs rreplacezProfileStatsFile.replacesr 8$d//0 h'$_5 3v; &(1F=1$ %"F2J :: KKM rc d|jzS)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-s r_headerzProfileStatsFile._headers ZZ! rc t|j}t|D]\}}|j }|r|j dr*|j \}}}|j|}||}|j dD cgc] } t| } } | |d<|dz|d<d|d<|jy#t$rYywxYwcc} w)N#,rTrrVrrU) openr%OSError enumeratestrip startswithr&r(intclose) r- profile_frVlinerQrNrTrYrZcountcs rr+zProfileStatsFile._reads TZZ(I&i0 .LFD::q|jy) NzWriting profile file %swz # TEST: %s rdc32K|]}t|ywr )rB).0rns r z*ProfileStatsFile._write..sLESZLsrT  ) printr%rer!rasortedr(r?rk)r-rlrQrYrNrZros rr,zProfileStatsFile._writes '$**45S)  'tyy) LHYYx(F OO.9 : &v L %l3 HHL\(5KLL, JK L L r) cumulativeN)__name__ __module__ __qualname____doc__r/propertyrNrRr\rr_rar+r,rrrrr?sC())4 6 + && rrc0ddlm}|fd}|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) decoratorct D] }||i| t}t5|D] }||i|} cdddS#1swYyxYw)N)variance)rangecount_functions) fnargskwwarm timerangetimervtimesrwarmups rwrapz!function_call_count..wrapsh&M D OO %L h / ! %__ %   s A  A)sqlalchemy.utilr)rrrrrs``` rfunction_call_countrs!* Krc #Kttjdtj s6tj s&tj dtjzttj}|jd|jtj|tj}|j }tj#|}|d}n|\}}t%d||fz|j&t)j*dtj,|j/tj0rtt2j4j7tj0\}}t8j+dd} |d| |xsd } |j;| t%d | ztj<rtj?|y|rqtA||z} tC||z | kD} | rOtj rtj?|ytEd |d |d zd|dtjFd yyw)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[, ]r4rr1z.profilezDumped stats to file %szAdjusted function call count z not within dz% of expected z , platform z;. Rerun with --write-profiles to regenerate this callcount.)$cProfiler_skip_test_exceptionr rRr! skip_testr'rProfileenabledisablepstatsStatsr@stdout total_callsr\rw sort_statsrer&r* print_statsr)r"r#splitextr dump_statsrr_rjabsAssertionErrorrN) rprstatsrXexpectedexpected_countline_nobaseext test_namedumpfiledeviancefaileds rrrs))*EFF  # # %n.B.B  .99 : L    BIIK JJL LLCJJ /E!!I$$Y/H"* (I~+F FGEbhhw(;(;<= GG$$^%8%89 c!'',R0 $i1B 1BC " '(23!!y) y8+,Y/08; ##&&y1$ "!C&&33     sI'I))皙?rr)r)r} __future__rr contextlibr"rCrrr@rutilrrr r ImportErrorr r rrrcontextmanagerrrrrrs#   # %ddN> AAGHsA--A76A7