Skip to content

Commit 0c37ee7

Browse files
committed
wagtail: profiling stats for django
1 parent 3e4a803 commit 0c37ee7

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

Benchmark/wagtail/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ env
22
**/*.sqlite3
33
cinder
44
profile_data
5-
**/prof_report.txt
5+
**/*prof_report.*

Benchmark/wagtail/profile.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
## Profiling
22

3-
Run the server with the following command:
3+
Run the server with the following command.
44

55
```bash
66
python ./manage.py runprofileserver --use-cprofile --prof-path=profile-data
77
```
88

9-
and proceed with the load testing as usual. Then run the following command to generate the profiling report in `prof_report.txt`:
9+
and proceed with the load testing as usual. Then run the following command from `testsite` to generate the profiling report in `prof_report.txt`.
1010

1111
```bash
1212
python view_profile_stats.py
1313
```
14+
15+
For a summary of total time spent inside each file of `django`, run the following command from `testsite`.
16+
17+
```bash
18+
python django_profile_stats.py
19+
```
20+
21+
This will emit a JSON file `django_prof_report.json`.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pstats
2+
import os
3+
import io
4+
import json
5+
6+
7+
data = {} # {filename: time}
8+
9+
# loop through all files in 'profile_data' directory
10+
for file in os.listdir("profile_data"):
11+
if file.endswith(".prof"):
12+
# print the file name
13+
print(file)
14+
buf = io.StringIO()
15+
# create a new instance of the Stats class
16+
s = pstats.Stats("profile_data/" + file, stream=buf)
17+
# sort the stats by time and print all but first 8 lines
18+
s.sort_stats("time").print_stats(20)
19+
20+
lines = buf.getvalue().split("\n")[8:]
21+
22+
# only retain lines with site-packages/django
23+
lines = filter(lambda x: "site-packages/django" in x, lines)
24+
25+
# for each line, extract the filename (element 5) and time (element 1)
26+
for line in lines:
27+
line = line.split()
28+
filename = line[5].split("site-packages/django/", 2)[1].split(":")[0]
29+
time = float(line[1])
30+
if filename in data:
31+
data[filename] += time
32+
else:
33+
data[filename] = time
34+
35+
# sort the data by time
36+
data = dict(sorted(data.items(), key=lambda item: item[1], reverse=True))
37+
38+
with open("django_prof_report.json", "w") as f:
39+
f.write(json.dumps(data, indent=4))

0 commit comments

Comments
 (0)