PrefectHQ / prefect

Prefect is a workflow orchestration framework for building resilient data pipelines in Python.
https://prefect.io
Apache License 2.0
15.33k stars 1.5k forks source link

Improve speed of Prefect CLI #12274

Open serinamarie opened 3 months ago

serinamarie commented 3 months ago

First check

Prefect Version

2.x

Describe the current behavior

When running prefect commands such as prefect flow-run ls, we see some potential performance bottlenecks regarding various import-related operations indicating potentially high overhead loading and initializing modules. We may be loading more modules than necessary or frequently reloading modules during execution? Utility functions like [_utilities.py](utilities.py:83(wrapper)) and asyncutils.py:263(coroutine_wrapper) are invoked often, have moderate cumulative time, and suggest some overhead as well.

Repeated calls to import-related functions like frozen importlib._bootstrap_external and frozen importlib._bootstrap also support the hypothesis that we could be loading modules unnecessarily, inefficiently, or redundantly.

Screenshot 2024-05-07 at 10 04 43 AM

Possible action item: Review the module imports hierarchy? Consider more lazy loading to speed CLI initialization time.

Screenshot 2024-05-07 at 10 11 50 AM Screenshot 2024-05-07 at 10 15 41 AM

Screenshot 2024-05-07 at 10 16 24 AM

Next step

Besides the aforementioned imports issues, runner.py:1(<module>) shows considerable time being used. I'd guess a lot of that is import-related as well. Perhaps that is an area we can explore improving.

Caveats

These are findings from running a single, more "innocuous" command (prefect flow-run ls). I imagine that running it on other commands could lead to different results.

serinamarie commented 1 month ago

Functions where we're spending a lot of time:

Screenshot 2024-05-07 at 1 01 39 PM
serinamarie commented 1 month ago
python -m cprofilev $(which prefect) --help
5577910 function calls (5143143 primitive calls) in 3.446 seconds

   Ordered by: cumulative time

   [ncalls](http://127.0.0.1:4000/?sort=calls)  [tottime](http://127.0.0.1:4000/?sort=time)  percall  [cumtime](http://127.0.0.1:4000/?sort=cumulative)  percall [filename](http://127.0.0.1:4000/?sort=module):[lineno](http://127.0.0.1:4000/?sort=nfl)(function)
   2946/1    0.038    0.000    3.449    3.449 {built-in method builtins.exec}
        1    0.000    0.000    3.449    3.449 /Users/bean/code-oss/prefect/venv311/bin/prefect:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
   2503/2    0.009    0.000    3.360    1.680 <frozen importlib._bootstrap>:1167([_find_and_load](http://127.0.0.1:4000/?func_name=_find_and_load))
   2441/2    0.007    0.000    3.360    1.680 <frozen importlib._bootstrap>:1122([_find_and_load_unlocked](http://127.0.0.1:4000/?func_name=_find_and_load_unlocked))
   2312/3    0.006    0.000    3.359    1.120 <frozen importlib._bootstrap>:666([_load_unlocked](http://127.0.0.1:4000/?func_name=_load_unlocked))
   2242/3    0.003    0.000    3.359    1.120 <frozen importlib._bootstrap_external>:934([exec_module](http://127.0.0.1:4000/?func_name=exec_module))
   5172/5    0.002    0.000    3.358    0.672 <frozen importlib._bootstrap>:233([_call_with_frames_removed](http://127.0.0.1:4000/?func_name=_call_with_frames_removed))
   476/32    0.001    0.000    2.920    0.091 {built-in method builtins.__import__}
        1    0.000    0.000    2.611    2.611 /Users/bean/code-oss/prefect/src/prefect/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    2.404    2.404 /Users/bean/code-oss/prefect/src/prefect/runner/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    2.403    2.403 /Users/bean/code-oss/prefect/src/prefect/runner/runner.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
 2080/574    0.002    0.000    1.735    0.003 <frozen importlib._bootstrap>:1209([_handle_fromlist](http://127.0.0.1:4000/?func_name=_handle_fromlist))
        1    0.000    0.000    1.439    1.439 /Users/bean/code-oss/prefect/src/prefect/deployments/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    1.401    1.401 /Users/bean/code-oss/prefect/src/prefect/deployments/deployments.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    1.319    1.319 /Users/bean/code-oss/prefect/src/prefect/deployments/schedules.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    1.318    1.318 /Users/bean/code-oss/prefect/src/prefect/server/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
6191/6040    0.234    0.000    1.049    0.000 {built-in method builtins.__build_class__}
        1    0.000    0.000    0.789    0.789 /Users/bean/code-oss/prefect/src/prefect/client/orchestration.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.746    0.746 /Users/bean/code-oss/prefect/src/prefect/cli/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.714    0.714 /Users/bean/code-oss/prefect/src/prefect/server/services/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.710    0.710 /Users/bean/code-oss/prefect/src/prefect/server/services/task_scheduling.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.710    0.710 /Users/bean/code-oss/prefect/src/prefect/server/api/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.615    0.615 /Users/bean/code-oss/prefect/src/prefect/cli/work_pool.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.604    0.604 /Users/bean/code-oss/prefect/src/prefect/server/models/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.600    0.600 /Users/bean/code-oss/prefect/src/prefect/infrastructure/provisioners/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        7    0.000    0.000    0.598    0.085 /Users/bean/code-oss/prefect/src/prefect/utilities/importtools.py:247([lazy_import](http://127.0.0.1:4000/?func_name=lazy_import))
        1    0.000    0.000    0.597    0.597 /Users/bean/code-oss/prefect/src/prefect/infrastructure/provisioners/modal.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.597    0.597 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:1761([stack](http://127.0.0.1:4000/?func_name=stack))
        1    0.000    0.000    0.597    0.597 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:1731([getouterframes](http://127.0.0.1:4000/?func_name=getouterframes))
       30    0.000    0.000    0.597    0.020 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:1670([getframeinfo](http://127.0.0.1:4000/?func_name=getframeinfo))
       96    0.075    0.001    0.589    0.006 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:973([getmodule](http://127.0.0.1:4000/?func_name=getmodule))
        1    0.000    0.000    0.567    0.567 /Users/bean/code-oss/prefect/src/prefect/server/models/agents.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     2242    0.011    0.000    0.544    0.000 <frozen importlib._bootstrap_external>:1007([get_code](http://127.0.0.1:4000/?func_name=get_code))
      170    0.000    0.000    0.535    0.003 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/routing.py:678([decorator](http://127.0.0.1:4000/?func_name=decorator))
      170    0.001    0.000    0.534    0.003 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/routing.py:566([add_api_route](http://127.0.0.1:4000/?func_name=add_api_route))
      167    0.000    0.000    0.533    0.003 /Users/bean/code-oss/prefect/src/prefect/server/utilities/server.py:120([add_api_route](http://127.0.0.1:4000/?func_name=add_api_route))
      170    0.003    0.000    0.533    0.003 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/routing.py:344([__init__](http://127.0.0.1:4000/?func_name=__init__))
       30    0.000    0.000    0.486    0.016 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:1055([findsource](http://127.0.0.1:4000/?func_name=findsource))
236165/662    0.186    0.000    0.412    0.001 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/copy.py:128([deepcopy](http://127.0.0.1:4000/?func_name=deepcopy))
 8448/337    0.075    0.000    0.409    0.001 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/copy.py:227([_deepcopy_dict](http://127.0.0.1:4000/?func_name=_deepcopy_dict))
6207/2665    0.030    0.000    0.398    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/copy.py:259([_reconstruct](http://127.0.0.1:4000/?func_name=_reconstruct))
4524/1476    0.008    0.000    0.374    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/copy.py:210([_deepcopy_tuple](http://127.0.0.1:4000/?func_name=_deepcopy_tuple))
 1016/109    0.028    0.000    0.373    0.003 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/utils.py:125([create_cloned_field](http://127.0.0.1:4000/?func_name=create_cloned_field))
4524/1476    0.004    0.000    0.371    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/copy.py:211([<listcomp>](http://127.0.0.1:4000/?func_name=%3Clistcomp%3E))
     2851    0.359    0.000    0.359    0.000 {method 'read' of '_io.BufferedReader' objects}
     2243    0.004    0.000    0.358    0.000 <frozen importlib._bootstrap_external>:1127([get_data](http://127.0.0.1:4000/?func_name=get_data))
199829/196257    0.029    0.000    0.324    0.000 {built-in method builtins.hasattr}
        2    0.000    0.000    0.290    0.145 <frozen importlib.util>:226([__getattribute__](http://127.0.0.1:4000/?func_name=__getattribute__))
        1    0.000    0.000    0.256    0.256 /Users/bean/code-oss/prefect/src/prefect/client/schemas/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     7852    0.005    0.000    0.254    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:3276([signature](http://127.0.0.1:4000/?func_name=signature))
7867/7852    0.007    0.000    0.249    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:3022([from_callable](http://127.0.0.1:4000/?func_name=from_callable))
9896/7852    0.045    0.000    0.242    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:2425([_signature_from_callable](http://127.0.0.1:4000/?func_name=_signature_from_callable))
        1    0.000    0.000    0.241    0.241 /Users/bean/code-oss/prefect/src/prefect/client/schemas/actions.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.214    0.214 /Users/bean/code-oss/prefect/src/prefect/server/database/dependencies.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.212    0.212 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/kubernetes/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.210    0.210 /Users/bean/code-oss/prefect/src/prefect/server/database/interface.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.202    0.202 /Users/bean/code-oss/prefect/src/prefect/server/database/orm_models.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.192    0.192 /Users/bean/code-oss/prefect/src/prefect/_internal/compatibility/deprecated.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.191    0.191 /Users/bean/code-oss/prefect/src/prefect/server/schemas/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     2811    0.004    0.000    0.187    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/re/__init__.py:272([_compile](http://127.0.0.1:4000/?func_name=_compile))
      539    0.000    0.000    0.181    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/re/__init__.py:225([compile](http://127.0.0.1:4000/?func_name=compile))
      486    0.002    0.000    0.179    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/re/_[compile](http://127.0.0.1:4000/?func_name=compile)r.py:738(compile)
        1    0.000    0.000    0.173    0.173 /Users/bean/code-oss/prefect/src/prefect/client/schemas/objects.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
2530/1620    0.004    0.000    0.168    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:936([getsourcefile](http://127.0.0.1:4000/?func_name=getsourcefile))
     2390    0.015    0.000    0.160    0.000 <frozen importlib._bootstrap>:1056([_find_spec](http://127.0.0.1:4000/?func_name=_find_spec))
        1    0.000    0.000    0.158    0.158 /Users/bean/code-oss/prefect/src/prefect/server/api/deployments.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.001    0.001    0.145    0.145 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/kubernetes/client/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.144    0.144 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/kubernetes/client/api/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     2242    0.004    0.000    0.142    0.000 <frozen importlib._bootstrap_external>:727([_compile_bytecode](http://127.0.0.1:4000/?func_name=_compile_bytecode))
     7836    0.045    0.000    0.139    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:2330([_signature_from_function](http://127.0.0.1:4000/?func_name=_signature_from_function))
     2242    0.137    0.000    0.137    0.000 {built-in method marshal.loads}
        1    0.000    0.000    0.134    0.134 /Users/bean/code-oss/prefect/src/prefect/server/api/artifacts.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     2387    0.001    0.000    0.130    0.000 <frozen importlib._bootstrap_external>:1496([find_spec](http://127.0.0.1:4000/?func_name=find_spec))
     2387    0.005    0.000    0.128    0.000 <frozen importlib._bootstrap_external>:1464([_get_spec](http://127.0.0.1:4000/?func_name=_get_spec))
     2472    0.002    0.000    0.128    0.000 <frozen posixpath>:412([realpath](http://127.0.0.1:4000/?func_name=realpath))
        1    0.000    0.000    0.126    0.126 /Users/bean/code-oss/prefect/src/prefect/_version.py:667([get_versions](http://127.0.0.1:4000/?func_name=get_versions))
        1    0.000    0.000    0.126    0.126 /Users/bean/code-oss/prefect/src/prefect/_version.py:270([git_pieces_from_vcs](http://127.0.0.1:4000/?func_name=git_pieces_from_vcs))
        5    0.000    0.000    0.125    0.025 /Users/bean/code-oss/prefect/src/prefect/_version.py:81([run_command](http://127.0.0.1:4000/?func_name=run_command))
        1    0.000    0.000    0.121    0.121 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/kubernetes/client/api/well_known_api.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.121    0.121 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/kubernetes/client/api_client.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
2474/2472    0.027    0.000    0.119    0.000 <frozen posixpath>:421([_joinrealpath](http://127.0.0.1:4000/?func_name=_joinrealpath))
        1    0.001    0.001    0.118    0.118 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/kubernetes/client/models/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
64909/6497    0.009    0.000    0.117    0.000 <frozen abc>:121([__subclasscheck__](http://127.0.0.1:4000/?func_name=__subclasscheck__))
64909/6497    0.108    0.000    0.115    0.000 {built-in method _abc._abc_subclasscheck}
     1566    0.033    0.000    0.114    0.000 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/utils.py:86([create_response_field](http://127.0.0.1:4000/?func_name=create_response_field))
     3159    0.020    0.000    0.114    0.000 <frozen importlib._bootstrap_external>:1604([find_spec](http://127.0.0.1:4000/?func_name=find_spec))
15550/14617    0.009    0.000    0.114    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/typing.py:338([inner](http://127.0.0.1:4000/?func_name=inner))
        5    0.000    0.000    0.111    0.022 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/subprocess.py:1163([communicate](http://127.0.0.1:4000/?func_name=communicate))
        1    0.000    0.000    0.108    0.108 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.107    0.107 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/applications.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.104    0.104 /Users/bean/code-oss/prefect/src/prefect/events/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.099    0.099 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/routing.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.099    0.099 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
  408/188    0.003    0.000    0.098    0.001 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/dependencies/utils.py:313([get_dependant](http://127.0.0.1:4000/?func_name=get_dependant))
        1    0.000    0.000    0.090    0.090 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/main.py:305([__call__](http://127.0.0.1:4000/?func_name=__call__))
      486    0.001    0.000    0.090    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/re/_[parse](http://127.0.0.1:4000/?func_name=parse)r.py:970(parse)
        1    0.000    0.000    0.089    0.089 /Users/bean/code-oss/prefect/src/prefect/states.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
2626/2110    0.002    0.000    0.087    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/copy.py:201([_deepcopy_list](http://127.0.0.1:4000/?func_name=_deepcopy_list))
 1537/486    0.005    0.000    0.087    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/re/_parser.py:447([_parse_sub](http://127.0.0.1:4000/?func_name=_parse_sub))
      486    0.001    0.000    0.087    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/re/_compiler.py:571([_code](http://127.0.0.1:4000/?func_name=_code))
        1    0.000    0.000    0.086    0.086 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/dependencies/models.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.085    0.085 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/security/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.085    0.085 /Users/bean/code-oss/prefect/src/prefect/results.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.085    0.085 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/engine/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
 2632/554    0.032    0.000    0.084    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/re/_parser.py:507([_parse](http://127.0.0.1:4000/?func_name=_parse))
        1    0.000    0.000    0.083    0.083 /Users/bean/code-oss/prefect/src/prefect/utilities/callables.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.083    0.083 /Users/bean/code-oss/prefect/src/prefect/server/api/automations.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.081    0.081 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/security/api_key.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
2315/2307    0.003    0.000    0.080    0.000 <frozen importlib._bootstrap>:566([module_from_spec](http://127.0.0.1:4000/?func_name=module_from_spec))
        1    0.001    0.001    0.079    0.079 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/openapi/models.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
12956/12253    0.029    0.000    0.078    0.000 {built-in method __new__ of type object at 0x100fcb308}
        1    0.000    0.000    0.077    0.077 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/docker/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     2243    0.077    0.000    0.077    0.000 {built-in method io.open_code}
    60/36    0.000    0.000    0.077    0.002 <frozen importlib._bootstrap_external>:1239([exec_module](http://127.0.0.1:4000/?func_name=exec_module))
    60/36    0.017    0.000    0.077    0.002 {built-in method _imp.exec_dynamic}
   124/97    0.000    0.000    0.073    0.001 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/utils.py:160([<listcomp>](http://127.0.0.1:4000/?func_name=%3Clistcomp%3E))
        1    0.000    0.000    0.072    0.072 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/docker/api/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.071    0.071 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/docker/api/client.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.071    0.071 /Users/bean/code-oss/prefect/src/prefect/server/events/actions.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.069    0.069 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/engine/events.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
1339/1336    0.001    0.000    0.069    0.000 <frozen abc>:105([__new__](http://127.0.0.1:4000/?func_name=__new__))
        1    0.000    0.000    0.067    0.067 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
 4031/486    0.016    0.000    0.065    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/re/_compiler.py:37([_compile](http://127.0.0.1:4000/?func_name=_compile))
        1    0.000    0.000    0.064    0.064 /Users/bean/code-oss/prefect/src/prefect/events/schemas/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.064    0.064 /Users/bean/code-oss/prefect/src/prefect/events/schemas/deployment_triggers.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.063    0.063 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/engine/interfaces.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.063    0.063 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/sql/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.062    0.062 /Users/bean/code-oss/prefect/src/prefect/blocks/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.061    0.061 /Users/bean/code-oss/prefect/src/prefect/server/schemas/core.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.061    0.061 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/main.py:338([get_command](http://127.0.0.1:4000/?func_name=get_command))
        1    0.000    0.000    0.060    0.060 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/main.py:329([get_group](http://127.0.0.1:4000/?func_name=get_group))
     49/1    0.001    0.000    0.060    0.060 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/main.py:474([get_group_from_info](http://127.0.0.1:4000/?func_name=get_group_from_info))
        1    0.000    0.000    0.060    0.060 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/ext/asyncio/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
760425/760423    0.059    0.000    0.060    0.000 {built-in method builtins.isinstance}
        1    0.000    0.000    0.060    0.060 /Users/bean/code-oss/prefect/src/prefect/server/api/admin.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.059    0.059 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/kubernetes/config/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.058    0.058 /Users/bean/code-oss/prefect/src/prefect/server/api/workers.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.058    0.058 /Users/bean/code-oss/prefect/src/prefect/_internal/pydantic/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.058    0.058 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/kubernetes/config/kube_config.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
2531/1614    0.001    0.000    0.058    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/typing.py:458([__getitem__](http://127.0.0.1:4000/?func_name=__getitem__))
      240    0.001    0.000    0.058    0.000 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/main.py:562([get_command_from_info](http://127.0.0.1:4000/?func_name=get_command_from_info))
        1    0.005    0.005    0.056    0.056 /Users/bean/code-oss/prefect/src/prefect/settings.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.056    0.056 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/httpx/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.052    0.052 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/ext/asyncio/events.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.052    0.052 /Users/bean/code-oss/prefect/src/prefect/server/utilities/database.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.052    0.052 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/ext/asyncio/session.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     1331    0.048    0.000    0.052    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/re/_compiler.py:241([_optimize_charset](http://127.0.0.1:4000/?func_name=_optimize_charset))
        1    0.000    0.000    0.051    0.051 /Users/bean/code-oss/prefect/src/prefect/server/api/flow_runs.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.050    0.050 /Users/bean/code-oss/prefect/src/prefect/cli/root.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.049    0.049 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/orm/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.049    0.049 /Users/bean/code-oss/prefect/src/prefect/server/events/schemas/automations.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.049    0.049 /Users/bean/code-oss/prefect/src/prefect/utilities/importtools.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.049    0.049 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/requests/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     3336    0.009    0.000    0.049    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/typing.py:1330([__init__](http://127.0.0.1:4000/?func_name=__init__))
        1    0.000    0.000    0.048    0.048 /Users/bean/code-oss/prefect/src/prefect/cli/agent.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.048    0.048 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
    61115    0.032    0.000    0.048    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/typing.py:2369([get_origin](http://127.0.0.1:4000/?func_name=get_origin))
     1580    0.005    0.000    0.048    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/typing.py:635([Union](http://127.0.0.1:4000/?func_name=Union))
        1    0.000    0.000    0.048    0.048 /Users/bean/code-oss/prefect/src/prefect/agent.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.047    0.047 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/main.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.047    0.047 /Users/bean/code-oss/prefect/src/prefect/blocks/notifications.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.047    0.047 /Users/bean/code-oss/prefect/src/prefect/deprecated/packaging/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.047    0.047 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/anyio/to_process.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.047    0.047 /Users/bean/code-oss/prefect/src/prefect/server/schemas/filters.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.047    0.047 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/anyio/streams/buffered.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.046    0.046    0.046    0.046 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/anyio/streams/buffered.py:10([BufferedByteReceiveStream](http://127.0.0.1:4000/?func_name=BufferedByteReceiveStream))
   673267    0.045    0.000    0.045    0.000 {method 'get' of 'dict' objects}
        1    0.000    0.000    0.045    0.045 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/rich/console.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.045    0.045 /Users/bean/code-oss/prefect/src/prefect/logging/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.044    0.044 /Users/bean/code-oss/prefect/src/prefect/logging/loggers.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.044    0.044 /Users/bean/code-oss/prefect/src/prefect/logging/filters.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
    25873    0.043    0.000    0.043    0.000 {built-in method posix.lstat}
    18368    0.025    0.000    0.043    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:2682([__init__](http://127.0.0.1:4000/?func_name=__init__))
    26271    0.026    0.000    0.042    0.000 <frozen posixpath>:71([join](http://127.0.0.1:4000/?func_name=join))
        1    0.000    0.000    0.042    0.042 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/fsspec/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.041    0.041 /Users/bean/code-oss/prefect/src/prefect/deprecated/packaging/docker.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.041    0.041 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/httpcore/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.041    0.041 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/sql/compiler.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
       60    0.000    0.000    0.041    0.001 /Users/bean/code-oss/prefect/src/prefect/utilities/dispatch.py:91([_register_subclass_of_base_type](http://127.0.0.1:4000/?func_name=_register_subclass_of_base_type))
       46    0.000    0.000    0.041    0.001 /Users/bean/code-oss/prefect/src/prefect/utilities/dispatch.py:123([register_type](http://127.0.0.1:4000/?func_name=register_type))
    82/54    0.000    0.000    0.040    0.001 /Users/bean/code-oss/prefect/src/prefect/utilities/dispatch.py:48([get_dispatch_key](http://127.0.0.1:4000/?func_name=get_dispatch_key))
       27    0.000    0.000    0.040    0.001 /Users/bean/code-oss/prefect/src/prefect/blocks/core.py:311([__dispatch_key__](http://127.0.0.1:4000/?func_name=__dispatch_key__))
       26    0.005    0.000    0.040    0.002 /Users/bean/code-oss/prefect/src/prefect/blocks/core.py:474([_to_block_schema](http://127.0.0.1:4000/?func_name=_to_block_schema))
        1    0.000    0.000    0.040    0.040 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/jsonschema/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.040    0.040 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/core.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
    11533    0.039    0.000    0.039    0.000 {built-in method posix.stat}
        1    0.000    0.000    0.038    0.038 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/rich_utils.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
    60/57    0.000    0.000    0.038    0.001 <frozen importlib._bootstrap_external>:1231([create_module](http://127.0.0.1:4000/?func_name=create_module))
        1    0.000    0.000    0.038    0.038 /Users/bean/code-oss/prefect/src/prefect/_internal/pydantic/_flags.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
    60/57    0.034    0.001    0.038    0.001 {built-in method _imp.create_dynamic}
      631    0.005    0.000    0.037    0.000 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/dependencies/utils.py:389([analyze_param](http://127.0.0.1:4000/?func_name=analyze_param))
     2315    0.007    0.000    0.037    0.000 <frozen importlib._bootstrap>:493([_init_module_attrs](http://127.0.0.1:4000/?func_name=_init_module_attrs))
      387    0.000    0.000    0.036    0.000 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/dependencies/utils.py:510([is_body_param](http://127.0.0.1:4000/?func_name=is_body_param))
  545/539    0.001    0.000    0.036    0.000 /Users/bean/code-oss/prefect/src/prefect/_vendor/fastapi/dependencies/utils.py:249([is_scalar_field](http://127.0.0.1:4000/?func_name=is_scalar_field))
        1    0.000    0.000    0.036    0.036 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/rich/markdown.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
      486    0.004    0.000    0.036    0.000 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/utils.py:109([get_params_from_function](http://127.0.0.1:4000/?func_name=get_params_from_function))
        1    0.000    0.000    0.035    0.035 /Users/bean/code-oss/prefect/src/prefect/events/clients.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.035    0.035 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/sqlalchemy/dialects/sqlite/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
      289    0.001    0.000    0.035    0.000 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/typer/main.py:543([get_params_convertors_ctx_param_name_from_function](http://127.0.0.1:4000/?func_name=get_params_convertors_ctx_param_name_from_function))
200684/200637    0.033    0.000    0.035    0.000 {built-in method builtins.getattr}
        1    0.000    0.000    0.035    0.035 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/markdown_it/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.034    0.034 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/google/auth/transport/requests.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
    12084    0.022    0.000    0.034    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:2083([_signature_is_functionlike](http://127.0.0.1:4000/?func_name=_signature_is_functionlike))
   157903    0.027    0.000    0.034    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:283([ismodule](http://127.0.0.1:4000/?func_name=ismodule))
        1    0.000    0.000    0.034    0.034 /Users/bean/code-oss/prefect/src/prefect/deployments/base.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
      925    0.001    0.000    0.034    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/typing.py:675([Optional](http://127.0.0.1:4000/?func_name=Optional))
        1    0.000    0.000    0.034    0.034 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/markdown_it/main.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
      228    0.001    0.000    0.034    0.000 <frozen importlib._bootstrap_external>:1655([_fill_cache](http://127.0.0.1:4000/?func_name=_fill_cache))
        1    0.000    0.000    0.034    0.034 /Users/bean/code-oss/prefect/src/prefect/utilities/names.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.034    0.034 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/httpcore/_api.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     2531    0.002    0.000    0.033    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/inspect.py:961([getabsfile](http://127.0.0.1:4000/?func_name=getabsfile))
        1    0.000    0.000    0.033    0.033 /Users/bean/code-oss/prefect/src/prefect/infrastructure/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
        1    0.000    0.000    0.033    0.033 /Users/bean/code-oss/prefect/venv311/lib/python3.11/site-packages/coolname/__init__.py:1([<module>](http://127.0.0.1:4000/?func_name=%3Cmodule%3E))
     7415    0.014    0.000    0.033    0.000 /Users/bean/.pyenv/versions/3.11.1/lib/python3.11/typing.py:168([_type_check](http://127.0.0.1:4000/?func_name=_type_check))
serinamarie commented 1 month ago

I’m happy to report that the main branch (3.0) has great improvements over version 2.18.3 (released 2 weeks ago). Our goal to reduce the prefect command total time by approximately 0.75 seconds when running the prefect command looks good!

Below are comparisons showcasing the improvements: Command: prefect Version 2.18.3: 1.71s user, 0.32s system, 53% CPU, 3.802s total Version main: 1.02s user 0.38s system 47% cpu 2.969 total Command: prefect profile ls Version 2.18.3: 1.66s user, 0.28s system, 95% CPU, 2.027s total Version main: 0.93s user, 0.32s system, 78% CPU, 1.591s total Command: prefect shell Version 2.18.3: 1.69s user, 0.33s system, 87% CPU, 2.301s total Version main: 1.00s user, 0.31s system, 80% CPU, 1.628s total Command: prefect --version Version 2.18.3: 1.67s user 0.30s system 89% cpu 2.197 total Version main: 0.92s user 0.30s system 81% cpu 1.489 total

Regarding the CLI speed issue, there are always more improvements that could be made. For example, there is at least one thread I’d be interested in pulling related to some imports in infrastructure.provisioners causing some failing tests in Andrew and I’s PR from the offsite. Resolving this would unblock a further bottleneck to CLI speed (about 0.25 total seconds).

If someone was looking to improve this further, that's where I'd look next!