pyperf 2.9.0__tar.gz → 2.11.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. {pyperf-2.9.0/pyperf.egg-info → pyperf-2.11.0}/PKG-INFO +12 -12
  2. {pyperf-2.9.0 → pyperf-2.11.0}/README.rst +7 -8
  3. {pyperf-2.9.0 → pyperf-2.11.0}/doc/api.rst +55 -1
  4. {pyperf-2.9.0 → pyperf-2.11.0}/doc/changelog.rst +37 -0
  5. {pyperf-2.9.0 → pyperf-2.11.0}/doc/conf.py +1 -1
  6. {pyperf-2.9.0 → pyperf-2.11.0}/doc/developer_guide.rst +0 -1
  7. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples.rst +0 -1
  8. {pyperf-2.9.0 → pyperf-2.11.0}/doc/index.rst +2 -2
  9. {pyperf-2.9.0 → pyperf-2.11.0}/doc/run_benchmark.rst +0 -5
  10. {pyperf-2.9.0 → pyperf-2.11.0}/doc/runner.rst +5 -1
  11. {pyperf-2.9.0 → pyperf-2.11.0}/doc/user_guide.rst +0 -2
  12. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/__init__.py +1 -1
  13. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_cli.py +1 -1
  14. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_collect_metadata.py +52 -0
  15. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_cpu_utils.py +5 -13
  16. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_formatter.py +2 -9
  17. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_hooks.py +88 -8
  18. pyperf-2.11.0/pyperf/_inprocess_manager.py +33 -0
  19. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_metadata.py +3 -5
  20. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_psutil_memory.py +7 -2
  21. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_runner.py +46 -8
  22. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_utils.py +10 -18
  23. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_worker.py +7 -5
  24. pyperf-2.11.0/pyperf/tests/test_inprocess.py +144 -0
  25. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_metadata.py +62 -4
  26. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_runner.py +76 -0
  27. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_utils.py +3 -3
  28. {pyperf-2.9.0 → pyperf-2.11.0/pyperf.egg-info}/PKG-INFO +12 -12
  29. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf.egg-info/SOURCES.txt +2 -0
  30. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf.egg-info/entry_points.txt +1 -0
  31. pyperf-2.11.0/pyperf.egg-info/requires.txt +6 -0
  32. {pyperf-2.9.0 → pyperf-2.11.0}/pyproject.toml +15 -2
  33. {pyperf-2.9.0 → pyperf-2.11.0}/tox.ini +1 -7
  34. pyperf-2.9.0/pyperf.egg-info/requires.txt +0 -4
  35. {pyperf-2.9.0 → pyperf-2.11.0}/COPYING +0 -0
  36. {pyperf-2.9.0 → pyperf-2.11.0}/MANIFEST.in +0 -0
  37. {pyperf-2.9.0 → pyperf-2.11.0}/TODO.rst +0 -0
  38. {pyperf-2.9.0 → pyperf-2.11.0}/doc/Makefile +0 -0
  39. {pyperf-2.9.0 → pyperf-2.11.0}/doc/analyze.rst +0 -0
  40. {pyperf-2.9.0 → pyperf-2.11.0}/doc/cli.rst +0 -0
  41. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples/bench_async_func.py +0 -0
  42. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples/bench_async_func_with_loop_factory.py +0 -0
  43. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples/bench_command.py +0 -0
  44. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples/bench_func.py +0 -0
  45. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples/bench_time_func.py +0 -0
  46. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples/bench_timeit.py +0 -0
  47. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples/export_csv.py +0 -0
  48. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples/hist_scipy.py +0 -0
  49. {pyperf-2.9.0 → pyperf-2.11.0}/doc/examples/plot.py +0 -0
  50. {pyperf-2.9.0 → pyperf-2.11.0}/doc/hist_scipy_telco.png +0 -0
  51. {pyperf-2.9.0 → pyperf-2.11.0}/doc/make.bat +0 -0
  52. {pyperf-2.9.0 → pyperf-2.11.0}/doc/outliers.json.gz +0 -0
  53. {pyperf-2.9.0 → pyperf-2.11.0}/doc/py2.json.gz +0 -0
  54. {pyperf-2.9.0 → pyperf-2.11.0}/doc/py3.json.gz +0 -0
  55. {pyperf-2.9.0 → pyperf-2.11.0}/doc/system.rst +0 -0
  56. {pyperf-2.9.0 → pyperf-2.11.0}/doc/timeit_strip.json.gz +0 -0
  57. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/__main__.py +0 -0
  58. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_bench.py +0 -0
  59. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_command.py +0 -0
  60. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_compare.py +0 -0
  61. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_linux_memory.py +0 -0
  62. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_manager.py +0 -0
  63. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_process_time.py +0 -0
  64. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_system.py +0 -0
  65. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_timeit.py +0 -0
  66. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_timeit_cli.py +0 -0
  67. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/_win_memory.py +0 -0
  68. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/__init__.py +0 -0
  69. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/mult_list_bench.py +0 -0
  70. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/mult_list_py36.json +0 -0
  71. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/mult_list_py36_tags.json +0 -0
  72. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/mult_list_py37.json +0 -0
  73. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/mult_list_py37_tags.json +0 -0
  74. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/mult_list_py38.json +0 -0
  75. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/replay.py +0 -0
  76. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/telco.json +0 -0
  77. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_bench.py +0 -0
  78. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_cli.py +0 -0
  79. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_examples.py +0 -0
  80. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_misc.py +0 -0
  81. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_perf_cli.py +0 -0
  82. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_system.py +0 -0
  83. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/test_timeit.py +0 -0
  84. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf/tests/track_memory.json +0 -0
  85. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf.egg-info/dependency_links.txt +0 -0
  86. {pyperf-2.9.0 → pyperf-2.11.0}/pyperf.egg-info/top_level.txt +0 -0
  87. {pyperf-2.9.0 → pyperf-2.11.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: pyperf
3
- Version: 2.9.0
3
+ Version: 2.11.0
4
4
  Summary: Python module to run and analyze benchmarks
5
5
  Author-email: Victor Stinner <vstinner@redhat.com>
6
6
  Maintainer-email: Dong-hee Na <donghee.na@python.org>
@@ -13,12 +13,13 @@ Classifier: Natural Language :: English
13
13
  Classifier: Operating System :: OS Independent
14
14
  Classifier: Programming Language :: Python :: 3
15
15
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
16
- Requires-Python: >=3.9
16
+ Requires-Python: >=3.11
17
17
  Description-Content-Type: text/x-rst
18
18
  License-File: COPYING
19
- Requires-Dist: psutil>=5.9.0
19
+ Requires-Dist: psutil>=5.9.0; sys_platform != "emscripten"
20
20
  Provides-Extra: dev
21
21
  Requires-Dist: tox; extra == "dev"
22
+ Dynamic: license-file
22
23
 
23
24
  ******
24
25
  pyperf
@@ -26,7 +27,11 @@ pyperf
26
27
 
27
28
  .. image:: https://img.shields.io/pypi/v/pyperf.svg
28
29
  :alt: Latest release on the Python Cheeseshop (PyPI)
29
- :target: https://pypi.python.org/pypi/pyperf
30
+ :target: https://pypi.org/project/pyperf/
31
+
32
+ .. image:: https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2Fpsf%2Fpyperf%2Frefs%2Fheads%2Fmain%2Fpyproject.toml
33
+ :alt: Supported Python versions
34
+ :target: https://pypi.org/project/pyperf/
30
35
 
31
36
  .. image:: https://github.com/psf/pyperf/actions/workflows/build.yml/badge.svg
32
37
  :alt: Build status of pyperf on GitHub Actions
@@ -144,17 +149,12 @@ Quick Links
144
149
  * `pyperf project homepage at GitHub
145
150
  <https://github.com/psf/pyperf>`_ (code, bugs)
146
151
  * `Download latest pyperf release at the Python Cheeseshop (PyPI)
147
- <https://pypi.python.org/pypi/pyperf>`_
152
+ <https://pypi.org/project/pyperf/>`_
148
153
 
149
- Command to install pyperf on Python 3::
154
+ Install pyperf::
150
155
 
151
156
  python3 -m pip install pyperf
152
157
 
153
- pyperf requires Python 3.7 or newer.
154
-
155
- Python 2.7 users can use pyperf 1.7.1 which is the last version compatible with
156
- Python 2.7.
157
-
158
158
  pyperf is distributed under the MIT license.
159
159
 
160
160
  The pyperf project is covered by the `PSF Code of Conduct
@@ -4,7 +4,11 @@ pyperf
4
4
 
5
5
  .. image:: https://img.shields.io/pypi/v/pyperf.svg
6
6
  :alt: Latest release on the Python Cheeseshop (PyPI)
7
- :target: https://pypi.python.org/pypi/pyperf
7
+ :target: https://pypi.org/project/pyperf/
8
+
9
+ .. image:: https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2Fpsf%2Fpyperf%2Frefs%2Fheads%2Fmain%2Fpyproject.toml
10
+ :alt: Supported Python versions
11
+ :target: https://pypi.org/project/pyperf/
8
12
 
9
13
  .. image:: https://github.com/psf/pyperf/actions/workflows/build.yml/badge.svg
10
14
  :alt: Build status of pyperf on GitHub Actions
@@ -122,17 +126,12 @@ Quick Links
122
126
  * `pyperf project homepage at GitHub
123
127
  <https://github.com/psf/pyperf>`_ (code, bugs)
124
128
  * `Download latest pyperf release at the Python Cheeseshop (PyPI)
125
- <https://pypi.python.org/pypi/pyperf>`_
129
+ <https://pypi.org/project/pyperf/>`_
126
130
 
127
- Command to install pyperf on Python 3::
131
+ Install pyperf::
128
132
 
129
133
  python3 -m pip install pyperf
130
134
 
131
- pyperf requires Python 3.7 or newer.
132
-
133
- Python 2.7 users can use pyperf 1.7.1 which is the last version compatible with
134
- Python 2.7.
135
-
136
135
  pyperf is distributed under the MIT license.
137
136
 
138
137
  The pyperf project is covered by the `PSF Code of Conduct
@@ -485,10 +485,39 @@ BenchmarkSuite class
485
485
  It can be ``None``.
486
486
 
487
487
 
488
+ HookBase class
489
+ --------------
490
+
491
+ .. class:: HookBase()
492
+
493
+ Hook used to do some actions before and after benchmark's run. It can collect
494
+ statistics, run external tools.
495
+
496
+ Methods:
497
+
498
+ .. method:: __enter__()
499
+
500
+ Called immediately before running benchmark code.
501
+
502
+ May be called multiple times per instance.
503
+
504
+ .. method:: __exit__(exc_type, exc_value, traceback):
505
+
506
+ Called immediately after running benchmark code.
507
+
508
+ May be called multiple times per instance.
509
+
510
+ .. method:: teardown(metadata)
511
+
512
+ Called when the hook is completed for a process.
513
+
514
+ May add any information collected to the passed-in ``metadata`` dictionary.
515
+
516
+
488
517
  Runner class
489
518
  ------------
490
519
 
491
- .. class:: Runner(values=3, warmups=1, processes=20, loops=0, min_time=0.1, metadata=None, show_name=True, program_args=None, add_cmdline_args=None)
520
+ .. class:: Runner(values=3, warmups=1, processes=20, loops=0, min_time=0.1, metadata=None, show_name=True, program_args=None, add_cmdline_args=None, hooks=None)
492
521
 
493
522
  Tool to run a benchmark in text mode.
494
523
 
@@ -512,6 +541,10 @@ Runner class
512
541
  (``list``) which must be modified in place and *args* is the :attr:`args`
513
542
  attribute of the runner.
514
543
 
544
+ *hooks* is a list or tuple of custom hooks. Each hook should implement
545
+ :class:`HookBase` and have ``name`` attribute and ``load()`` method that
546
+ returns instance of the hook.
547
+
515
548
  If *show_name* is true, displays the benchmark name.
516
549
 
517
550
  If isolated CPUs are detected, the CPU affinity is automatically
@@ -659,6 +692,23 @@ Runner class
659
692
 
660
693
  Return the :attr:`args` attribute.
661
694
 
695
+ .. method:: is_worker()
696
+
697
+ Return ``True`` in a benchmark worker process, or ``False`` in the
698
+ main process. Parse command line arguments if they have not been parsed
699
+ yet.
700
+
701
+ Worker processes re-execute the benchmark script and may skip benchmarks,
702
+ returning ``None``. Run all benchmarks in both the main process and
703
+ workers, and use this method to guard code that processes their results::
704
+
705
+ runner = pyperf.Runner()
706
+ benchmarks = [runner.bench_func("first", first_func),
707
+ runner.bench_func("second", second_func)]
708
+ if not runner.is_worker():
709
+ suite = pyperf.BenchmarkSuite(benchmarks)
710
+ suite.dump("benchmarks.json")
711
+
662
712
  Attributes:
663
713
 
664
714
  .. attribute:: args
@@ -725,6 +775,10 @@ CPU metadata:
725
775
  * ``cpu_freq``: Frequency of CPUs
726
776
  * ``cpu_machine``: CPU machine
727
777
  * ``cpu_model_name``: CPU model name
778
+ * ``cpu_quota``: if set, the Linux cgroup CPU bandwidth limit of the process,
779
+ as a number of CPUs (ex: ``2 CPUs`` for ``docker run --cpus=2``). The
780
+ process can use that much CPU time per period, even if ``cpu_count`` is
781
+ larger.
728
782
  * ``cpu_temp``: Temperature of CPUs
729
783
 
730
784
  System metadata:
@@ -1,6 +1,43 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ Version 2.11.0 (2026-11-08)
5
+ ---------------------------
6
+
7
+ * Feature: Add ``--in-process`` option to run benchmarks in the current process.
8
+ This is useful when you cannot spawn child processes, such as on WASM environments.
9
+ Patch by Gyeongjae Choi.
10
+ * Feature: Add a new ``tachyon`` hook to profile benchmarks with the Tachyon
11
+ sampling profiler of Python 3.15 (``--hook tachyon``).
12
+ Patch by Maurycy Pietrzak.
13
+ * Feature: Hooks can now be registered from code with the new ``hooks``
14
+ parameter of :class:`Runner`.
15
+ Patch by Sergey Miryanov.
16
+ * Feature: Add :meth:`Runner.is_worker` to guard code which processes
17
+ benchmark results in the main process.
18
+ Patch by joo00der.
19
+ * Feature: Add ``cpu_quota`` metadata: the Linux cgroup CPU bandwidth limit of
20
+ the process (ex: ``docker run --cpus=2``).
21
+ Patch by Manu Nicholas Jacob.
22
+ * Bugfix: Allow a ``mem_max_rss`` metadata value of zero.
23
+ Patch by Samartha.
24
+ * Bugfix: ``psutil`` is now optional on the Emscripten platform.
25
+ Patch by Gyeongjae Choi.
26
+ * Optimization: ``format_number()`` and ``_common_metadata()`` are faster.
27
+ Patch by Deban Kumar Sahu.
28
+ * Maintenance: ``pyperf`` now requires Python 3.11 or higher.
29
+ Patch by Hugo van Kemenade.
30
+
31
+ Version 2.10.0 (2026-02-07)
32
+ ---------------------------
33
+
34
+ * Feature: JIT builds of CPython are now recognized as a JIT implementation by :func:`perf.python_has_jit`.
35
+ Patch by Ken Jin.
36
+ * Feature: Memory tracker now supports BSD.
37
+ Patch by Laurent Cheylus and Victor Stinner.
38
+ * Maintenance: ``pyperf`` now requires Python 3.9 or higher.
39
+ Patch by Alex Willmer.
40
+
4
41
  Version 2.9.0 (2025-03-03)
5
42
  --------------------------
6
43
 
@@ -50,7 +50,7 @@ author = 'Victor Stinner'
50
50
  # built documents.
51
51
  #
52
52
  # The short X.Y version.
53
- version = release = '2.9.0'
53
+ version = release = '2.11.0'
54
54
 
55
55
  # The language for content autogenerated by Sphinx. Refer to documentation
56
56
  # for a list of supported languages.
@@ -8,4 +8,3 @@ Perf Developer Guide
8
8
  examples
9
9
  api
10
10
  changelog
11
-
@@ -122,4 +122,3 @@ Usage::
122
122
 
123
123
  $ python3 plot.py telco.json telco.csv
124
124
  $ python3 plot.py result.json -b telco telco.csv
125
-
@@ -46,11 +46,11 @@ Quick Links:
46
46
  * `pyperf project homepage at GitHub
47
47
  <https://github.com/psf/pyperf>`_ (code, bugs)
48
48
  * `Download latest pyperf release at the Python Cheeseshop (PyPI)
49
- <https://pypi.python.org/pypi/pyperf>`_
49
+ <https://pypi.org/project/pyperf/>`_
50
50
 
51
51
  Other Python benchmark projects:
52
52
 
53
- * `pyperformance <https://pypi.python.org/pypi/pyperformance>`_: the Python
53
+ * `pyperformance <https://pypi.org/project/pyperformance/>`_: the Python
54
54
  benchmark suite which uses ``pyperf``
55
55
  * `Python speed mailing list
56
56
  <https://mail.python.org/mailman/listinfo/speed>`_
@@ -23,11 +23,6 @@ Optional dependencies:
23
23
  * Python module ``psutil``. Install: ``python3 -m pip install -U psutil``.
24
24
  * When you are using macOS, you need to install ``psutil`` if you want to use ``--track-memory`` option.
25
25
 
26
- pyperf requires Python 3.6 or newer.
27
-
28
- Python 2.7 users can use pyperf 1.7.1 which is the last version compatible with
29
- Python 2.7.
30
-
31
26
 
32
27
  Run a benchmark
33
28
  ===============
@@ -101,6 +101,7 @@ Option::
101
101
  --timeout TIMEOUT
102
102
  --track-memory
103
103
  --tracemalloc
104
+ --in-process
104
105
 
105
106
  * ``--python=PYTHON``: Python executable. By default, use the running Python
106
107
  (``sys.executable``). The Python executable must have the ``pyperf`` module
@@ -146,7 +147,7 @@ Option::
146
147
  There is no time out by default.
147
148
  * ``--tracemalloc``: Use the ``tracemalloc`` module to track Python memory
148
149
  allocation and get the peak of memory usage in metadata
149
- (``tracemalloc_peak``). The module is only available on Python 3.4 and newer.
150
+ (``tracemalloc_peak``).
150
151
  See the `tracemalloc module
151
152
  <https://docs.python.org/dev/library/tracemalloc.html>`_.
152
153
  * ``--track-memory``: get the memory peak usage. it is less accurate than
@@ -155,6 +156,9 @@ Option::
155
156
  ``/proc/self/smaps``. On Windows, get ``PeakPagefileUsage`` of
156
157
  ``GetProcessMemoryInfo()`` (of the current process): the peak value of the
157
158
  Commit Charge during the lifetime of this process.
159
+ * ``--in-process``: run the benchmark in the current process instead of a child process.
160
+ This is useful when you cannot spawn child processes, such as on WASM environments.
161
+ Note that this makes each run affect the next run, as the benchmark is executed in the same process.
158
162
 
159
163
 
160
164
  Internal usage only
@@ -11,5 +11,3 @@ Table of Contents:
11
11
  cli
12
12
  runner
13
13
  system
14
-
15
-
@@ -1,6 +1,6 @@
1
1
  from time import perf_counter
2
2
 
3
- VERSION = (2, 9, 0)
3
+ VERSION = (2, 11, 0)
4
4
  __version__ = '.'.join(map(str, VERSION))
5
5
 
6
6
  # Export pyperf.perf_counter for backward compatibility with pyperf 1.7
@@ -429,7 +429,7 @@ def format_checks(bench, lines=None, check_too_many_processes=False):
429
429
  required_nprocesses is not None and
430
430
  required_nprocesses > len(bench._runs)
431
431
  ):
432
- warn("Not enough samples to get a stable result (95% certainly of less than 1% variation)")
432
+ warn("Not enough samples to get a stable result (95% certainty of less than 1% variation)")
433
433
 
434
434
  # Minimum and maximum, detect obvious outliers
435
435
  for minimum, value in (
@@ -373,6 +373,55 @@ def collect_cpu_affinity(metadata, cpu_affinity, cpu_count):
373
373
  metadata['cpu_affinity'] = format_cpu_list(cpu_affinity)
374
374
 
375
375
 
376
+ def get_cgroup_cpu_quota():
377
+ """Get the Linux cgroup CPU bandwidth limit of the current process.
378
+
379
+ Return the limit as a number of CPUs (quota / period), or None if there
380
+ is no limit or it cannot be read. With cgroup v2, a limit set on a parent
381
+ cgroup also applies, so the smallest limit of the hierarchy is used.
382
+ """
383
+ limits = []
384
+ for line in read_proc("self/cgroup"):
385
+ parts = line.split(":", 2)
386
+ if len(parts) != 3:
387
+ continue
388
+ hierarchy, controllers, path = parts
389
+ path = path.strip("/")
390
+ names = path.split("/") if path else []
391
+ try:
392
+ if hierarchy == "0" and not controllers:
393
+ # cgroup v2: check the cgroup and each of its parents
394
+ for index in range(len(names), -1, -1):
395
+ cpu_max = os.path.join("/sys/fs/cgroup", *names[:index],
396
+ "cpu.max")
397
+ fields = read_first_line(cpu_max).split()
398
+ if len(fields) == 2 and fields[0] != "max":
399
+ limits.append(int(fields[0]) / int(fields[1]))
400
+ elif "cpu" in controllers.split(","):
401
+ # cgroup v1: the cgroup path is not visible inside a container
402
+ for base in (os.path.join("/sys/fs/cgroup/cpu", *names),
403
+ "/sys/fs/cgroup/cpu"):
404
+ quota = read_first_line(os.path.join(base,
405
+ "cpu.cfs_quota_us"))
406
+ period = read_first_line(os.path.join(base,
407
+ "cpu.cfs_period_us"))
408
+ if quota and period:
409
+ if int(quota) > 0:
410
+ limits.append(int(quota) / int(period))
411
+ break
412
+ except (ValueError, ZeroDivisionError):
413
+ continue
414
+ if not limits:
415
+ return None
416
+ return min(limits)
417
+
418
+
419
+ def collect_cpu_quota(metadata):
420
+ quota = get_cgroup_cpu_quota()
421
+ if quota is not None:
422
+ metadata['cpu_quota'] = '%s CPUs' % format(quota, 'g')
423
+
424
+
376
425
  def collect_cpu_model(metadata):
377
426
  for line in read_proc("cpuinfo"):
378
427
  if line.startswith('model name'):
@@ -399,6 +448,9 @@ def collect_cpu_metadata(metadata):
399
448
  cpu_affinity = get_cpu_affinity()
400
449
  collect_cpu_affinity(metadata, cpu_affinity, cpu_count)
401
450
 
451
+ if sys.platform.startswith('linux'):
452
+ collect_cpu_quota(metadata)
453
+
402
454
  all_cpus = cpu_affinity
403
455
  if not all_cpus and cpu_count:
404
456
  all_cpus = tuple(range(cpu_count))
@@ -17,20 +17,10 @@ def get_logical_cpu_count():
17
17
  if psutil is not None:
18
18
  # Number of logical CPUs
19
19
  cpu_count = psutil.cpu_count()
20
- elif hasattr(os, 'cpu_count'):
20
+ else:
21
21
  # Python 3.4
22
+ # Python 3.13+: capped by -X cpu_count=n or $PYTHON_CPU_COUNT if set
22
23
  cpu_count = os.cpu_count()
23
- else:
24
- cpu_count = None
25
- try:
26
- import multiprocessing
27
- except ImportError:
28
- pass
29
- else:
30
- try:
31
- cpu_count = multiprocessing.cpu_count()
32
- except NotImplementedError:
33
- pass
34
24
 
35
25
  if cpu_count is not None and cpu_count < 1:
36
26
  return None
@@ -148,7 +138,7 @@ def get_isolated_cpus():
148
138
 
149
139
 
150
140
  def set_cpu_affinity(cpus):
151
- # Python 3.3 or newer?
141
+ # Availability: some Unix platforms
152
142
  if hasattr(os, 'sched_setaffinity'):
153
143
  os.sched_setaffinity(0, cpus)
154
144
  return True
@@ -161,6 +151,8 @@ def set_cpu_affinity(cpus):
161
151
  except ImportError:
162
152
  return
163
153
 
154
+ # Availability: Linux, Windows, FreeBSD (psutil 2.2.0+)
155
+ # https://psutil.rtfd.io/en/latest/index.html#psutil.Process.cpu_affinity
164
156
  proc = psutil.Process()
165
157
  if not hasattr(proc, 'cpu_affinity'):
166
158
  return
@@ -78,15 +78,8 @@ def format_number(number, unit=None, units=None):
78
78
  number = '10^%s' % pow10
79
79
 
80
80
  if isinstance(number, int) and number > 8192:
81
- pow2 = 0
82
- x = number
83
- while x >= 2:
84
- x, r = divmod(x, 2)
85
- pow2 += 1
86
- if r:
87
- break
88
- if not r:
89
- number = '2^%s' % pow2
81
+ if number & (number-1) == 0:
82
+ number = '2^%s' % (number.bit_length() - 1)
90
83
 
91
84
  if not unit:
92
85
  return str(number)
@@ -5,8 +5,10 @@
5
5
 
6
6
  import abc
7
7
  import importlib.metadata
8
+ import os
8
9
  import os.path
9
10
  import shlex
11
+ import signal
10
12
  import subprocess
11
13
  import sys
12
14
  import tempfile
@@ -16,10 +18,7 @@ import uuid
16
18
  def get_hooks():
17
19
  hook_prefix = "pyperf.hook"
18
20
  entry_points = importlib.metadata.entry_points()
19
- if sys.version_info[:2] < (3, 10):
20
- group = entry_points[hook_prefix]
21
- else:
22
- group = entry_points.select(group=hook_prefix)
21
+ group = entry_points.select(group=hook_prefix)
23
22
  return group
24
23
 
25
24
 
@@ -27,18 +26,18 @@ def get_hook_names():
27
26
  return (x.name for x in get_hooks())
28
27
 
29
28
 
30
- def get_selected_hooks(hook_names):
29
+ def get_selected_hooks(hook_names, hooks=None):
31
30
  if hook_names is None:
32
31
  return
33
32
 
34
- hook_mapping = {hook.name: hook for hook in get_hooks()}
33
+ hook_mapping = {hook.name: hook for hook in get_hooks() + tuple(hooks or ())}
35
34
  for hook_name in hook_names:
36
35
  yield hook_mapping[hook_name]
37
36
 
38
37
 
39
- def instantiate_selected_hooks(hook_names):
38
+ def instantiate_selected_hooks(hook_names, hooks=None):
40
39
  hook_managers = {}
41
- for hook in get_selected_hooks(hook_names):
40
+ for hook in get_selected_hooks(hook_names, hooks):
42
41
  try:
43
42
  hook_managers[hook.name] = hook.load()()
44
43
  except HookError as e:
@@ -168,3 +167,84 @@ class perf_record(HookBase):
168
167
  self.ctl_fd.write(f"{cmd}\n")
169
168
  self.ctl_fd.flush()
170
169
  self.ack_fd.readline()
170
+
171
+
172
+ class tachyon(HookBase):
173
+ """Profile the benchmark using sampling profiler (Tachyon).
174
+
175
+ The value of the `PYPERF_TACHYON_OPTS` environment variable is
176
+ appended to the `profiling.sampling attach` command line.
177
+
178
+ This hook does not generate output filenames. Use -o or --output in
179
+ `PYPERF_TACHYON_OPTS` to control output. For most formats, -o can
180
+ point at an existing directory and the profiler will auto-generate a
181
+ filename inside it.
182
+
183
+ Configuration environment variables:
184
+ PYPERF_TACHYON_OPTS: Extra arguments passed to
185
+ `python -m profiling.sampling attach`.
186
+ """
187
+
188
+ def __init__(self):
189
+ if sys.platform == "win32":
190
+ raise HookError("tachyon hook is not supported on Windows")
191
+
192
+ if sys.version_info < (3, 15):
193
+ raise HookError(
194
+ "tachyon hook requires Python 3.15+, "
195
+ "current version: %s.%s"
196
+ % (sys.version_info.major, sys.version_info.minor)
197
+ )
198
+
199
+ try:
200
+ import profiling.sampling # noqa: F401
201
+ except ImportError:
202
+ raise HookError("profiling.sampling module not available")
203
+
204
+ self.extra_opts = os.environ.get("PYPERF_TACHYON_OPTS", "")
205
+
206
+ self._proc = None
207
+
208
+ def __enter__(self):
209
+ if self._proc is not None:
210
+ self._stop_profiler()
211
+
212
+ cmd = [
213
+ sys.executable,
214
+ "-m", "profiling.sampling",
215
+ "attach",
216
+ str(os.getpid()),
217
+ ]
218
+ cmd += shlex.split(self.extra_opts)
219
+
220
+ self._proc = subprocess.Popen(
221
+ cmd,
222
+ stdout=subprocess.DEVNULL,
223
+ stderr=subprocess.DEVNULL,
224
+ )
225
+
226
+ def __exit__(self, _exc_type, _exc_value, _traceback):
227
+ self._stop_profiler()
228
+
229
+ def _stop_profiler(self):
230
+ if not self._proc:
231
+ return
232
+
233
+ if self._proc.poll() is None:
234
+ self._proc.send_signal(signal.SIGINT)
235
+ try:
236
+ self._proc.wait(timeout=30)
237
+ except subprocess.TimeoutExpired:
238
+ self._proc.terminate()
239
+ try:
240
+ self._proc.wait(timeout=5)
241
+ except subprocess.TimeoutExpired:
242
+ self._proc.kill()
243
+ self._proc.wait()
244
+
245
+ self._proc = None
246
+
247
+ def teardown(self, metadata):
248
+ self._stop_profiler()
249
+ if self.extra_opts:
250
+ metadata["tachyon_extra_opts"] = self.extra_opts
@@ -0,0 +1,33 @@
1
+ import pyperf
2
+ from pyperf._manager import Manager
3
+ from pyperf._worker import WorkerProcessTask
4
+
5
+
6
+ class InProcessManager(Manager):
7
+ def __init__(self, runner, task):
8
+ super().__init__(runner)
9
+ self._task_func = task.task_func
10
+ self._task_name = task.name
11
+ self._func_metadata = {
12
+ k: v for k, v in task.metadata.items() if k not in ("name",)
13
+ }
14
+ self._inner_loops = task.inner_loops
15
+
16
+ def spawn_worker(self, calibrate_loops, calibrate_warmups):
17
+ args = self.args
18
+ args.calibrate_loops = int(calibrate_loops == 1)
19
+ args.recalibrate_loops = int(calibrate_loops > 1)
20
+ args.calibrate_warmups = int(calibrate_warmups == 1)
21
+ args.recalibrate_warmups = int(calibrate_warmups > 1)
22
+
23
+ task = WorkerProcessTask(
24
+ self.runner,
25
+ self._task_name,
26
+ self._task_func,
27
+ self._func_metadata,
28
+ )
29
+ task.inner_loops = self._inner_loops
30
+ run = task.create_run()
31
+
32
+ bench = pyperf.Benchmark((run,))
33
+ return pyperf.BenchmarkSuite([bench])
@@ -14,10 +14,8 @@ def _common_metadata(metadatas):
14
14
 
15
15
  metadata = dict(metadatas[0])
16
16
  for run_metadata in metadatas[1:]:
17
- for key in set(metadata) - set(run_metadata):
18
- del metadata[key]
19
- for key in set(run_metadata) & set(metadata):
20
- if run_metadata[key] != metadata[key]:
17
+ for key in list(metadata):
18
+ if metadata[key] != run_metadata.get(key,None):
21
19
  del metadata[key]
22
20
  return metadata
23
21
 
@@ -63,7 +61,7 @@ def format_noop(value):
63
61
  # types: accepted types
64
62
  _MetadataInfo = collections.namedtuple('_MetadataInfo', 'formatter types check_value unit')
65
63
 
66
- BYTES = _MetadataInfo(format_filesize, (int,), is_strictly_positive, 'byte')
64
+ BYTES = _MetadataInfo(format_filesize, (int,), is_positive, 'byte')
67
65
  DATETIME = _MetadataInfo(format_noop, (str,), None, None)
68
66
  LOOPS = _MetadataInfo(format_number, (int,), is_strictly_positive, 'integer')
69
67
  WARMUPS = _MetadataInfo(format_number, (int,), is_positive, 'integer')
@@ -1,6 +1,6 @@
1
1
  import os
2
2
  try:
3
- from pyperf._utils import USE_PSUTIL
3
+ from pyperf._utils import USE_PSUTIL, BSD
4
4
  if not USE_PSUTIL:
5
5
  raise ImportError
6
6
  else:
@@ -21,7 +21,12 @@ class PeakMemoryUsageThread(threading.Thread):
21
21
  self._quit = False
22
22
 
23
23
  def get(self):
24
- usage = self.process.memory_full_info().uss
24
+ if BSD:
25
+ # USS (Unique Set Size) is not supported on BSD,
26
+ # use RSS (Resident Set Size) instead.
27
+ usage = self.process.memory_info().rss
28
+ else:
29
+ usage = self.process.memory_full_info().uss
25
30
  self.peak_usage = max(self.peak_usage, usage)
26
31
 
27
32
  def run(self):