프로파일러를 사용할 수 있도록 설정한 후에는 저장 프로시저를 호출하여 프로파일러 출력을 생성하는 것만으로 프로파일러를 사용할 수 있습니다. 프로시저 실행이 완료되면 프로파일러의 출력은 사용자가 지정한 스테이지의 파일에 기록됩니다. 시스템 함수를 사용하여 프로파일러 출력을 가져올 수 있습니다.
PYTHON_PROFILER_MODULES 매개 변수를 사용하여 기본적으로 포함되지 않는 모듈을 프로필에 포함할 수 있습니다. 이러한 방식으로 모듈을 포함하면 해당 모듈에서 사용된 모든 함수가 프로파일러 출력에 포함됩니다. 기본적으로 PYTHON_PROFILER_MODULES 매개 변수 값은 빈 문자열('')이며, 프로필은 인라인 처리기 코드(있는 경우)만 프로파일링합니다.
프로파일링할 모듈을 포함하려면 아래 그림과 같이 쉼표로 구분된 목록에 매개 변수 값으로 모듈 이름을 지정합니다.
이 예의 코드는 프로파일러를 사용하여 라인 사용량 보고서를 생성하고 검색하는 방법을 보여줍니다.
CREATEORREPLACEPROCEDURElast_n_query_duration(last_nNUMBER,totalNUMBER)RETURNSSTRINGLANGUAGEPYTHONRUNTIME_VERSION=3.12PACKAGES=('snowflake-snowpark-python')HANDLER='main'AS$$importsnowflake.snowpark.functionsasfuncsdefmain(session,last_n,total):# create sample dataset to emulate id + elapsed timesession.sql(''' CREATE OR REPLACE TABLE sample_query_history (query_id INT, elapsed_time FLOAT) ''').collect()session.sql(''' INSERT INTO sample_query_history SELECT seq8() AS query_id, uniform(0::float, 100::float, random()) as elapsed_time FROM table(generator(rowCount => {0}));'''.format(total)).collect()# get the mean of the last n query elapsed timedf=session.table('sample_query_history').select(funcs.col('query_id'),funcs.col('elapsed_time')).limit(last_n)pandas_df=df.to_pandas()mean_time=pandas_df.loc[:,'ELAPSED_TIME'].mean()delpandas_dfreturnmean_time$$;CREATETEMPORARYSTAGEprofiler_output;ALTERSESSIONSETPYTHON_PROFILER_TARGET_STAGE="my_database.my_schema.profiler_output";ALTERSESSIONSETACTIVE_PYTHON_PROFILER='LINE';-- Sample 1 million from 10 million recordsCALLlast_n_query_duration(1000000,10000000);SELECTSNOWFLAKE.CORE.GET_PYTHON_PROFILER_OUTPUT(last_query_id());
라인 프로파일러 출력은 다음과 같습니다.
Handler Name: mainPython Runtime Version: 3.12Modules Profiled: ['main_module']Timer Unit: 0.001 sTotal Time: 8.96127 sFile: _udf_code.pyFunction: main at line 4Line # Hits Time Per Hit % Time Line Contents============================================================== 4 def main(session, last_n, total): 5 # create sample dataset to emulate id + elapsed time 6 1 122.3 122.3 1.4 session.sql(''' 7 CREATE OR REPLACE TABLE sample_query_history (query_id INT, elapsed_time FLOAT)''').collect() 8 2 7248.4 3624.2 80.9 session.sql(''' 9 INSERT INTO sample_query_history 10 SELECT 11 seq8() AS query_id, 12 uniform(0::float, 100::float, random()) as elapsed_time 13 1 0.0 0.0 0.0 FROM table(generator(rowCount => {0}));'''.format(total)).collect() 14 15 # get the mean of the last n query elapsed time 16 3 58.6 19.5 0.7 df = session.table('sample_query_history').select( 17 1 0.0 0.0 0.0 funcs.col('query_id'), 18 2 0.0 0.0 0.0 funcs.col('elapsed_time')).limit(last_n) 19 20 1 1528.4 1528.4 17.1 pandas_df = df.to_pandas() 21 1 3.2 3.2 0.0 mean_time = pandas_df.loc[:, 'ELAPSED_TIME'].mean() 22 1 0.3 0.3 0.0 del pandas_df 23 1 0.0 0.0 0.0 return mean_time
메모리 프로파일러 출력은 다음과 같습니다.
ALTER SESSION SET ACTIVE_PYTHON_PROFILER = 'MEMORY';Handler Name: mainPython Runtime Version: 3.12Modules Profiled: ['main_module']File: _udf_code.pyFunction: main at line 4Line # Mem usage Increment Occurrences Line Contents============================================================= 4 245.3 MiB 245.3 MiB 1 def main(session, last_n, total): 5 # create sample dataset to emulate id + elapsed time 6 245.8 MiB 0.5 MiB 1 session.sql(''' 7 CREATE OR REPLACE TABLE sample_query_history (query_id INT, elapsed_time FLOAT)''').collect() 8 245.8 MiB 0.0 MiB 2 session.sql(''' 9 INSERT INTO sample_query_history 10 SELECT 11 seq8() AS query_id, 12 uniform(0::float, 100::float, random()) as elapsed_time 13 245.8 MiB 0.0 MiB 1 FROM table(generator(rowCount => {0}));'''.format(total)).collect() 14 15 # get the mean of the last n query elapsed time 16 245.8 MiB 0.0 MiB 3 df = session.table('sample_query_history').select( 17 245.8 MiB 0.0 MiB 1 funcs.col('query_id'), 18 245.8 MiB 0.0 MiB 2 funcs.col('elapsed_time')).limit(last_n) 19 20 327.9 MiB 82.1 MiB 1 pandas_df = df.to_pandas() 21 328.9 MiB 1.0 MiB 1 mean_time = pandas_df.loc[:, 'ELAPSED_TIME'].mean() 22 320.9 MiB -8.0 MiB 1 del pandas_df 23 320.9 MiB 0.0 MiB 1 return mean_time