Skip to content

PYTHON-5404 - Add docs for profiling execution #2402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,17 @@ partially-converted asynchronous version of the same name to the `test/asynchron
Use this generated file as a starting point for the completed conversion.

The script is used like so: `python tools/convert_test_to_async.py [test_file.py]`

## Generating a flame graph using py-spy
To profile a test script and generate a flame graph, follow these steps:
1. Install `py-spy` if you haven't already:
```bash
pip install py-spy
```
2. Inside your test script, perform any required setup and then loop over the code you want to profile for improved sampling
3. Run the `flamegraph` justfile target to generate a `.svg` file containing the flame graph:
```bash
just flamegraph <output_name=profile> <sample_rate=2000> <path/to/script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we even add this to just? Since we already need to install py-spy manually why not document the py-spy command to run without just.

```
4. Profiling should be done on a Linux system, as macOS and Windows do not support the `--native` option of `py-spy`.
Creating an ubuntu Evergreen spawn host and using `scp` to copy the flamegraph `.svg` file back to your local machine is the best way to do this.
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ setup-tests *args="":
teardown-tests:
bash .evergreen/scripts/teardown-tests.sh

flamegraph *args:
bash tools/generate_flamegraph.sh {{args}}

[group('server')]
run-server *args="":
bash .evergreen/scripts/run-server.sh {{args}}
Expand Down
4 changes: 4 additions & 0 deletions tools/generate_flamegraph.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -eu

sudo py-spy record -o ${1:-profile}.svg -r ${2:-2000} -- python $3
Loading