diff --git a/backend/.gitignore b/backend/.gitignore index 552623c5..dccba242 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -4,3 +4,4 @@ __pycache__/ *.onnx .DS_Store hmm_model/ +profiles/ diff --git a/backend/app.py b/backend/app.py index 7389aadf..216b71f3 100644 --- a/backend/app.py +++ b/backend/app.py @@ -79,4 +79,5 @@ def analyze_sleep(): allow_headers='Content-Type') app.config['CORS_HEADERS'] = 'Content-Type' -serve(app, host='0.0.0.0', port=8080) +if __name__ == '__main__': + serve(app, host='0.0.0.0', port=8080) diff --git a/backend/profiler.py b/backend/profiler.py new file mode 100644 index 00000000..3ec11018 --- /dev/null +++ b/backend/profiler.py @@ -0,0 +1,18 @@ +from waitress import serve +from werkzeug.middleware.profiler import ProfilerMiddleware +from pathlib import Path +from os import path, makedirs +import sys + +from app import app + +SCRIPT_PATH = Path(path.realpath(sys.argv[0])).parent +PROFILES_PATH = SCRIPT_PATH / "profiles" + +if not path.exists(PROFILES_PATH): + makedirs(PROFILES_PATH) + +app = ProfilerMiddleware(app, stream=None, profile_dir="profiles") + +if __name__ == '__main__': + serve(app, host='0.0.0.0', port=8080) diff --git a/backend/readme.md b/backend/readme.md index b7d97173..e0e942d2 100644 --- a/backend/readme.md +++ b/backend/readme.md @@ -17,7 +17,7 @@ source venv/bin/activate Install the required dependencies. ```bash -pip install -r requirements.txt requirements-dev.txt +pip install -r requirements.txt -r requirements-dev.txt ``` 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 ```bash hupper -m waitress app:app ``` + +## Profile application + +- Run `python profiler.py` + +- Send the request to the server + +- Open the profiler result contained in `profiles` folder with `snakeviz` diff --git a/backend/requirements-dev.txt b/backend/requirements-dev.txt index 3e77229d..002c9813 100644 --- a/backend/requirements-dev.txt +++ b/backend/requirements-dev.txt @@ -1,2 +1,4 @@ hupper==1.10.2 pyinstaller==4.0 +snakeviz==2.1.0 +Werkzeug==1.0.1