Introduction
============
The ``ox_profile`` package provides a python framework for statistical
profiling. If you are using ``Flask``, then ``ox_profile`` provides a
flask blueprint so that you can start/stop/analyze profiling from within
your application. You can also run the profiler stand-alone without
``Flask`` as well.
To learn more, you can browse the overview slides in `PDF
form <https://github.com/emin63/ox_profile/blob/master/docs/overview_slides.pdf>`__
or read more details below.
Why statistical profiling (and why ox\_profile)?
================================================
Python contains many profilers which instrument your code and give you
exact results. A main benefit here is you know *exactly* what your
program is doing. The disadvantage is that there can be significant
overhead. With a statistical profiler such as ``ox_profile``, we sample
a running program periodically to get a sense of what the program is
doing with an overhead that can be tuned as desired.
One main use case for ``ox_profile`` specifically (and statistical
profiling in general) is that you can apply it to a production server to
see how things work "in the wild".
There are other statistical profilers out there for python (such as
statprof), which are pretty good and may be better for your needs than
``ox_profile``. So why would you consider ``ox_profile``? Some possible
reasons include:
1. Works on non-UNIX systems (e.g., works on Windows).
- Many other statistical profilers use various excellent features of
LINUX or UNIX while ``ox_profile`` only really relies on the
python ``sys._current_frames`` method.
2. Simple to understand.
- The code for ``ox_profile`` is fairly simple. The main work is
really inside ``ox_profile.core.sampling.Sampler`` so it is easy
to reason about or modify if you need slightly different
profiling.
3. Flask Blueprint provided.
- If you are using Flask, then you can register the ``ox_profile``
blueprint and easily get statistical profiling in your flask app.
Usage
=====
Stand alone
-----------
You can install ``ox_profile`` using pip via something like
.. code:: sh
$ pip install ox_profile
The simplest way to run the profiler is by starting the launcher,
calling some functions, and the printing the profiled data via something
like:
::
>>> from ox_profile.core.launchers import SimpleLauncher
>>> profiler = SimpleLauncher.launch() # Create and start a profiler.
>>> # call some functions
>>> print(profiler.show()) # Print current results in preformated way.
>>> profiler.cancel() # Turns off the profiler for good.
Often you may want a slightly more sophisticated use case where you can
pause and unpause the profiler and get more details about its status as
shown below:
::
>>> from ox_profile.core import launchers
>>> profiler = launchers.SimpleLauncher() # Create an instance of launcher to be your profiler
>>> profiler.start() # The profiler is a thread so we need to call start
>>> profiler.unpause() # The profiler starts out paused so we unpause it
>>> # call functions or start main program
>>> profiler.pause() # You can pause if done profiling or leave running
>>> query, total_records = profiler.query() # Query for what the profiler has found
>>> info = ['%s: %s' % (i.name, i.hits) for i in query]
>>> print('Items in query:\n - %s' % (('\n - '.join(info))))
>>> profiler.cancel() # This turns off the profiler for good
With Flask
----------
If you are using the python flask framework and have installed
``ox_profile`` (e.g., with ``pip install ox_profile``) then you can
simply do the following in the appropriate place after initializing your
app:
::
from ox_profile.ui.flask.views import OX_PROF_BP
app.register_blueprint(OX_PROF_BP)
app.config['OX_PROF_USERS'] = {<admin_user_1>, <admin_user_2>, ...}
where ``<admin_user_>``, etc. are strings referring to users who are
allowed to access ``ox_profile``. By default, we check
``current_user.name``, but you can set the ``OX_PROF_USERNAME_FIELD`` in
``app.config`` to something else (e.g., ``'email'`` to choose which
field of ``current_user`` is checked against the sequence of strings in
``OX_PROF_USERS``.
Pointing your browser to the route ``/ox_profile/status`` will then show
you the profiling status. By default, ``ox_profile`` starts out paused
so that it will not incur any overhead for your app. Go to the
``/ox_profile/unpause`` route to unpause and begin profiling so that
``/ox_profile/status`` shows something interesting.
Output
======
Currently ``ox_profile`` is in alpha mode and so the output is fairly
bare bones. When you look at the results of calling the ``query`` method
of an instance of ``SimpleLauncher`` in stand alone mode or at the
``/ox_profile/status`` route when running with flask, what you get is a
raw list of each function your program has called along with how many
times that function was called in our sampling.
Design
======
High Level Design
-----------------
Python offers a number of ways to get profiling information. In addition
to high-level profiling tools such as in the ``profile`` package, there
are specialized functions like ``sys.settrace`` and ``sys.setprofile``.
These are used for deterministic profiling and relatively robust but
have some overhead as they are invoked on each function call.
At a high level, we want a way to get a sample of what the python
interpreter is doing at any give instance. The sampling approach has the
advantage that by turning the sampling interval low enough, we can add
arbitrarily low overhead and make profiling feasible in a production
system. By taking a long enough sample, however, we should be able to
get arbitrarily accurate profiling information.
Low Level Design
----------------
At a low level, we do this sampling using ``sys._current_frames``. As
suggested by the leading underscore, this system function may be a bit
less robust. Indeed, the documentation says "This function should be
used for specialized purposes only." Hopefully the core python
developers will not make major changes to such a useful function.
In any case, the most interesting class is the ``Sampler`` class in the
``ox_profile.core.sampling`` module. This class has a run method which
does the following:
1. Uses ``sys.setswitchinterval`` to try and prevent a thread context
switch.
2. Calls ``sys._current_frames`` to sample what the python interpreter
is doing.
3. Updates a simple in-memory database of what functions are running.
In principle, you could just use the Sampler via something like
::
>>> from ox_profile.core import sampling, recording
>>> sampler = sampling.Sampler(recording.CountingRecorder())
>>> def foo():
... sampler.run()
... return 'done'
...
>>> foo()
The above would have the sampler take a snapshot of the stack frames
when the ``foo`` function is run. Of course, this isn't very useful by
itself because it just tells you that ``foo`` is being run. It could be
useful if there were other threads which were running because the
sampler would tell you what stack frame those threads were in.
In principle, you could just call the ``Sampler.run`` method to track
other threads but that still isn't very convenient. To make things easy
to use, we provide the ``SimpleLauncher`` class in the
``ox_profile.core.launchers`` module as shown in the Usage section. The
``SimpleLauncher`` basically does the following:
1. Creates an instance of the ``Sampler`` class with reasonable
defaults.
2. Initializes itself as a daemon thread and starts.
3. Pauses itself so the thread does nothing so as to not load the
system.
4. Provides an ``unpause`` method you can use when you want to turn on
profiling.
5. Provides a ``pause`` method if you want to turn off profiling.
In principle, you don't need much beyond the ``Sampler`` but the
``SimpleLauncher`` makes it easier to launch a ``Sampler`` in a separate
thread.
Known Issues
============
Granularity
-----------
With statistical profiling, we need to ask the thread to sleep for some
small amount so that it does not overuse CPU resources. Sadly, the
minimum sleep time (using either ``time.sleep`` or ``wait`` on a thread
event) is on the order of 1--10 milliseconds on most operating systems.
This means that you can not efficiently do statistical profiling at a
granularity finer than about 1 millisecond.
Thus you should consider statistical profiling as a tool to find the
relatively slow issues in production and not a tool for optimizing
issues faster than about a millisecond.
Raw data
{
"_id": null,
"home_page": "http://github.com/emin63/ox_profile",
"name": "ox-profile",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "statistical profiling",
"author": "Emin Martinian",
"author_email": "emin.martinian@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/58/4c/f9003cc69dc463217b47ec93c7bcc4071ffb46caf30cc7699848461cdf54/ox_profile-0.2.7.tar.gz",
"platform": null,
"description": "Introduction\n============\n\nThe ``ox_profile`` package provides a python framework for statistical\nprofiling. If you are using ``Flask``, then ``ox_profile`` provides a\nflask blueprint so that you can start/stop/analyze profiling from within\nyour application. You can also run the profiler stand-alone without\n``Flask`` as well.\n\nTo learn more, you can browse the overview slides in `PDF\nform <https://github.com/emin63/ox_profile/blob/master/docs/overview_slides.pdf>`__\nor read more details below.\n\nWhy statistical profiling (and why ox\\_profile)?\n================================================\n\nPython contains many profilers which instrument your code and give you\nexact results. A main benefit here is you know *exactly* what your\nprogram is doing. The disadvantage is that there can be significant\noverhead. With a statistical profiler such as ``ox_profile``, we sample\na running program periodically to get a sense of what the program is\ndoing with an overhead that can be tuned as desired.\n\nOne main use case for ``ox_profile`` specifically (and statistical\nprofiling in general) is that you can apply it to a production server to\nsee how things work \"in the wild\".\n\nThere are other statistical profilers out there for python (such as\nstatprof), which are pretty good and may be better for your needs than\n``ox_profile``. So why would you consider ``ox_profile``? Some possible\nreasons include:\n\n1. Works on non-UNIX systems (e.g., works on Windows).\n\n - Many other statistical profilers use various excellent features of\n LINUX or UNIX while ``ox_profile`` only really relies on the\n python ``sys._current_frames`` method.\n\n2. Simple to understand.\n\n - The code for ``ox_profile`` is fairly simple. The main work is\n really inside ``ox_profile.core.sampling.Sampler`` so it is easy\n to reason about or modify if you need slightly different\n profiling.\n\n3. Flask Blueprint provided.\n\n - If you are using Flask, then you can register the ``ox_profile``\n blueprint and easily get statistical profiling in your flask app.\n\nUsage\n=====\n\nStand alone\n-----------\n\nYou can install ``ox_profile`` using pip via something like\n\n.. code:: sh\n\n $ pip install ox_profile\n\nThe simplest way to run the profiler is by starting the launcher,\ncalling some functions, and the printing the profiled data via something\nlike:\n\n::\n\n >>> from ox_profile.core.launchers import SimpleLauncher\n >>> profiler = SimpleLauncher.launch() # Create and start a profiler.\n >>> # call some functions\n >>> print(profiler.show()) # Print current results in preformated way.\n >>> profiler.cancel() # Turns off the profiler for good.\n\nOften you may want a slightly more sophisticated use case where you can\npause and unpause the profiler and get more details about its status as\nshown below:\n\n::\n\n >>> from ox_profile.core import launchers\n >>> profiler = launchers.SimpleLauncher() # Create an instance of launcher to be your profiler\n >>> profiler.start() # The profiler is a thread so we need to call start\n >>> profiler.unpause() # The profiler starts out paused so we unpause it\n >>> # call functions or start main program\n >>> profiler.pause() # You can pause if done profiling or leave running\n >>> query, total_records = profiler.query() # Query for what the profiler has found\n >>> info = ['%s: %s' % (i.name, i.hits) for i in query]\n >>> print('Items in query:\\n - %s' % (('\\n - '.join(info))))\n >>> profiler.cancel() # This turns off the profiler for good\n\nWith Flask\n----------\n\nIf you are using the python flask framework and have installed\n``ox_profile`` (e.g., with ``pip install ox_profile``) then you can\nsimply do the following in the appropriate place after initializing your\napp:\n\n::\n\n from ox_profile.ui.flask.views import OX_PROF_BP\n app.register_blueprint(OX_PROF_BP)\n app.config['OX_PROF_USERS'] = {<admin_user_1>, <admin_user_2>, ...}\n\nwhere ``<admin_user_>``, etc. are strings referring to users who are\nallowed to access ``ox_profile``. By default, we check\n``current_user.name``, but you can set the ``OX_PROF_USERNAME_FIELD`` in\n``app.config`` to something else (e.g., ``'email'`` to choose which\nfield of ``current_user`` is checked against the sequence of strings in\n``OX_PROF_USERS``.\n\nPointing your browser to the route ``/ox_profile/status`` will then show\nyou the profiling status. By default, ``ox_profile`` starts out paused\nso that it will not incur any overhead for your app. Go to the\n``/ox_profile/unpause`` route to unpause and begin profiling so that\n``/ox_profile/status`` shows something interesting.\n\nOutput\n======\n\nCurrently ``ox_profile`` is in alpha mode and so the output is fairly\nbare bones. When you look at the results of calling the ``query`` method\nof an instance of ``SimpleLauncher`` in stand alone mode or at the\n``/ox_profile/status`` route when running with flask, what you get is a\nraw list of each function your program has called along with how many\ntimes that function was called in our sampling.\n\nDesign\n======\n\nHigh Level Design\n-----------------\n\nPython offers a number of ways to get profiling information. In addition\nto high-level profiling tools such as in the ``profile`` package, there\nare specialized functions like ``sys.settrace`` and ``sys.setprofile``.\nThese are used for deterministic profiling and relatively robust but\nhave some overhead as they are invoked on each function call.\n\nAt a high level, we want a way to get a sample of what the python\ninterpreter is doing at any give instance. The sampling approach has the\nadvantage that by turning the sampling interval low enough, we can add\narbitrarily low overhead and make profiling feasible in a production\nsystem. By taking a long enough sample, however, we should be able to\nget arbitrarily accurate profiling information.\n\nLow Level Design\n----------------\n\nAt a low level, we do this sampling using ``sys._current_frames``. As\nsuggested by the leading underscore, this system function may be a bit\nless robust. Indeed, the documentation says \"This function should be\nused for specialized purposes only.\" Hopefully the core python\ndevelopers will not make major changes to such a useful function.\n\nIn any case, the most interesting class is the ``Sampler`` class in the\n``ox_profile.core.sampling`` module. This class has a run method which\ndoes the following:\n\n1. Uses ``sys.setswitchinterval`` to try and prevent a thread context\n switch.\n2. Calls ``sys._current_frames`` to sample what the python interpreter\n is doing.\n3. Updates a simple in-memory database of what functions are running.\n\nIn principle, you could just use the Sampler via something like\n\n::\n\n >>> from ox_profile.core import sampling, recording\n >>> sampler = sampling.Sampler(recording.CountingRecorder())\n >>> def foo():\n ... sampler.run()\n ... return 'done'\n ... \n >>> foo()\n\nThe above would have the sampler take a snapshot of the stack frames\nwhen the ``foo`` function is run. Of course, this isn't very useful by\nitself because it just tells you that ``foo`` is being run. It could be\nuseful if there were other threads which were running because the\nsampler would tell you what stack frame those threads were in.\n\nIn principle, you could just call the ``Sampler.run`` method to track\nother threads but that still isn't very convenient. To make things easy\nto use, we provide the ``SimpleLauncher`` class in the\n``ox_profile.core.launchers`` module as shown in the Usage section. The\n``SimpleLauncher`` basically does the following:\n\n1. Creates an instance of the ``Sampler`` class with reasonable\n defaults.\n2. Initializes itself as a daemon thread and starts.\n3. Pauses itself so the thread does nothing so as to not load the\n system.\n4. Provides an ``unpause`` method you can use when you want to turn on\n profiling.\n5. Provides a ``pause`` method if you want to turn off profiling.\n\nIn principle, you don't need much beyond the ``Sampler`` but the\n``SimpleLauncher`` makes it easier to launch a ``Sampler`` in a separate\nthread.\n\nKnown Issues\n============\n\nGranularity\n-----------\n\nWith statistical profiling, we need to ask the thread to sleep for some\nsmall amount so that it does not overuse CPU resources. Sadly, the\nminimum sleep time (using either ``time.sleep`` or ``wait`` on a thread\nevent) is on the order of 1--10 milliseconds on most operating systems.\nThis means that you can not efficiently do statistical profiling at a\ngranularity finer than about 1 millisecond.\n\nThus you should consider statistical profiling as a tool to find the\nrelatively slow issues in production and not a tool for optimizing\nissues faster than about a millisecond.",
"bugtrack_url": null,
"license": "MIT",
"summary": "Tools for statistical profiling.",
"version": "0.2.7",
"project_urls": {
"Homepage": "http://github.com/emin63/ox_profile"
},
"split_keywords": [
"statistical",
"profiling"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "584cf9003cc69dc463217b47ec93c7bcc4071ffb46caf30cc7699848461cdf54",
"md5": "9f94c10da3b6eaf86a53d74586c103e6",
"sha256": "f30f031369b2bc10a932cfa79d867df634504da4371c4db65cca3b6745fa0f8c"
},
"downloads": -1,
"filename": "ox_profile-0.2.7.tar.gz",
"has_sig": false,
"md5_digest": "9f94c10da3b6eaf86a53d74586c103e6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 20875,
"upload_time": "2024-06-05T17:34:47",
"upload_time_iso_8601": "2024-06-05T17:34:47.953677Z",
"url": "https://files.pythonhosted.org/packages/58/4c/f9003cc69dc463217b47ec93c7bcc4071ffb46caf30cc7699848461cdf54/ox_profile-0.2.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-05 17:34:47",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "emin63",
"github_project": "ox_profile",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "ox-profile"
}