Skip to content

Commit 0aba14f

Browse files
committed
Update README
1 parent 0cb4010 commit 0aba14f

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

python-312/README.md

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ You need Python 3.12 installed to run these examples. See the following tutorial
88

99
- [How Can You Install a Pre-Release Version of Python](https://realpython.com/python-pre-release/)
1010

11+
Note that for the `perf` support, you'll need to build Python from source code with additional compiler flags enabled, as [explained below](#support-for-the-linux-perf-profiler).
12+
1113
You can learn more about Python 3.12's new features in the following Real Python tutorials:
1214

1315
- [Python 3.12 Preview: Ever Better Error Messages](https://realpython.com/python312-error-messages/)
16+
- [Python 3.12 Preview: Support For the Linux `perf` Profiler](https://realpython.com/python312-perf-profiler/)
1417

1518
You'll find examples from all these tutorials in this repository.
1619

@@ -20,7 +23,7 @@ This section only contains brief instructions on how you can run the examples. S
2023

2124
### Improved Error Messages
2225

23-
Run [`encoder.py](encoder.py) to create an encoded message like the one shown in the tutorial. You can decode the message using [`decoder.py](decoder.py).
26+
Run [`encoder.py`](error-messages/encoder.py) to create an encoded message like the one shown in the tutorial. You can decode the message using [`decoder.py`](error-messages/decoder.py).
2427

2528
You can swap the import statement to `import d from this` in either of the files to encounter the improved error message:
2629

@@ -32,14 +35,93 @@ You can swap the import statement to `import d from this` in either of the files
3235
SyntaxError: Did you mean to use 'from ... import ...' instead?
3336
```
3437

35-
In [`local_self.py`](local_self.py), you can see a naive reproduction of another improved error message. Pick apart the example code to learn more about how this was implemented in Python 3.12.
38+
In [`local_self.py`](error-messages/local_self.py), you can see a naive reproduction of another improved error message. Pick apart the example code to learn more about how this was implemented in Python 3.12.
3639

3740
See [Ever Better Error Messages in Python 3.12](https://realpython.com/python312-error-messages/) for more information.
3841

42+
### Support For the Linux `perf` Profiler
43+
44+
#### Setting Up
45+
46+
You'll need to download, build, and install Python 3.12 from the source code with the frame pointer optimizations disabled:
47+
48+
```shell
49+
$ git clone --branch v3.12.0b2 https://github.com/python/cpython.git
50+
$ cd cpython/
51+
$ export CFLAGS='-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer'
52+
$ ./configure --prefix="$HOME/python-custom-build"
53+
$ make -j $(nproc)
54+
$ make install
55+
```
56+
57+
Create and activate a new virtual environment based on Python 3.12:
58+
59+
```shell
60+
$ "$HOME/python-custom-build/bin/python3" -m venv venv/ --prompt 'py3.12-custom'
61+
$ source venv/bin/activate
62+
```
63+
64+
Install dependencies from the `requirements.txt` file into your virtual environment:
65+
66+
```shell
67+
(py3.12-custom) $ python -m pip install -r requirements.txt
68+
```
69+
70+
#### Using `perf` With Python
71+
72+
Record Samples:
73+
74+
```shell
75+
$ cd perf-profiler/
76+
$ sudo perf record -g -F max ../venv/bin/python -X perf benchmark.py
77+
```
78+
79+
Display reports:
80+
81+
```shell
82+
$ cd perf-profiler/
83+
$ sudo perf report
84+
$ sudo perf report --stdio -g
85+
$ sudo perf report --hierarchy --verbose --call-graph fractal --sort sample,dso
86+
```
87+
88+
#### Rendering Flame Graphs
89+
90+
Download Perl scripts and add them to your `$PATH` environment variable:
91+
92+
```shell
93+
$ git clone [email protected]:brendangregg/FlameGraph.git
94+
$ export PATH="$(pwd)/FlameGraph:$PATH"
95+
```
96+
97+
Generate the flame graph and save it to a local file:
98+
99+
```shell
100+
$ cd perf-profiler/
101+
$ sudo perf script | stackcollapse-perf.pl | flamegraph.pl > flamegraph.svg
102+
```
103+
104+
Open the flame graph in your default SVG viewer: (Use your web browser for the interactive features.)
105+
106+
```shell
107+
$ xdg-open flamegraph.svg
108+
```
109+
110+
Produce a pure-Python flame graph by filtering and processing the collapsed stack traces:
111+
112+
```shell
113+
$ sudo perf script | stackcollapse-perf.pl > traces.txt
114+
$ cat traces.txt | python censor.py -m benchmark,PIL > traces_censored.txt
115+
$ cat traces_censored.txt | flamegraph.pl --minwidth 10 > flamegraph.svg
116+
```
117+
118+
See [Support For the Linux `perf` Profiler in Python 3.12](https://realpython.com/python312-perf-profiler/) for more information.
119+
39120
## Authors
40121

41122
- **Martin Breuss**, E-mail: [[email protected]]([email protected])
42123
- **Bartosz Zaczyński**, E-mail: [[email protected]]([email protected])
124+
- **Leodanis Pozo Ramos**, E-mail: [[email protected]]([email protected])
43125
- **Geir Arne Hjelle**, E-mail: [[email protected]]([email protected])
44126

45127
## License

0 commit comments

Comments
 (0)