.. contents::
Line-granularity, thread-aware deterministic and statistic pure-python profiler
Inspired from Robert Kern's line_profiler_ .
Usage
=====
As a command::
$ pprofile some_python_executable arg1 ...
Once `some_python_executable` returns, prints annotated code of each file
involved in the execution.
As a command, ignoring any files from default `sys.path` (ie, python modules
themselves), for shorter output::
$ pprofile --exclude-syspath some_python_executable arg1 ...
Executing a module, like :code:`python -m`. `--exclude-syspath` is not
recommended in this mode, as it will likely hide what you intend to profile.
Also, explicitly ending pprofile arguments with `--` will prevent accidentally
stealing command's arguments::
$ pprofile -m some_python_module -- arg1 ...
As a module:
.. code:: python
import pprofile
def someHotSpotCallable():
# Deterministic profiler
prof = pprofile.Profile()
with prof():
# Code to profile
prof.print_stats()
def someOtherHotSpotCallable():
# Statistic profiler
prof = pprofile.StatisticalProfile()
with prof(
period=0.001, # Sample every 1ms
single=True, # Only sample current thread
):
# Code to profile
prof.print_stats()
For advanced usage, see :code:`pprofile --help` and :code:`pydoc pprofile`.
Profiling overhead
------------------
pprofile default mode (`Deterministic profiling`_) has a large overhead.
Part of the reason being that it is written to be as portable as possible
(so no C extension). This large overhead can be an issue, which can be
avoided by using `Statistic profiling`_ at the cost of some result
readability decrease.
Rule of thumb:
+-----------------------------+----------------------------+------------------------+
| Code to profile runs for... | `Deterministic profiling`_ | `Statistic profiling`_ |
+=============================+============================+========================+
| a few seconds | Yes | No [#]_ |
+-----------------------------+----------------------------+------------------------+
| a few minutes | Maybe | Yes |
+-----------------------------+----------------------------+------------------------+
| more (ex: daemon) | No | Yes [#]_ |
+-----------------------------+----------------------------+------------------------+
Once you identified the hot spot and you decide you need finer-grained
profiling to understand what needs fixing, you should try to make to-profile
code run for shorter time so you can reasonably use deterministic profiling:
use a smaller data set triggering the same code path, modify the code to only
enable profiling on small pieces of code...
.. [#] Statistic profiling will not have time to collect
enough samples to produce usable output.
.. [#] You may want to consider triggering pprofile from
a signal handler or other IPC mechanism to profile
a shorter subset. See `zpprofile.py` for how it can
be used to profile code inside a running (zope)
service (in which case the IPC mechanism is just
Zope normal URL handling).
Output
======
Supported output formats.
Callgrind
---------
The most useful output mode of pprofile is `Callgrind Profile Format`_, allows
browsing profiling results with kcachegrind_ (or qcachegrind_ on Windows).
::
$ pprofile --format callgrind --out cachegrind.out.threads demo/threads.py
Callgrind format is implicitly enabled if ``--out`` basename starts with
``cachegrind.out.``, so above command can be simplified as::
$ pprofile --out cachegrind.out.threads demo/threads.py
If you are analyzing callgrind traces on a different machine, you may want to
use the ``--zipfile`` option to generate a zip file containing all files::
$ pprofile --out cachegrind.out.threads --zipfile threads_source.zip demo/threads.py
Generated files will use relative paths, so you can extract generated archive
in the same path as profiling result, and kcachegrind will load them - and not
your system-wide files, which may differ.
Annotated code
--------------
Human-readable output, but can become difficult to use with large programs.
::
$ pprofile demo/threads.py
Profiling modes
===============
Deterministic profiling
-----------------------
In deterministic profiling mode, pprofile gets notified of each executed line.
This mode generates very detailed reports, but at the cost of a large overhead.
Also, profiling hooks being per-thread, either profiling must be enable before
spawning threads (if you want to profile more than just the current thread),
or profiled application must provide ways of enabling profiling afterwards
- which is not very convenient.
::
$ pprofile --threads 0 demo/threads.py
Command line: ['demo/threads.py']
Total duration: 1.00573s
File: demo/threads.py
File duration: 1.00168s (99.60%)
Line #| Hits| Time| Time per hit| %|Source code
------+----------+-------------+-------------+-------+-----------
1| 2| 3.21865e-05| 1.60933e-05| 0.00%|import threading
2| 1| 5.96046e-06| 5.96046e-06| 0.00%|import time
3| 0| 0| 0| 0.00%|
4| 2| 1.5974e-05| 7.98702e-06| 0.00%|def func():
5| 1| 1.00111| 1.00111| 99.54%| time.sleep(1)
6| 0| 0| 0| 0.00%|
7| 2| 2.00272e-05| 1.00136e-05| 0.00%|def func2():
8| 1| 1.69277e-05| 1.69277e-05| 0.00%| pass
9| 0| 0| 0| 0.00%|
10| 1| 1.81198e-05| 1.81198e-05| 0.00%|t1 = threading.Thread(target=func)
(call)| 1| 0.000610828| 0.000610828| 0.06%|# /usr/lib/python2.7/threading.py:436 __init__
11| 1| 1.52588e-05| 1.52588e-05| 0.00%|t2 = threading.Thread(target=func)
(call)| 1| 0.000438929| 0.000438929| 0.04%|# /usr/lib/python2.7/threading.py:436 __init__
12| 1| 4.79221e-05| 4.79221e-05| 0.00%|t1.start()
(call)| 1| 0.000843048| 0.000843048| 0.08%|# /usr/lib/python2.7/threading.py:485 start
13| 1| 6.48499e-05| 6.48499e-05| 0.01%|t2.start()
(call)| 1| 0.00115609| 0.00115609| 0.11%|# /usr/lib/python2.7/threading.py:485 start
14| 1| 0.000205994| 0.000205994| 0.02%|(func(), func2())
(call)| 1| 1.00112| 1.00112| 99.54%|# demo/threads.py:4 func
(call)| 1| 3.09944e-05| 3.09944e-05| 0.00%|# demo/threads.py:7 func2
15| 1| 7.62939e-05| 7.62939e-05| 0.01%|t1.join()
(call)| 1| 0.000423908| 0.000423908| 0.04%|# /usr/lib/python2.7/threading.py:653 join
16| 1| 5.26905e-05| 5.26905e-05| 0.01%|t2.join()
(call)| 1| 0.000320196| 0.000320196| 0.03%|# /usr/lib/python2.7/threading.py:653 join
Note that time.sleep call is not counted as such. For some reason, python is
not generating c_call/c_return/c_exception events (which are ignored by current
code, as a result).
Statistic profiling
-------------------
In statistic profiling mode, pprofile periodically snapshots the current
callstack(s) of current process to see what is being executed.
As a result, profiler overhead can be dramatically reduced, making it possible
to profile real workloads. Also, as statistic profiling acts at the
whole-process level, it can be toggled independently of profiled code.
The downside of statistic profiling is that output lacks timing information,
which makes it harder to understand.
::
$ pprofile --statistic .01 demo/threads.py
Command line: ['demo/threads.py']
Total duration: 1.0026s
File: demo/threads.py
File duration: 0s (0.00%)
Line #| Hits| Time| Time per hit| %|Source code
------+----------+-------------+-------------+-------+-----------
1| 0| 0| 0| 0.00%|import threading
2| 0| 0| 0| 0.00%|import time
3| 0| 0| 0| 0.00%|
4| 0| 0| 0| 0.00%|def func():
5| 288| 0| 0| 0.00%| time.sleep(1)
6| 0| 0| 0| 0.00%|
7| 0| 0| 0| 0.00%|def func2():
8| 0| 0| 0| 0.00%| pass
9| 0| 0| 0| 0.00%|
10| 0| 0| 0| 0.00%|t1 = threading.Thread(target=func)
11| 0| 0| 0| 0.00%|t2 = threading.Thread(target=func)
12| 0| 0| 0| 0.00%|t1.start()
13| 0| 0| 0| 0.00%|t2.start()
14| 0| 0| 0| 0.00%|(func(), func2())
(call)| 96| 0| 0| 0.00%|# demo/threads.py:4 func
15| 0| 0| 0| 0.00%|t1.join()
16| 0| 0| 0| 0.00%|t2.join()
File: /usr/lib/python2.7/threading.py
File duration: 0s (0.00%)
Line #| Hits| Time| Time per hit| %|Source code
------+----------+-------------+-------------+-------+-----------
[...]
308| 0| 0| 0| 0.00%| def wait(self, timeout=None):
[...]
338| 0| 0| 0| 0.00%| if timeout is None:
339| 1| 0| 0| 0.00%| waiter.acquire()
340| 0| 0| 0| 0.00%| if __debug__:
[...]
600| 0| 0| 0| 0.00%| def wait(self, timeout=None):
[...]
617| 0| 0| 0| 0.00%| if not self.__flag:
618| 0| 0| 0| 0.00%| self.__cond.wait(timeout)
(call)| 1| 0| 0| 0.00%|# /usr/lib/python2.7/threading.py:308 wait
[...]
724| 0| 0| 0| 0.00%| def start(self):
[...]
748| 0| 0| 0| 0.00%| self.__started.wait()
(call)| 1| 0| 0| 0.00%|# /usr/lib/python2.7/threading.py:600 wait
749| 0| 0| 0| 0.00%|
750| 0| 0| 0| 0.00%| def run(self):
[...]
760| 0| 0| 0| 0.00%| if self.__target:
761| 0| 0| 0| 0.00%| self.__target(*self.__args, **self.__kwargs)
(call)| 192| 0| 0| 0.00%|# demo/threads.py:4 func
762| 0| 0| 0| 0.00%| finally:
[...]
767| 0| 0| 0| 0.00%| def __bootstrap(self):
[...]
780| 0| 0| 0| 0.00%| try:
781| 0| 0| 0| 0.00%| self.__bootstrap_inner()
(call)| 192| 0| 0| 0.00%|# /usr/lib/python2.7/threading.py:790 __bootstrap_inner
[...]
790| 0| 0| 0| 0.00%| def __bootstrap_inner(self):
[...]
807| 0| 0| 0| 0.00%| try:
808| 0| 0| 0| 0.00%| self.run()
(call)| 192| 0| 0| 0.00%|# /usr/lib/python2.7/threading.py:750 run
Some details are lost (not all executed lines have a non-null hit-count), but
the hot spot is still easily identifiable in this trivial example, and its call
stack is still visible.
Thread-aware profiling
======================
``ThreadProfile`` class provides the same features as ``Profile``, but uses
``threading.settrace`` to propagate tracing to ``threading.Thread`` threads
started after profiling is enabled.
Limitations
-----------
The time spent in another thread is not discounted from interrupted line.
On the long run, it should not be a problem if switches are evenly distributed
among lines, but threads executing fewer lines will appear as eating more CPU
time than they really do.
This is not specific to simultaneous multi-thread profiling: profiling a single
thread of a multi-threaded application will also be polluted by time spent in
other threads.
Example (lines are reported as taking longer to execute when profiled along
with another thread - although the other thread is not profiled)::
$ demo/embedded.py
Total duration: 1.00013s
File: demo/embedded.py
File duration: 1.00003s (99.99%)
Line #| Hits| Time| Time per hit| %|Source code
------+----------+-------------+-------------+-------+-----------
1| 0| 0| 0| 0.00%|#!/usr/bin/env python
2| 0| 0| 0| 0.00%|import threading
3| 0| 0| 0| 0.00%|import pprofile
4| 0| 0| 0| 0.00%|import time
5| 0| 0| 0| 0.00%|import sys
6| 0| 0| 0| 0.00%|
7| 1| 1.5974e-05| 1.5974e-05| 0.00%|def func():
8| 0| 0| 0| 0.00%| # Busy loop, so context switches happen
9| 1| 1.40667e-05| 1.40667e-05| 0.00%| end = time.time() + 1
10| 146604| 0.511392| 3.48826e-06| 51.13%| while time.time() < end:
11| 146603| 0.48861| 3.33288e-06| 48.85%| pass
12| 0| 0| 0| 0.00%|
13| 0| 0| 0| 0.00%|# Single-treaded run
14| 0| 0| 0| 0.00%|prof = pprofile.Profile()
15| 0| 0| 0| 0.00%|with prof:
16| 0| 0| 0| 0.00%| func()
(call)| 1| 1.00003| 1.00003| 99.99%|# ./demo/embedded.py:7 func
17| 0| 0| 0| 0.00%|prof.annotate(sys.stdout, __file__)
18| 0| 0| 0| 0.00%|
19| 0| 0| 0| 0.00%|# Dual-threaded run
20| 0| 0| 0| 0.00%|t1 = threading.Thread(target=func)
21| 0| 0| 0| 0.00%|prof = pprofile.Profile()
22| 0| 0| 0| 0.00%|with prof:
23| 0| 0| 0| 0.00%| t1.start()
24| 0| 0| 0| 0.00%| func()
25| 0| 0| 0| 0.00%| t1.join()
26| 0| 0| 0| 0.00%|prof.annotate(sys.stdout, __file__)
Total duration: 1.00129s
File: demo/embedded.py
File duration: 1.00004s (99.88%)
Line #| Hits| Time| Time per hit| %|Source code
------+----------+-------------+-------------+-------+-----------
[...]
7| 1| 1.50204e-05| 1.50204e-05| 0.00%|def func():
8| 0| 0| 0| 0.00%| # Busy loop, so context switches happen
9| 1| 2.38419e-05| 2.38419e-05| 0.00%| end = time.time() + 1
10| 64598| 0.538571| 8.33728e-06| 53.79%| while time.time() < end:
11| 64597| 0.461432| 7.14324e-06| 46.08%| pass
[...]
This also means that the sum of the percentage of all lines can exceed 100%. It
can reach the number of concurrent threads (200% with 2 threads being busy for
the whole profiled execution time, etc).
Example with 3 threads::
$ pprofile demo/threads.py
Command line: ['demo/threads.py']
Total duration: 1.00798s
File: demo/threads.py
File duration: 3.00604s (298.22%)
Line #| Hits| Time| Time per hit| %|Source code
------+----------+-------------+-------------+-------+-----------
1| 2| 3.21865e-05| 1.60933e-05| 0.00%|import threading
2| 1| 6.91414e-06| 6.91414e-06| 0.00%|import time
3| 0| 0| 0| 0.00%|
4| 4| 3.91006e-05| 9.77516e-06| 0.00%|def func():
5| 3| 3.00539| 1.0018|298.16%| time.sleep(1)
6| 0| 0| 0| 0.00%|
7| 2| 2.31266e-05| 1.15633e-05| 0.00%|def func2():
8| 1| 2.38419e-05| 2.38419e-05| 0.00%| pass
9| 0| 0| 0| 0.00%|
10| 1| 1.81198e-05| 1.81198e-05| 0.00%|t1 = threading.Thread(target=func)
(call)| 1| 0.000612974| 0.000612974| 0.06%|# /usr/lib/python2.7/threading.py:436 __init__
11| 1| 1.57356e-05| 1.57356e-05| 0.00%|t2 = threading.Thread(target=func)
(call)| 1| 0.000438213| 0.000438213| 0.04%|# /usr/lib/python2.7/threading.py:436 __init__
12| 1| 6.60419e-05| 6.60419e-05| 0.01%|t1.start()
(call)| 1| 0.000913858| 0.000913858| 0.09%|# /usr/lib/python2.7/threading.py:485 start
13| 1| 6.8903e-05| 6.8903e-05| 0.01%|t2.start()
(call)| 1| 0.00167513| 0.00167513| 0.17%|# /usr/lib/python2.7/threading.py:485 start
14| 1| 0.000200272| 0.000200272| 0.02%|(func(), func2())
(call)| 1| 1.00274| 1.00274| 99.48%|# demo/threads.py:4 func
(call)| 1| 4.19617e-05| 4.19617e-05| 0.00%|# demo/threads.py:7 func2
15| 1| 9.58443e-05| 9.58443e-05| 0.01%|t1.join()
(call)| 1| 0.000411987| 0.000411987| 0.04%|# /usr/lib/python2.7/threading.py:653 join
16| 1| 5.29289e-05| 5.29289e-05| 0.01%|t2.join()
(call)| 1| 0.000316143| 0.000316143| 0.03%|# /usr/lib/python2.7/threading.py:653 join
Note that the call time is not added to file total: it's already accounted
for inside "func".
Why another profiler ?
======================
Python's standard profiling tools have a callable-level granularity, which
means it is only possible to tell which function is a hot-spot, not which
lines in that function.
Robert Kern's line_profiler_ is a very nice alternative providing line-level
profiling granularity, but in my opinion it has a few drawbacks which (in
addition to the attractive technical challenge) made me start pprofile:
- It is not pure-python. This choice makes sense for performance
but makes usage with pypy difficult and requires installation (I value
execution straight from checkout).
- It requires source code modification to select what should be profiled.
I prefer to have the option to do an in-depth, non-intrusive profiling.
- As an effect of previous point, it does not have a notion above individual
callable, annotating functions but not whole files - preventing module
import profiling.
- Profiling recursive code provides unexpected results (recursion cost is
accumulated on callable's first line) because it doesn't track call stack.
This may be unintended, and may be fixed at some point in line_profiler.
.. _line_profiler: https://github.com/rkern/line_profiler
.. _`Callgrind Profile Format`: http://valgrind.org/docs/manual/cl-format.html
.. _kcachegrind: http://kcachegrind.sourceforge.net
.. _qcachegrind: http://sourceforge.net/projects/qcachegrindwin/
Raw data
{
"_id": null,
"home_page": "http://github.com/vpelletier/pprofile",
"name": "pprofile",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Vincent Pelletier",
"author_email": "plr.vincent@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/0c/ad/412a6ae455a682679ffccdac11cfba7f7e3f16e25d6fd84089ca47bbed75/pprofile-2.2.0.tar.gz",
"platform": "any",
"description": ".. contents::\n\nLine-granularity, thread-aware deterministic and statistic pure-python profiler\n\nInspired from Robert Kern's line_profiler_ .\n\nUsage\n=====\n\nAs a command::\n\n $ pprofile some_python_executable arg1 ...\n\nOnce `some_python_executable` returns, prints annotated code of each file\ninvolved in the execution.\n\nAs a command, ignoring any files from default `sys.path` (ie, python modules\nthemselves), for shorter output::\n\n $ pprofile --exclude-syspath some_python_executable arg1 ...\n\nExecuting a module, like :code:`python -m`. `--exclude-syspath` is not\nrecommended in this mode, as it will likely hide what you intend to profile.\nAlso, explicitly ending pprofile arguments with `--` will prevent accidentally\nstealing command's arguments::\n\n $ pprofile -m some_python_module -- arg1 ...\n\nAs a module:\n\n.. code:: python\n\n import pprofile\n\n def someHotSpotCallable():\n # Deterministic profiler\n prof = pprofile.Profile()\n with prof():\n # Code to profile\n prof.print_stats()\n\n def someOtherHotSpotCallable():\n # Statistic profiler\n prof = pprofile.StatisticalProfile()\n with prof(\n period=0.001, # Sample every 1ms\n single=True, # Only sample current thread\n ):\n # Code to profile\n prof.print_stats()\n\nFor advanced usage, see :code:`pprofile --help` and :code:`pydoc pprofile`.\n\nProfiling overhead\n------------------\n\npprofile default mode (`Deterministic profiling`_) has a large overhead.\nPart of the reason being that it is written to be as portable as possible\n(so no C extension). This large overhead can be an issue, which can be\navoided by using `Statistic profiling`_ at the cost of some result\nreadability decrease.\n\nRule of thumb:\n\n+-----------------------------+----------------------------+------------------------+\n| Code to profile runs for... | `Deterministic profiling`_ | `Statistic profiling`_ |\n+=============================+============================+========================+\n| a few seconds | Yes | No [#]_ |\n+-----------------------------+----------------------------+------------------------+\n| a few minutes | Maybe | Yes |\n+-----------------------------+----------------------------+------------------------+\n| more (ex: daemon) | No | Yes [#]_ |\n+-----------------------------+----------------------------+------------------------+\n\nOnce you identified the hot spot and you decide you need finer-grained\nprofiling to understand what needs fixing, you should try to make to-profile\ncode run for shorter time so you can reasonably use deterministic profiling:\nuse a smaller data set triggering the same code path, modify the code to only\nenable profiling on small pieces of code...\n\n.. [#] Statistic profiling will not have time to collect\n enough samples to produce usable output.\n\n.. [#] You may want to consider triggering pprofile from\n a signal handler or other IPC mechanism to profile\n a shorter subset. See `zpprofile.py` for how it can\n be used to profile code inside a running (zope)\n service (in which case the IPC mechanism is just\n Zope normal URL handling).\n\nOutput\n======\n\nSupported output formats.\n\nCallgrind\n---------\n\nThe most useful output mode of pprofile is `Callgrind Profile Format`_, allows\nbrowsing profiling results with kcachegrind_ (or qcachegrind_ on Windows).\n\n::\n\n $ pprofile --format callgrind --out cachegrind.out.threads demo/threads.py\n\nCallgrind format is implicitly enabled if ``--out`` basename starts with\n``cachegrind.out.``, so above command can be simplified as::\n\n $ pprofile --out cachegrind.out.threads demo/threads.py\n\nIf you are analyzing callgrind traces on a different machine, you may want to\nuse the ``--zipfile`` option to generate a zip file containing all files::\n\n $ pprofile --out cachegrind.out.threads --zipfile threads_source.zip demo/threads.py\n\nGenerated files will use relative paths, so you can extract generated archive\nin the same path as profiling result, and kcachegrind will load them - and not\nyour system-wide files, which may differ.\n\nAnnotated code\n--------------\n\nHuman-readable output, but can become difficult to use with large programs.\n\n::\n\n $ pprofile demo/threads.py\n\nProfiling modes\n===============\n\nDeterministic profiling\n-----------------------\n\nIn deterministic profiling mode, pprofile gets notified of each executed line.\nThis mode generates very detailed reports, but at the cost of a large overhead.\nAlso, profiling hooks being per-thread, either profiling must be enable before\nspawning threads (if you want to profile more than just the current thread),\nor profiled application must provide ways of enabling profiling afterwards\n- which is not very convenient.\n\n::\n\n $ pprofile --threads 0 demo/threads.py\n Command line: ['demo/threads.py']\n Total duration: 1.00573s\n File: demo/threads.py\n File duration: 1.00168s (99.60%)\n Line #| Hits| Time| Time per hit| %|Source code\n ------+----------+-------------+-------------+-------+-----------\n 1| 2| 3.21865e-05| 1.60933e-05| 0.00%|import threading\n 2| 1| 5.96046e-06| 5.96046e-06| 0.00%|import time\n 3| 0| 0| 0| 0.00%|\n 4| 2| 1.5974e-05| 7.98702e-06| 0.00%|def func():\n 5| 1| 1.00111| 1.00111| 99.54%| time.sleep(1)\n 6| 0| 0| 0| 0.00%|\n 7| 2| 2.00272e-05| 1.00136e-05| 0.00%|def func2():\n 8| 1| 1.69277e-05| 1.69277e-05| 0.00%| pass\n 9| 0| 0| 0| 0.00%|\n 10| 1| 1.81198e-05| 1.81198e-05| 0.00%|t1 = threading.Thread(target=func)\n (call)| 1| 0.000610828| 0.000610828| 0.06%|# /usr/lib/python2.7/threading.py:436 __init__\n 11| 1| 1.52588e-05| 1.52588e-05| 0.00%|t2 = threading.Thread(target=func)\n (call)| 1| 0.000438929| 0.000438929| 0.04%|# /usr/lib/python2.7/threading.py:436 __init__\n 12| 1| 4.79221e-05| 4.79221e-05| 0.00%|t1.start()\n (call)| 1| 0.000843048| 0.000843048| 0.08%|# /usr/lib/python2.7/threading.py:485 start\n 13| 1| 6.48499e-05| 6.48499e-05| 0.01%|t2.start()\n (call)| 1| 0.00115609| 0.00115609| 0.11%|# /usr/lib/python2.7/threading.py:485 start\n 14| 1| 0.000205994| 0.000205994| 0.02%|(func(), func2())\n (call)| 1| 1.00112| 1.00112| 99.54%|# demo/threads.py:4 func\n (call)| 1| 3.09944e-05| 3.09944e-05| 0.00%|# demo/threads.py:7 func2\n 15| 1| 7.62939e-05| 7.62939e-05| 0.01%|t1.join()\n (call)| 1| 0.000423908| 0.000423908| 0.04%|# /usr/lib/python2.7/threading.py:653 join\n 16| 1| 5.26905e-05| 5.26905e-05| 0.01%|t2.join()\n (call)| 1| 0.000320196| 0.000320196| 0.03%|# /usr/lib/python2.7/threading.py:653 join\n\nNote that time.sleep call is not counted as such. For some reason, python is\nnot generating c_call/c_return/c_exception events (which are ignored by current\ncode, as a result).\n\nStatistic profiling\n-------------------\n\nIn statistic profiling mode, pprofile periodically snapshots the current\ncallstack(s) of current process to see what is being executed.\nAs a result, profiler overhead can be dramatically reduced, making it possible\nto profile real workloads. Also, as statistic profiling acts at the\nwhole-process level, it can be toggled independently of profiled code.\n\nThe downside of statistic profiling is that output lacks timing information,\nwhich makes it harder to understand.\n\n::\n\n $ pprofile --statistic .01 demo/threads.py\n Command line: ['demo/threads.py']\n Total duration: 1.0026s\n File: demo/threads.py\n File duration: 0s (0.00%)\n Line #| Hits| Time| Time per hit| %|Source code\n ------+----------+-------------+-------------+-------+-----------\n 1| 0| 0| 0| 0.00%|import threading\n 2| 0| 0| 0| 0.00%|import time\n 3| 0| 0| 0| 0.00%|\n 4| 0| 0| 0| 0.00%|def func():\n 5| 288| 0| 0| 0.00%| time.sleep(1)\n 6| 0| 0| 0| 0.00%|\n 7| 0| 0| 0| 0.00%|def func2():\n 8| 0| 0| 0| 0.00%| pass\n 9| 0| 0| 0| 0.00%|\n 10| 0| 0| 0| 0.00%|t1 = threading.Thread(target=func)\n 11| 0| 0| 0| 0.00%|t2 = threading.Thread(target=func)\n 12| 0| 0| 0| 0.00%|t1.start()\n 13| 0| 0| 0| 0.00%|t2.start()\n 14| 0| 0| 0| 0.00%|(func(), func2())\n (call)| 96| 0| 0| 0.00%|# demo/threads.py:4 func\n 15| 0| 0| 0| 0.00%|t1.join()\n 16| 0| 0| 0| 0.00%|t2.join()\n File: /usr/lib/python2.7/threading.py\n File duration: 0s (0.00%)\n Line #| Hits| Time| Time per hit| %|Source code\n ------+----------+-------------+-------------+-------+-----------\n [...]\n 308| 0| 0| 0| 0.00%| def wait(self, timeout=None):\n [...]\n 338| 0| 0| 0| 0.00%| if timeout is None:\n 339| 1| 0| 0| 0.00%| waiter.acquire()\n 340| 0| 0| 0| 0.00%| if __debug__:\n [...]\n 600| 0| 0| 0| 0.00%| def wait(self, timeout=None):\n [...]\n 617| 0| 0| 0| 0.00%| if not self.__flag:\n 618| 0| 0| 0| 0.00%| self.__cond.wait(timeout)\n (call)| 1| 0| 0| 0.00%|# /usr/lib/python2.7/threading.py:308 wait\n [...]\n 724| 0| 0| 0| 0.00%| def start(self):\n [...]\n 748| 0| 0| 0| 0.00%| self.__started.wait()\n (call)| 1| 0| 0| 0.00%|# /usr/lib/python2.7/threading.py:600 wait\n 749| 0| 0| 0| 0.00%|\n 750| 0| 0| 0| 0.00%| def run(self):\n [...]\n 760| 0| 0| 0| 0.00%| if self.__target:\n 761| 0| 0| 0| 0.00%| self.__target(*self.__args, **self.__kwargs)\n (call)| 192| 0| 0| 0.00%|# demo/threads.py:4 func\n 762| 0| 0| 0| 0.00%| finally:\n [...]\n 767| 0| 0| 0| 0.00%| def __bootstrap(self):\n [...]\n 780| 0| 0| 0| 0.00%| try:\n 781| 0| 0| 0| 0.00%| self.__bootstrap_inner()\n (call)| 192| 0| 0| 0.00%|# /usr/lib/python2.7/threading.py:790 __bootstrap_inner\n [...]\n 790| 0| 0| 0| 0.00%| def __bootstrap_inner(self):\n [...]\n 807| 0| 0| 0| 0.00%| try:\n 808| 0| 0| 0| 0.00%| self.run()\n (call)| 192| 0| 0| 0.00%|# /usr/lib/python2.7/threading.py:750 run\n\nSome details are lost (not all executed lines have a non-null hit-count), but\nthe hot spot is still easily identifiable in this trivial example, and its call\nstack is still visible.\n\nThread-aware profiling\n======================\n\n``ThreadProfile`` class provides the same features as ``Profile``, but uses\n``threading.settrace`` to propagate tracing to ``threading.Thread`` threads\nstarted after profiling is enabled.\n\nLimitations\n-----------\n\nThe time spent in another thread is not discounted from interrupted line.\nOn the long run, it should not be a problem if switches are evenly distributed\namong lines, but threads executing fewer lines will appear as eating more CPU\ntime than they really do.\n\nThis is not specific to simultaneous multi-thread profiling: profiling a single\nthread of a multi-threaded application will also be polluted by time spent in\nother threads.\n\nExample (lines are reported as taking longer to execute when profiled along\nwith another thread - although the other thread is not profiled)::\n\n $ demo/embedded.py\n Total duration: 1.00013s\n File: demo/embedded.py\n File duration: 1.00003s (99.99%)\n Line #| Hits| Time| Time per hit| %|Source code\n ------+----------+-------------+-------------+-------+-----------\n 1| 0| 0| 0| 0.00%|#!/usr/bin/env python\n 2| 0| 0| 0| 0.00%|import threading\n 3| 0| 0| 0| 0.00%|import pprofile\n 4| 0| 0| 0| 0.00%|import time\n 5| 0| 0| 0| 0.00%|import sys\n 6| 0| 0| 0| 0.00%|\n 7| 1| 1.5974e-05| 1.5974e-05| 0.00%|def func():\n 8| 0| 0| 0| 0.00%| # Busy loop, so context switches happen\n 9| 1| 1.40667e-05| 1.40667e-05| 0.00%| end = time.time() + 1\n 10| 146604| 0.511392| 3.48826e-06| 51.13%| while time.time() < end:\n 11| 146603| 0.48861| 3.33288e-06| 48.85%| pass\n 12| 0| 0| 0| 0.00%|\n 13| 0| 0| 0| 0.00%|# Single-treaded run\n 14| 0| 0| 0| 0.00%|prof = pprofile.Profile()\n 15| 0| 0| 0| 0.00%|with prof:\n 16| 0| 0| 0| 0.00%| func()\n (call)| 1| 1.00003| 1.00003| 99.99%|# ./demo/embedded.py:7 func\n 17| 0| 0| 0| 0.00%|prof.annotate(sys.stdout, __file__)\n 18| 0| 0| 0| 0.00%|\n 19| 0| 0| 0| 0.00%|# Dual-threaded run\n 20| 0| 0| 0| 0.00%|t1 = threading.Thread(target=func)\n 21| 0| 0| 0| 0.00%|prof = pprofile.Profile()\n 22| 0| 0| 0| 0.00%|with prof:\n 23| 0| 0| 0| 0.00%| t1.start()\n 24| 0| 0| 0| 0.00%| func()\n 25| 0| 0| 0| 0.00%| t1.join()\n 26| 0| 0| 0| 0.00%|prof.annotate(sys.stdout, __file__)\n Total duration: 1.00129s\n File: demo/embedded.py\n File duration: 1.00004s (99.88%)\n Line #| Hits| Time| Time per hit| %|Source code\n ------+----------+-------------+-------------+-------+-----------\n [...]\n 7| 1| 1.50204e-05| 1.50204e-05| 0.00%|def func():\n 8| 0| 0| 0| 0.00%| # Busy loop, so context switches happen\n 9| 1| 2.38419e-05| 2.38419e-05| 0.00%| end = time.time() + 1\n 10| 64598| 0.538571| 8.33728e-06| 53.79%| while time.time() < end:\n 11| 64597| 0.461432| 7.14324e-06| 46.08%| pass\n [...]\n\nThis also means that the sum of the percentage of all lines can exceed 100%. It\ncan reach the number of concurrent threads (200% with 2 threads being busy for\nthe whole profiled execution time, etc).\n\nExample with 3 threads::\n\n $ pprofile demo/threads.py\n Command line: ['demo/threads.py']\n Total duration: 1.00798s\n File: demo/threads.py\n File duration: 3.00604s (298.22%)\n Line #| Hits| Time| Time per hit| %|Source code\n ------+----------+-------------+-------------+-------+-----------\n 1| 2| 3.21865e-05| 1.60933e-05| 0.00%|import threading\n 2| 1| 6.91414e-06| 6.91414e-06| 0.00%|import time\n 3| 0| 0| 0| 0.00%|\n 4| 4| 3.91006e-05| 9.77516e-06| 0.00%|def func():\n 5| 3| 3.00539| 1.0018|298.16%| time.sleep(1)\n 6| 0| 0| 0| 0.00%|\n 7| 2| 2.31266e-05| 1.15633e-05| 0.00%|def func2():\n 8| 1| 2.38419e-05| 2.38419e-05| 0.00%| pass\n 9| 0| 0| 0| 0.00%|\n 10| 1| 1.81198e-05| 1.81198e-05| 0.00%|t1 = threading.Thread(target=func)\n (call)| 1| 0.000612974| 0.000612974| 0.06%|# /usr/lib/python2.7/threading.py:436 __init__\n 11| 1| 1.57356e-05| 1.57356e-05| 0.00%|t2 = threading.Thread(target=func)\n (call)| 1| 0.000438213| 0.000438213| 0.04%|# /usr/lib/python2.7/threading.py:436 __init__\n 12| 1| 6.60419e-05| 6.60419e-05| 0.01%|t1.start()\n (call)| 1| 0.000913858| 0.000913858| 0.09%|# /usr/lib/python2.7/threading.py:485 start\n 13| 1| 6.8903e-05| 6.8903e-05| 0.01%|t2.start()\n (call)| 1| 0.00167513| 0.00167513| 0.17%|# /usr/lib/python2.7/threading.py:485 start\n 14| 1| 0.000200272| 0.000200272| 0.02%|(func(), func2())\n (call)| 1| 1.00274| 1.00274| 99.48%|# demo/threads.py:4 func\n (call)| 1| 4.19617e-05| 4.19617e-05| 0.00%|# demo/threads.py:7 func2\n 15| 1| 9.58443e-05| 9.58443e-05| 0.01%|t1.join()\n (call)| 1| 0.000411987| 0.000411987| 0.04%|# /usr/lib/python2.7/threading.py:653 join\n 16| 1| 5.29289e-05| 5.29289e-05| 0.01%|t2.join()\n (call)| 1| 0.000316143| 0.000316143| 0.03%|# /usr/lib/python2.7/threading.py:653 join\n\nNote that the call time is not added to file total: it's already accounted\nfor inside \"func\".\n\nWhy another profiler ?\n======================\n\nPython's standard profiling tools have a callable-level granularity, which\nmeans it is only possible to tell which function is a hot-spot, not which\nlines in that function.\n\nRobert Kern's line_profiler_ is a very nice alternative providing line-level\nprofiling granularity, but in my opinion it has a few drawbacks which (in\naddition to the attractive technical challenge) made me start pprofile:\n\n- It is not pure-python. This choice makes sense for performance\n but makes usage with pypy difficult and requires installation (I value\n execution straight from checkout).\n\n- It requires source code modification to select what should be profiled.\n I prefer to have the option to do an in-depth, non-intrusive profiling.\n\n- As an effect of previous point, it does not have a notion above individual\n callable, annotating functions but not whole files - preventing module\n import profiling.\n\n- Profiling recursive code provides unexpected results (recursion cost is\n accumulated on callable's first line) because it doesn't track call stack.\n This may be unintended, and may be fixed at some point in line_profiler.\n\n.. _line_profiler: https://github.com/rkern/line_profiler\n.. _`Callgrind Profile Format`: http://valgrind.org/docs/manual/cl-format.html\n.. _kcachegrind: http://kcachegrind.sourceforge.net\n.. _qcachegrind: http://sourceforge.net/projects/qcachegrindwin/\n",
"bugtrack_url": null,
"license": "GPL 2+",
"summary": "Line-granularity, thread-aware deterministic and statistic pure-python profiler",
"version": "2.2.0",
"project_urls": {
"Homepage": "http://github.com/vpelletier/pprofile"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0cad412a6ae455a682679ffccdac11cfba7f7e3f16e25d6fd84089ca47bbed75",
"md5": "e0e83dbc1768fae56d214b2501769449",
"sha256": "edd57f35f2b3fb84e7d01aa310a44bfabacc66ffc353dce147348ec14de3c7d9"
},
"downloads": -1,
"filename": "pprofile-2.2.0.tar.gz",
"has_sig": false,
"md5_digest": "e0e83dbc1768fae56d214b2501769449",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 55988,
"upload_time": "2024-09-06T05:01:36",
"upload_time_iso_8601": "2024-09-06T05:01:36.872620Z",
"url": "https://files.pythonhosted.org/packages/0c/ad/412a6ae455a682679ffccdac11cfba7f7e3f16e25d6fd84089ca47bbed75/pprofile-2.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-06 05:01:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "vpelletier",
"github_project": "pprofile",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pprofile"
}