Skip to content

ENH: more intuitive profiling-target selection #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ff25243
WIP: eager on-import profiling
TTsangSC Apr 20, 2025
75bd3b0
Tests: `~.autoprofile.eager_preimports`
TTsangSC Apr 20, 2025
d7c5d87
Eager pre-importing in `kernprof`
TTsangSC Apr 20, 2025
c2d7cb1
Extended `--eager-preimports`
TTsangSC Apr 20, 2025
1e21911
More aggressive on-import profiling
TTsangSC Apr 20, 2025
5a3ddba
Stub files
TTsangSC Apr 21, 2025
e5d4445
Test for `--eager-preimports`
TTsangSC Apr 21, 2025
fe235ad
Profiling-target-adding tests
TTsangSC Apr 21, 2025
0ef7cbe
Fixed recursion bug
TTsangSC Apr 21, 2025
c92b00c
CHANGELOG entry and doc fix
TTsangSC Apr 21, 2025
880c3ef
Compatibility fix with `pytest < 8`
TTsangSC Apr 21, 2025
001117c
Fixed `traceback` call (Python < 3.10 compat.)
TTsangSC Apr 21, 2025
0cc3dd6
Scope matching for `LineProfiler.add_*()`
TTsangSC Apr 24, 2025
00b4ff0
Tests and fixes
TTsangSC Apr 27, 2025
d987b56
Fixed typo
TTsangSC Apr 27, 2025
04ccce6
Refactoring: default to eager imports
TTsangSC May 18, 2025
57d8b32
Updated tests
TTsangSC May 18, 2025
2407fcc
CI fix
TTsangSC May 18, 2025
a0c164d
`line_profiler` refactoring
TTsangSC May 18, 2025
6e3b435
`line_profiler.line_profiler_utils` module
TTsangSC May 18, 2025
c1942dd
More refactoring
TTsangSC May 19, 2025
3f3fe46
Streamlined `tests/test_eager_preimports.py`
TTsangSC May 19, 2025
3f1588b
Refactored `tests/test_explicit_profile.py`
TTsangSC May 19, 2025
37cc83f
Refactored `tests/test_line_profiler.py`
TTsangSC May 19, 2025
581efcc
Refactored `tests/test_autoprofile.py`
TTsangSC May 19, 2025
c9e767a
`kernprof.py` doc updates and refactoring
TTsangSC May 19, 2025
0e56350
Doc updates
TTsangSC May 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Changes
* FIX: Fixed explicit profiling of class methods; added handling for profiling static, bound, and partial methods, ``functools.partial`` objects, (cached) properties, and async generator functions
* FIX: Fixed namespace bug when running ``kernprof -m`` on certain modules (e.g. ``calendar`` on Python 3.12+).
* FIX: Fixed ``@contextlib.contextmanager`` bug where the cleanup code (e.g. restoration of ``sys`` attributes) is not run if exceptions occurred inside the context
* ENH: Added CLI arguments ``-c`` to ``kernprof`` for (auto-)profiling module/package/inline-script execution instead of that of script files; passing ``'-'`` as the script-file name now also reads from and profiles ``stdin``
* ENH: Added CLI arguments ``-c`` to ``kernprof`` for (auto-)profiling inline-script execution instead of that of script files; passing ``'-'`` as the script-file name now also reads from and profiles ``stdin``
* ENH: ``kernprof --prof-mod`` target entities are now imported and profiled regardless of whether they are directly imported in the run script/module/code (old behavior recoed by passing ``--no-preimports``); made on-import profiling more aggressive so that it doesn't miss entities like class methods and properties

4.2.0
~~~~~
Expand Down
269 changes: 214 additions & 55 deletions kernprof.py

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions line_profiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
pip install line_profiler[all]


.. _line-profiler-basic-usage:

Line Profiler Basic Usage
=========================

To demonstrate line profiling, we first need to generate a Python script to
profile. Write the following code to a file called ``demo_primes.py``.
profile. Write the following code to a file called :file:`demo_primes.py`:

.. code:: python

Expand Down Expand Up @@ -80,8 +82,10 @@ def main():
main()


In this script we explicitly import the ``profile`` function from
``line_profiler``, and then we decorate function of interest with ``@profile``.
In this script we explicitly import the
:py:deco:`profile <line_profiler.explicit_profiler.GlobalProfiler>` function
from :py:mod:`line_profiler`, and then we decorate function of interest with
:py:deco:`profile`.

By default nothing is profiled when running the script.

Expand All @@ -99,17 +103,18 @@ def main():


The quickest way to enable profiling is to set the environment variable
``LINE_PROFILE=1`` and running your script as normal.

.... todo: add a link that points to docs showing all the different ways to enable profiling.
:envvar:`LINE_PROFILE=1 <LINE_PROFILE>` and running your script as normal.

.. .. todo: add a link that points to docs showing all the different ways to
.. .. to enable profiling.

.. code:: bash

LINE_PROFILE=1 python demo_primes.py

This will output 3 files: profile_output.txt, profile_output_<timestamp>.txt,
and profile_output.lprof and stdout will look something like:
This will output 3 files: :file:`profile_output.txt`,
:file:`profile_output_<timestamp>.txt`, and :file:`profile_output.lprof`; and
:file:`stdout` will look something like:


.. code::
Expand Down Expand Up @@ -219,7 +224,7 @@ def main():

* `timeit <https://docs.python.org/3/library/timeit.html>`_: The builtin timeit module for profiling single statements.

* `timerit <https://github.com/Erotemic/timerit>`_: A multi-statements alternative to the builtin ``timeit`` module.
* `timerit <https://github.com/Erotemic/timerit>`_: A multi-statements alternative to the builtin :py:mod:`timeit` module.

* `torch.profiler <https://pytorch.org/docs/stable/profiler.html>`_ tools for profiling torch code.

Expand Down
Loading
Loading