Skip to content

Commit 40895ae

Browse files
authored
Add python profiler (#78)
* Setup profiler * Add snakeviz * Add profiler readme * Whitespace * order
1 parent bf5d577 commit 40895ae

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

backend/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__/
44
*.onnx
55
.DS_Store
66
hmm_model/
7+
profiles/

backend/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ def analyze_sleep():
7979
allow_headers='Content-Type')
8080
app.config['CORS_HEADERS'] = 'Content-Type'
8181

82-
serve(app, host='0.0.0.0', port=8080)
82+
if __name__ == '__main__':
83+
serve(app, host='0.0.0.0', port=8080)

backend/profiler.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from waitress import serve
2+
from werkzeug.middleware.profiler import ProfilerMiddleware
3+
from pathlib import Path
4+
from os import path, makedirs
5+
import sys
6+
7+
from app import app
8+
9+
SCRIPT_PATH = Path(path.realpath(sys.argv[0])).parent
10+
PROFILES_PATH = SCRIPT_PATH / "profiles"
11+
12+
if not path.exists(PROFILES_PATH):
13+
makedirs(PROFILES_PATH)
14+
15+
app = ProfilerMiddleware(app, stream=None, profile_dir="profiles")
16+
17+
if __name__ == '__main__':
18+
serve(app, host='0.0.0.0', port=8080)

backend/readme.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ source venv/bin/activate
1717
Install the required dependencies.
1818

1919
```bash
20-
pip install -r requirements.txt requirements-dev.txt
20+
pip install -r requirements.txt -r requirements-dev.txt
2121
```
2222

2323
If you are running on Linux or MacOS, you also have to install OpenMP with your package manager. It is a dependency of ONNX runtime, used to load our model and make predictions.
@@ -40,3 +40,11 @@ If you want to run the backend with hot reload enabled (you must have installed
4040
```bash
4141
hupper -m waitress app:app
4242
```
43+
44+
## Profile application
45+
46+
- Run `python profiler.py`
47+
48+
- Send the request to the server
49+
50+
- Open the profiler result contained in `profiles` folder with `snakeviz`

backend/requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
hupper==1.10.2
22
pyinstaller==4.0
3+
snakeviz==2.1.0
4+
Werkzeug==1.0.1

0 commit comments

Comments
 (0)