Skip to content

Commit f0f7150

Browse files
committed
PYTHON-5404 - Add docs for profiling execution
1 parent 65f7c54 commit f0f7150

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CONTRIBUTING.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,17 @@ partially-converted asynchronous version of the same name to the `test/asynchron
460460
Use this generated file as a starting point for the completed conversion.
461461

462462
The script is used like so: `python tools/convert_test_to_async.py [test_file.py]`
463+
464+
## Generating a flame graph using py-spy
465+
To profile a test script and generate a flame graph, follow these steps:
466+
1. Install `py-spy` if you haven't already:
467+
```bash
468+
pip install py-spy
469+
```
470+
2. Inside your test script, perform any required setup and then loop over the code you want to profile for improved sampling
471+
3. Run the `flamegraph` justfile target to generate a `.svg` file containing the flame graph:
472+
```bash
473+
just flamegraph <output_name=profile> <sample_rate=2000> <path/to/script>
474+
```
475+
4. Profiling should be done on a Linux system, as macOS and Windows do not support the `--native` option of `py-spy`.
476+
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.

justfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ setup-tests *args="":
6868
teardown-tests:
6969
bash .evergreen/scripts/teardown-tests.sh
7070

71+
flamegraph *args:
72+
bash tools/generate_flamegraph.sh {{args}}
73+
7174
[group('server')]
7275
run-server *args="":
7376
bash .evergreen/scripts/run-server.sh {{args}}

tools/generate_flamegraph.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -eu
3+
4+
sudo py-spy record -o ${1:-profile}.svg -r ${2:-2000} -- python $3

0 commit comments

Comments
 (0)