Skip to content

Commit 8c929be

Browse files
committed
Address review
1 parent f0f7150 commit 8c929be

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,5 +472,5 @@ To profile a test script and generate a flame graph, follow these steps:
472472
```bash
473473
just flamegraph <output_name=profile> <sample_rate=2000> <path/to/script>
474474
```
475-
4. Profiling should be done on a Linux system, as macOS and Windows do not support the `--native` option of `py-spy`.
475+
4. If you need to include native code (for example the C extensions), profiling should be done on a Linux system, as macOS and Windows do not support the `--native` option of `py-spy`.
476476
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.

tools/generate_flamegraph.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from __future__ import annotations
2+
3+
import argparse
4+
import subprocess
5+
import sys
6+
7+
8+
def main():
9+
parser = argparse.ArgumentParser(description="Generate a flamegraph of a given Python script.")
10+
11+
parser.add_argument(
12+
"--output",
13+
default="profile",
14+
help="Output filename (default: 'profile')",
15+
)
16+
parser.add_argument(
17+
"--sampling_rate",
18+
default="2000",
19+
help="Sampling rate in samples/sec (default: 2000)",
20+
)
21+
parser.add_argument(
22+
"--native",
23+
default=False,
24+
action=argparse.BooleanOptionalAction,
25+
help="Whether to profile native extensions (default: False)",
26+
)
27+
parser.add_argument(
28+
"--script_path",
29+
required=True,
30+
help="Path to the Python script to be profiled (required)",
31+
)
32+
33+
args = parser.parse_args()
34+
35+
bash_command = [
36+
"py-spy",
37+
"record",
38+
"-o",
39+
f"{args.output}.svg",
40+
"-r",
41+
f"{args.sampling_rate}",
42+
"--",
43+
"python",
44+
f"{args.script_path}",
45+
]
46+
47+
if args.native:
48+
# Insert --native option at the correct position
49+
bash_command.insert(6, "--native")
50+
51+
try:
52+
subprocess.check_call(bash_command) # noqa: S603
53+
except Exception:
54+
sys.exit(1)
55+
56+
57+
if __name__ == "__main__":
58+
main()

tools/generate_flamegraph.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)