From 65b960f0f0225e643fd6aa530b893bb6c462ede5 Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Thu, 23 Apr 2020 18:51:27 -0400 Subject: [PATCH] Add request profiling code To profile a given request, just add ?perfprofile to the url. Instead of returning the actual results, an HTML presentation of the profiling results is returned. This is only enabled when debug mode is enabled. --- docs/development/index.rst | 1 + docs/development/request-profiling.rst | 20 ++++++++++++++++++++ setup.py | 3 +++ src/octoprint/server/__init__.py | 14 +++++++++++++- 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 docs/development/request-profiling.rst diff --git a/docs/development/index.rst b/docs/development/index.rst index 8b3038592..d8e330da9 100644 --- a/docs/development/index.rst +++ b/docs/development/index.rst @@ -9,3 +9,4 @@ Development environment.rst virtual_printer.rst + profiling.rst diff --git a/docs/development/request-profiling.rst b/docs/development/request-profiling.rst new file mode 100644 index 000000000..a60c82a68 --- /dev/null +++ b/docs/development/request-profiling.rst @@ -0,0 +1,20 @@ +.. _sec-request-profiling: + +Profiling requests +================== + +Once you have a development environment set up, you will need to launch +OctoPrint using ``serve --debug`` as parameters. + +At this point, you are able to make the exact same requests as before. To +profile a specific request, just add ``?perfprofile`` or ``&perfprofile`` to the +request parameters. The request will be rendered as usual, but you will receive +an html document with the profiling results instead of the contents of the +response. + +Errors +------ + +If you receive a ``500: Internal Server Error`` and a ``ModuleNotFoundError: No +module named 'pyinstrument'`` in the console, you didn't install development +dependencies. Do that now using ``pip install -e '.[develop]'``. diff --git a/setup.py b/setup.py index 91245addd..202bb32c1 100644 --- a/setup.py +++ b/setup.py @@ -97,6 +97,9 @@ EXTRA_REQUIRES = dict( # linter "flake8", + # profiler + "pyinstrument", + # Documentation dependencies "sphinx>=1.8.5,<2", "sphinxcontrib-httpdomain", diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 970100d2c..f3e6104f0 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -7,7 +7,7 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms import uuid from octoprint.vendor.sockjs.tornado import SockJSRouter -from flask import Flask, g, request, session, Blueprint, Request, Response, current_app +from flask import Flask, g, request, session, Blueprint, Request, Response, current_app, make_response from flask_login import LoginManager, current_user, session_protected, user_logged_out from octoprint.vendor.flask_principal import Principal, Permission, RoleNeed, identity_loaded, identity_changed, UserNeed, Identity, AnonymousIdentity from flask_babel import Babel, gettext, ngettext @@ -1030,13 +1030,25 @@ class Server(object): @app.before_request def before_request(): g.locale = self._get_locale() + if self._debug and "perfprofile" in request.args: + from pyinstrument import Profiler + g.perfprofiler = Profiler() + g.perfprofiler.start() + @app.after_request def after_request(response): # send no-cache headers with all POST responses if request.method == "POST": response.cache_control.no_cache = True + response.headers.add("X-Clacks-Overhead", "GNU Terry Pratchett") + + if hasattr(g, "perfprofiler"): + g.perfprofiler.stop() + output_html = g.perfprofiler.output_html() + return make_response(output_html) + return response from octoprint.util.jinja import MarkdownFilter